Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 1 | //===- lib/MC/MCMachOStreamer.cpp - Mach-O Object Output ------------===// |
| 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 | #include "llvm/MC/MCStreamer.h" |
| 11 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCAssembler.h" |
| 13 | #include "llvm/MC/MCContext.h" |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCCodeEmitter.h" |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame^] | 15 | #include "llvm/MC/MCExpr.h" |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCInst.h" |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCSection.h" |
| 18 | #include "llvm/MC/MCSymbol.h" |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | class MCMachOStreamer : public MCStreamer { |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 26 | /// SymbolFlags - We store the value for the 'desc' symbol field in the lowest |
| 27 | /// 16 bits of the implementation defined flags. |
| 28 | enum SymbolFlags { // See <mach-o/nlist.h>. |
| 29 | SF_DescFlagsMask = 0xFFFF, |
| 30 | |
| 31 | // Reference type flags. |
| 32 | SF_ReferenceTypeMask = 0x0007, |
| 33 | SF_ReferenceTypeUndefinedNonLazy = 0x0000, |
| 34 | SF_ReferenceTypeUndefinedLazy = 0x0001, |
| 35 | SF_ReferenceTypeDefined = 0x0002, |
| 36 | SF_ReferenceTypePrivateDefined = 0x0003, |
| 37 | SF_ReferenceTypePrivateUndefinedNonLazy = 0x0004, |
| 38 | SF_ReferenceTypePrivateUndefinedLazy = 0x0005, |
| 39 | |
| 40 | // Other 'desc' flags. |
| 41 | SF_NoDeadStrip = 0x0020, |
| 42 | SF_WeakReference = 0x0040, |
| 43 | SF_WeakDefinition = 0x0080 |
| 44 | }; |
| 45 | |
| 46 | private: |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 47 | MCAssembler Assembler; |
| 48 | |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 49 | MCCodeEmitter *Emitter; |
| 50 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 51 | MCSectionData *CurSectionData; |
| 52 | |
| 53 | DenseMap<const MCSection*, MCSectionData*> SectionMap; |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 54 | |
| 55 | DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap; |
| 56 | |
| 57 | private: |
| 58 | MCFragment *getCurrentFragment() const { |
| 59 | assert(CurSectionData && "No current section!"); |
| 60 | |
| 61 | if (!CurSectionData->empty()) |
| 62 | return &CurSectionData->getFragmentList().back(); |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
Daniel Dunbar | 0f5fa69 | 2009-08-28 05:48:54 +0000 | [diff] [blame] | 67 | MCSectionData &getSectionData(const MCSection &Section) { |
| 68 | MCSectionData *&Entry = SectionMap[&Section]; |
| 69 | |
| 70 | if (!Entry) |
| 71 | Entry = new MCSectionData(Section, &Assembler); |
| 72 | |
| 73 | return *Entry; |
| 74 | } |
| 75 | |
Daniel Dunbar | cb579b3 | 2009-08-31 08:08:06 +0000 | [diff] [blame] | 76 | MCSymbolData &getSymbolData(const MCSymbol &Symbol) { |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 77 | MCSymbolData *&Entry = SymbolMap[&Symbol]; |
| 78 | |
| 79 | if (!Entry) |
| 80 | Entry = new MCSymbolData(Symbol, 0, 0, &Assembler); |
| 81 | |
| 82 | return *Entry; |
| 83 | } |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 84 | |
| 85 | public: |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 86 | MCMachOStreamer(MCContext &Context, raw_ostream &_OS, MCCodeEmitter *_Emitter) |
Daniel Dunbar | a03a368 | 2009-08-31 08:07:55 +0000 | [diff] [blame] | 87 | : MCStreamer(Context), Assembler(Context, _OS), Emitter(_Emitter), |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 88 | CurSectionData(0) {} |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 89 | ~MCMachOStreamer() {} |
| 90 | |
Daniel Dunbar | a421de1 | 2009-08-26 13:57:37 +0000 | [diff] [blame] | 91 | const MCValue &AddValueSymbols(const MCValue &Value) { |
| 92 | if (Value.getSymA()) |
| 93 | getSymbolData(*const_cast<MCSymbol*>(Value.getSymA())); |
| 94 | if (Value.getSymB()) |
| 95 | getSymbolData(*const_cast<MCSymbol*>(Value.getSymB())); |
| 96 | return Value; |
| 97 | } |
| 98 | |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame^] | 99 | const MCExpr *AddValueSymbols(const MCExpr *Value) { |
| 100 | switch (Value->getKind()) { |
| 101 | case MCExpr::Constant: |
| 102 | break; |
| 103 | |
| 104 | case MCExpr::Binary: { |
| 105 | const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value); |
| 106 | AddValueSymbols(BE->getLHS()); |
| 107 | AddValueSymbols(BE->getRHS()); |
| 108 | break; |
| 109 | } |
| 110 | |
| 111 | case MCExpr::SymbolRef: |
| 112 | getSymbolData(cast<MCSymbolRefExpr>(Value)->getSymbol()); |
| 113 | break; |
| 114 | |
| 115 | case MCExpr::Unary: |
| 116 | AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr()); |
| 117 | break; |
| 118 | } |
| 119 | |
| 120 | return Value; |
| 121 | } |
| 122 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 123 | /// @name MCStreamer Interface |
| 124 | /// @{ |
| 125 | |
| 126 | virtual void SwitchSection(const MCSection *Section); |
| 127 | |
| 128 | virtual void EmitLabel(MCSymbol *Symbol); |
| 129 | |
| 130 | virtual void EmitAssemblerFlag(AssemblerFlag Flag); |
| 131 | |
| 132 | virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value, |
| 133 | bool MakeAbsolute = false); |
| 134 | |
| 135 | virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute); |
| 136 | |
| 137 | virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue); |
| 138 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 139 | virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 140 | unsigned ByteAlignment); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 141 | |
Daniel Dunbar | 8751b94 | 2009-08-28 05:48:22 +0000 | [diff] [blame] | 142 | virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 143 | unsigned Size = 0, unsigned ByteAlignment = 0); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 144 | |
| 145 | virtual void EmitBytes(const StringRef &Data); |
| 146 | |
| 147 | virtual void EmitValue(const MCValue &Value, unsigned Size); |
| 148 | |
| 149 | virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, |
| 150 | unsigned ValueSize = 1, |
| 151 | unsigned MaxBytesToEmit = 0); |
| 152 | |
| 153 | virtual void EmitValueToOffset(const MCValue &Offset, |
| 154 | unsigned char Value = 0); |
| 155 | |
| 156 | virtual void EmitInstruction(const MCInst &Inst); |
| 157 | |
| 158 | virtual void Finish(); |
| 159 | |
| 160 | /// @} |
| 161 | }; |
| 162 | |
| 163 | } // end anonymous namespace. |
| 164 | |
| 165 | void MCMachOStreamer::SwitchSection(const MCSection *Section) { |
| 166 | assert(Section && "Cannot switch to a null section!"); |
Chris Lattner | cda7f78 | 2009-08-22 19:35:08 +0000 | [diff] [blame] | 167 | |
| 168 | // If already in this section, then this is a noop. |
| 169 | if (Section == CurSection) return; |
Daniel Dunbar | 0f5fa69 | 2009-08-28 05:48:54 +0000 | [diff] [blame] | 170 | |
Chris Lattner | cda7f78 | 2009-08-22 19:35:08 +0000 | [diff] [blame] | 171 | CurSection = Section; |
Daniel Dunbar | 0f5fa69 | 2009-08-28 05:48:54 +0000 | [diff] [blame] | 172 | CurSectionData = &getSectionData(*Section); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) { |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 176 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 177 | |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 178 | // FIXME: We should also use offsets into Fill fragments. |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 179 | MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment()); |
| 180 | if (!F) |
| 181 | F = new MCDataFragment(CurSectionData); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 182 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 183 | MCSymbolData &SD = getSymbolData(*Symbol); |
| 184 | assert(!SD.getFragment() && "Unexpected fragment on symbol data!"); |
| 185 | SD.setFragment(F); |
| 186 | SD.setOffset(F->getContents().size()); |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 187 | |
| 188 | // This causes the reference type and weak reference flags to be cleared. |
| 189 | SD.setFlags(SD.getFlags() & ~(SF_WeakReference | SF_ReferenceTypeMask)); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 190 | |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 191 | Symbol->setSection(*CurSection); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | void MCMachOStreamer::EmitAssemblerFlag(AssemblerFlag Flag) { |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame] | 195 | switch (Flag) { |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame] | 196 | case SubsectionsViaSymbols: |
| 197 | Assembler.setSubsectionsViaSymbols(true); |
Daniel Dunbar | 8c3eaf4 | 2009-08-28 07:08:47 +0000 | [diff] [blame] | 198 | return; |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame] | 199 | } |
Daniel Dunbar | 8c3eaf4 | 2009-08-28 07:08:47 +0000 | [diff] [blame] | 200 | |
| 201 | assert(0 && "invalid assembler flag!"); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, |
| 205 | const MCValue &Value, |
| 206 | bool MakeAbsolute) { |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 207 | // Only absolute symbols can be redefined. |
| 208 | assert((Symbol->isUndefined() || Symbol->isAbsolute()) && |
| 209 | "Cannot define a symbol twice!"); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 210 | |
| 211 | llvm_unreachable("FIXME: Not yet implemented!"); |
| 212 | } |
| 213 | |
| 214 | void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol, |
| 215 | SymbolAttr Attribute) { |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 216 | // Indirect symbols are handled differently, to match how 'as' handles |
| 217 | // them. This makes writing matching .o files easier. |
| 218 | if (Attribute == MCStreamer::IndirectSymbol) { |
Daniel Dunbar | ad7c3d5 | 2009-08-26 00:18:21 +0000 | [diff] [blame] | 219 | // Note that we intentionally cannot use the symbol data here; this is |
| 220 | // important for matching the string table that 'as' generates. |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 221 | IndirectSymbolData ISD; |
| 222 | ISD.Symbol = Symbol; |
| 223 | ISD.SectionData = CurSectionData; |
| 224 | Assembler.getIndirectSymbols().push_back(ISD); |
| 225 | return; |
| 226 | } |
| 227 | |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 228 | // Adding a symbol attribute always introduces the symbol, note that an |
| 229 | // important side effect of calling getSymbolData here is to register the |
| 230 | // symbol with the assembler. |
| 231 | MCSymbolData &SD = getSymbolData(*Symbol); |
| 232 | |
| 233 | // The implementation of symbol attributes is designed to match 'as', but it |
| 234 | // leaves much to desired. It doesn't really make sense to arbitrarily add and |
| 235 | // remove flags, but 'as' allows this (in particular, see .desc). |
| 236 | // |
| 237 | // In the future it might be worth trying to make these operations more well |
| 238 | // defined. |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 239 | switch (Attribute) { |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 240 | case MCStreamer::IndirectSymbol: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 241 | case MCStreamer::Hidden: |
| 242 | case MCStreamer::Internal: |
| 243 | case MCStreamer::Protected: |
| 244 | case MCStreamer::Weak: |
| 245 | assert(0 && "Invalid symbol attribute for Mach-O!"); |
| 246 | break; |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 247 | |
| 248 | case MCStreamer::Global: |
Daniel Dunbar | 8f4d146 | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 249 | SD.setExternal(true); |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 250 | break; |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 251 | |
| 252 | case MCStreamer::LazyReference: |
| 253 | // FIXME: This requires -dynamic. |
| 254 | SD.setFlags(SD.getFlags() | SF_NoDeadStrip); |
| 255 | if (Symbol->isUndefined()) |
| 256 | SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy); |
| 257 | break; |
| 258 | |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 259 | // Since .reference sets the no dead strip bit, it is equivalent to |
| 260 | // .no_dead_strip in practice. |
| 261 | case MCStreamer::Reference: |
| 262 | case MCStreamer::NoDeadStrip: |
| 263 | SD.setFlags(SD.getFlags() | SF_NoDeadStrip); |
| 264 | break; |
| 265 | |
| 266 | case MCStreamer::PrivateExtern: |
| 267 | SD.setExternal(true); |
| 268 | SD.setPrivateExtern(true); |
| 269 | break; |
| 270 | |
| 271 | case MCStreamer::WeakReference: |
| 272 | // FIXME: This requires -dynamic. |
| 273 | if (Symbol->isUndefined()) |
| 274 | SD.setFlags(SD.getFlags() | SF_WeakReference); |
| 275 | break; |
| 276 | |
| 277 | case MCStreamer::WeakDefinition: |
| 278 | // FIXME: 'as' enforces that this is defined and global. The manual claims |
| 279 | // it has to be in a coalesced section, but this isn't enforced. |
| 280 | SD.setFlags(SD.getFlags() | SF_WeakDefinition); |
| 281 | break; |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 282 | } |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 286 | // Encode the 'desc' value into the lowest implementation defined bits. |
| 287 | assert(DescValue == (DescValue & SF_DescFlagsMask) && |
| 288 | "Invalid .desc value!"); |
| 289 | getSymbolData(*Symbol).setFlags(DescValue & SF_DescFlagsMask); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 292 | void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 293 | unsigned ByteAlignment) { |
Daniel Dunbar | 8f4d146 | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 294 | // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself. |
| 295 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
| 296 | |
| 297 | MCSymbolData &SD = getSymbolData(*Symbol); |
| 298 | SD.setExternal(true); |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 299 | SD.setCommon(Size, ByteAlignment); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Daniel Dunbar | 8751b94 | 2009-08-28 05:48:22 +0000 | [diff] [blame] | 302 | void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 303 | unsigned Size, unsigned ByteAlignment) { |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 304 | MCSectionData &SectData = getSectionData(*Section); |
| 305 | |
| 306 | // The symbol may not be present, which only creates the section. |
| 307 | if (!Symbol) |
| 308 | return; |
| 309 | |
| 310 | // FIXME: Assert that this section has the zerofill type. |
| 311 | |
| 312 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
| 313 | |
| 314 | MCSymbolData &SD = getSymbolData(*Symbol); |
| 315 | |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 316 | MCFragment *F = new MCZeroFillFragment(Size, ByteAlignment, &SectData); |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 317 | SD.setFragment(F); |
| 318 | |
| 319 | Symbol->setSection(*Section); |
| 320 | |
| 321 | // Update the maximum alignment on the zero fill section if necessary. |
| 322 | if (ByteAlignment > SectData.getAlignment()) |
| 323 | SectData.setAlignment(ByteAlignment); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | void MCMachOStreamer::EmitBytes(const StringRef &Data) { |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 327 | MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment()); |
| 328 | if (!DF) |
| 329 | DF = new MCDataFragment(CurSectionData); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 330 | DF->getContents().append(Data.begin(), Data.end()); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | void MCMachOStreamer::EmitValue(const MCValue &Value, unsigned Size) { |
Daniel Dunbar | a421de1 | 2009-08-26 13:57:37 +0000 | [diff] [blame] | 334 | new MCFillFragment(AddValueSymbols(Value), Size, 1, CurSectionData); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment, |
| 338 | int64_t Value, unsigned ValueSize, |
| 339 | unsigned MaxBytesToEmit) { |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 340 | if (MaxBytesToEmit == 0) |
| 341 | MaxBytesToEmit = ByteAlignment; |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 342 | new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit, |
| 343 | CurSectionData); |
| 344 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 345 | // Update the maximum alignment on the current section if necessary. |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 346 | if (ByteAlignment > CurSectionData->getAlignment()) |
| 347 | CurSectionData->setAlignment(ByteAlignment); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | void MCMachOStreamer::EmitValueToOffset(const MCValue &Offset, |
| 351 | unsigned char Value) { |
Daniel Dunbar | a421de1 | 2009-08-26 13:57:37 +0000 | [diff] [blame] | 352 | new MCOrgFragment(AddValueSymbols(Offset), Value, CurSectionData); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | void MCMachOStreamer::EmitInstruction(const MCInst &Inst) { |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 356 | // Scan for values. |
| 357 | for (unsigned i = 0; i != Inst.getNumOperands(); ++i) |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame^] | 358 | if (Inst.getOperand(i).isExpr()) |
| 359 | AddValueSymbols(Inst.getOperand(i).getExpr()); |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 360 | |
| 361 | if (!Emitter) |
| 362 | llvm_unreachable("no code emitter available!"); |
| 363 | |
| 364 | // FIXME: Relocations! |
| 365 | SmallString<256> Code; |
| 366 | raw_svector_ostream VecOS(Code); |
| 367 | Emitter->EncodeInstruction(Inst, VecOS); |
| 368 | EmitBytes(VecOS.str()); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | void MCMachOStreamer::Finish() { |
| 372 | Assembler.Finish(); |
| 373 | } |
| 374 | |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 375 | MCStreamer *llvm::createMachOStreamer(MCContext &Context, raw_ostream &OS, |
| 376 | MCCodeEmitter *CE) { |
| 377 | return new MCMachOStreamer(Context, OS, CE); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 378 | } |