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 | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCValue.h" |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | class MCMachOStreamer : public MCStreamer { |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 27 | /// SymbolFlags - We store the value for the 'desc' symbol field in the lowest |
| 28 | /// 16 bits of the implementation defined flags. |
| 29 | enum SymbolFlags { // See <mach-o/nlist.h>. |
| 30 | SF_DescFlagsMask = 0xFFFF, |
| 31 | |
| 32 | // Reference type flags. |
| 33 | SF_ReferenceTypeMask = 0x0007, |
| 34 | SF_ReferenceTypeUndefinedNonLazy = 0x0000, |
| 35 | SF_ReferenceTypeUndefinedLazy = 0x0001, |
| 36 | SF_ReferenceTypeDefined = 0x0002, |
| 37 | SF_ReferenceTypePrivateDefined = 0x0003, |
| 38 | SF_ReferenceTypePrivateUndefinedNonLazy = 0x0004, |
| 39 | SF_ReferenceTypePrivateUndefinedLazy = 0x0005, |
| 40 | |
| 41 | // Other 'desc' flags. |
| 42 | SF_NoDeadStrip = 0x0020, |
| 43 | SF_WeakReference = 0x0040, |
| 44 | SF_WeakDefinition = 0x0080 |
| 45 | }; |
| 46 | |
| 47 | private: |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 48 | MCAssembler Assembler; |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 49 | MCCodeEmitter *Emitter; |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 50 | MCSectionData *CurSectionData; |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 51 | DenseMap<const MCSection*, MCSectionData*> SectionMap; |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 52 | DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap; |
| 53 | |
| 54 | private: |
| 55 | MCFragment *getCurrentFragment() const { |
| 56 | assert(CurSectionData && "No current section!"); |
| 57 | |
| 58 | if (!CurSectionData->empty()) |
| 59 | return &CurSectionData->getFragmentList().back(); |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
Daniel Dunbar | 0f5fa69 | 2009-08-28 05:48:54 +0000 | [diff] [blame] | 64 | MCSectionData &getSectionData(const MCSection &Section) { |
| 65 | MCSectionData *&Entry = SectionMap[&Section]; |
| 66 | |
| 67 | if (!Entry) |
| 68 | Entry = new MCSectionData(Section, &Assembler); |
| 69 | |
| 70 | return *Entry; |
| 71 | } |
| 72 | |
Daniel Dunbar | cb579b3 | 2009-08-31 08:08:06 +0000 | [diff] [blame] | 73 | MCSymbolData &getSymbolData(const MCSymbol &Symbol) { |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 74 | MCSymbolData *&Entry = SymbolMap[&Symbol]; |
| 75 | |
| 76 | if (!Entry) |
| 77 | Entry = new MCSymbolData(Symbol, 0, 0, &Assembler); |
| 78 | |
| 79 | return *Entry; |
| 80 | } |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 81 | |
| 82 | public: |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 83 | MCMachOStreamer(MCContext &Context, raw_ostream &_OS, MCCodeEmitter *_Emitter) |
Daniel Dunbar | a03a368 | 2009-08-31 08:07:55 +0000 | [diff] [blame] | 84 | : MCStreamer(Context), Assembler(Context, _OS), Emitter(_Emitter), |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 85 | CurSectionData(0) {} |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 86 | ~MCMachOStreamer() {} |
| 87 | |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 88 | const MCExpr *AddValueSymbols(const MCExpr *Value) { |
| 89 | switch (Value->getKind()) { |
| 90 | case MCExpr::Constant: |
| 91 | break; |
| 92 | |
| 93 | case MCExpr::Binary: { |
| 94 | const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value); |
| 95 | AddValueSymbols(BE->getLHS()); |
| 96 | AddValueSymbols(BE->getRHS()); |
| 97 | break; |
| 98 | } |
| 99 | |
| 100 | case MCExpr::SymbolRef: |
| 101 | getSymbolData(cast<MCSymbolRefExpr>(Value)->getSymbol()); |
| 102 | break; |
| 103 | |
| 104 | case MCExpr::Unary: |
| 105 | AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr()); |
| 106 | break; |
| 107 | } |
| 108 | |
| 109 | return Value; |
| 110 | } |
| 111 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 112 | /// @name MCStreamer Interface |
| 113 | /// @{ |
| 114 | |
| 115 | virtual void SwitchSection(const MCSection *Section); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 116 | virtual void EmitLabel(MCSymbol *Symbol); |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 117 | virtual void EmitAssemblerFlag(MCAssemblerFlag Flag); |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 118 | virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value); |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 119 | virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 120 | virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue); |
Chris Lattner | 9eb158d | 2010-01-23 07:47:02 +0000 | [diff] [blame] | 121 | virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 122 | unsigned ByteAlignment); |
Chris Lattner | 99328ad | 2010-01-25 07:52:13 +0000 | [diff] [blame] | 123 | virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) { |
| 124 | assert(0 && "macho doesn't support this directive"); |
| 125 | } |
Chris Lattner | 9eb158d | 2010-01-23 07:47:02 +0000 | [diff] [blame] | 126 | virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) { |
| 127 | assert(0 && "macho doesn't support this directive"); |
| 128 | } |
Daniel Dunbar | 8751b94 | 2009-08-28 05:48:22 +0000 | [diff] [blame] | 129 | virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 130 | unsigned Size = 0, unsigned ByteAlignment = 0); |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 131 | virtual void EmitBytes(StringRef Data, unsigned AddrSpace); |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 132 | virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace); |
Chris Lattner | 718fb59 | 2010-01-25 21:28:50 +0000 | [diff] [blame] | 133 | virtual void EmitGPRel32Value(const MCExpr *Value) { |
| 134 | assert(0 && "macho doesn't support this directive"); |
| 135 | } |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 136 | virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, |
| 137 | unsigned ValueSize = 1, |
| 138 | unsigned MaxBytesToEmit = 0); |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 139 | virtual void EmitValueToOffset(const MCExpr *Offset, |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 140 | unsigned char Value = 0); |
Chris Lattner | a6594fc | 2010-01-25 18:58:59 +0000 | [diff] [blame] | 141 | |
| 142 | virtual void EmitFileDirective(StringRef Filename) { |
| 143 | errs() << "FIXME: MCMachoStreamer:EmitFileDirective not implemented\n"; |
| 144 | } |
| 145 | virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) { |
| 146 | errs() << "FIXME: MCMachoStreamer:EmitDwarfFileDirective not implemented\n"; |
| 147 | } |
| 148 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 149 | virtual void EmitInstruction(const MCInst &Inst); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 150 | virtual void Finish(); |
| 151 | |
| 152 | /// @} |
| 153 | }; |
| 154 | |
| 155 | } // end anonymous namespace. |
| 156 | |
| 157 | void MCMachOStreamer::SwitchSection(const MCSection *Section) { |
| 158 | assert(Section && "Cannot switch to a null section!"); |
Chris Lattner | cda7f78 | 2009-08-22 19:35:08 +0000 | [diff] [blame] | 159 | |
| 160 | // If already in this section, then this is a noop. |
| 161 | if (Section == CurSection) return; |
Daniel Dunbar | 0f5fa69 | 2009-08-28 05:48:54 +0000 | [diff] [blame] | 162 | |
Chris Lattner | cda7f78 | 2009-08-22 19:35:08 +0000 | [diff] [blame] | 163 | CurSection = Section; |
Daniel Dunbar | 0f5fa69 | 2009-08-28 05:48:54 +0000 | [diff] [blame] | 164 | CurSectionData = &getSectionData(*Section); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) { |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 168 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 169 | |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 170 | // FIXME: We should also use offsets into Fill fragments. |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 171 | MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment()); |
| 172 | if (!F) |
| 173 | F = new MCDataFragment(CurSectionData); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 174 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 175 | MCSymbolData &SD = getSymbolData(*Symbol); |
| 176 | assert(!SD.getFragment() && "Unexpected fragment on symbol data!"); |
| 177 | SD.setFragment(F); |
| 178 | SD.setOffset(F->getContents().size()); |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 179 | |
| 180 | // This causes the reference type and weak reference flags to be cleared. |
| 181 | SD.setFlags(SD.getFlags() & ~(SF_WeakReference | SF_ReferenceTypeMask)); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 182 | |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 183 | Symbol->setSection(*CurSection); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 186 | void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) { |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame] | 187 | switch (Flag) { |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 188 | case MCAF_SubsectionsViaSymbols: |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame] | 189 | Assembler.setSubsectionsViaSymbols(true); |
Daniel Dunbar | 8c3eaf4 | 2009-08-28 07:08:47 +0000 | [diff] [blame] | 190 | return; |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame] | 191 | } |
Daniel Dunbar | 8c3eaf4 | 2009-08-28 07:08:47 +0000 | [diff] [blame] | 192 | |
| 193 | assert(0 && "invalid assembler flag!"); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 196 | void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) { |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 197 | // Only absolute symbols can be redefined. |
| 198 | assert((Symbol->isUndefined() || Symbol->isAbsolute()) && |
| 199 | "Cannot define a symbol twice!"); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 200 | |
Daniel Dunbar | a4d8667 | 2009-10-16 01:58:23 +0000 | [diff] [blame] | 201 | // FIXME: Lift context changes into super class. |
| 202 | // FIXME: Set associated section. |
| 203 | Symbol->setValue(Value); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol, |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 207 | MCSymbolAttr Attribute) { |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 208 | // Indirect symbols are handled differently, to match how 'as' handles |
| 209 | // them. This makes writing matching .o files easier. |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 210 | if (Attribute == MCSA_IndirectSymbol) { |
Daniel Dunbar | ad7c3d5 | 2009-08-26 00:18:21 +0000 | [diff] [blame] | 211 | // Note that we intentionally cannot use the symbol data here; this is |
| 212 | // important for matching the string table that 'as' generates. |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 213 | IndirectSymbolData ISD; |
| 214 | ISD.Symbol = Symbol; |
| 215 | ISD.SectionData = CurSectionData; |
| 216 | Assembler.getIndirectSymbols().push_back(ISD); |
| 217 | return; |
| 218 | } |
| 219 | |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 220 | // Adding a symbol attribute always introduces the symbol, note that an |
| 221 | // important side effect of calling getSymbolData here is to register the |
| 222 | // symbol with the assembler. |
| 223 | MCSymbolData &SD = getSymbolData(*Symbol); |
| 224 | |
| 225 | // The implementation of symbol attributes is designed to match 'as', but it |
| 226 | // leaves much to desired. It doesn't really make sense to arbitrarily add and |
| 227 | // remove flags, but 'as' allows this (in particular, see .desc). |
| 228 | // |
| 229 | // In the future it might be worth trying to make these operations more well |
| 230 | // defined. |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 231 | switch (Attribute) { |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 232 | case MCSA_Invalid: |
Chris Lattner | ed0ab15 | 2010-01-25 18:30:45 +0000 | [diff] [blame] | 233 | case MCSA_ELF_TypeFunction: |
| 234 | case MCSA_ELF_TypeIndFunction: |
| 235 | case MCSA_ELF_TypeObject: |
| 236 | case MCSA_ELF_TypeTLS: |
| 237 | case MCSA_ELF_TypeCommon: |
| 238 | case MCSA_ELF_TypeNoType: |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 239 | case MCSA_IndirectSymbol: |
| 240 | case MCSA_Hidden: |
| 241 | case MCSA_Internal: |
| 242 | case MCSA_Protected: |
| 243 | case MCSA_Weak: |
| 244 | case MCSA_Local: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 245 | assert(0 && "Invalid symbol attribute for Mach-O!"); |
| 246 | break; |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 247 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 248 | case MCSA_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 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 252 | case MCSA_LazyReference: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 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. |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 261 | case MCSA_Reference: |
| 262 | case MCSA_NoDeadStrip: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 263 | SD.setFlags(SD.getFlags() | SF_NoDeadStrip); |
| 264 | break; |
| 265 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 266 | case MCSA_PrivateExtern: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 267 | SD.setExternal(true); |
| 268 | SD.setPrivateExtern(true); |
| 269 | break; |
| 270 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 271 | case MCSA_WeakReference: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 272 | // FIXME: This requires -dynamic. |
| 273 | if (Symbol->isUndefined()) |
| 274 | SD.setFlags(SD.getFlags() | SF_WeakReference); |
| 275 | break; |
| 276 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 277 | case MCSA_WeakDefinition: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 9eb158d | 2010-01-23 07:47:02 +0000 | [diff] [blame] | 292 | void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t 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 | |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 326 | void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) { |
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 | |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 333 | void MCMachOStreamer::EmitValue(const MCExpr *Value, unsigned Size, |
| 334 | unsigned AddrSpace) { |
Daniel Dunbar | 1253a6f | 2009-10-16 01:58:03 +0000 | [diff] [blame] | 335 | new MCFillFragment(*AddValueSymbols(Value), Size, 1, CurSectionData); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment, |
| 339 | int64_t Value, unsigned ValueSize, |
| 340 | unsigned MaxBytesToEmit) { |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 341 | if (MaxBytesToEmit == 0) |
| 342 | MaxBytesToEmit = ByteAlignment; |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 343 | new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit, |
| 344 | CurSectionData); |
| 345 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 346 | // Update the maximum alignment on the current section if necessary. |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 347 | if (ByteAlignment > CurSectionData->getAlignment()) |
| 348 | CurSectionData->setAlignment(ByteAlignment); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 351 | void MCMachOStreamer::EmitValueToOffset(const MCExpr *Offset, |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 352 | unsigned char Value) { |
Daniel Dunbar | 1253a6f | 2009-10-16 01:58:03 +0000 | [diff] [blame] | 353 | new MCOrgFragment(*Offset, Value, CurSectionData); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | void MCMachOStreamer::EmitInstruction(const MCInst &Inst) { |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 357 | // Scan for values. |
| 358 | for (unsigned i = 0; i != Inst.getNumOperands(); ++i) |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 359 | if (Inst.getOperand(i).isExpr()) |
| 360 | AddValueSymbols(Inst.getOperand(i).getExpr()); |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 361 | |
| 362 | if (!Emitter) |
| 363 | llvm_unreachable("no code emitter available!"); |
| 364 | |
Daniel Dunbar | e1ec617 | 2010-02-02 21:44:01 +0000 | [diff] [blame^] | 365 | CurSectionData->setHasInstructions(true); |
| 366 | |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 367 | // FIXME: Relocations! |
| 368 | SmallString<256> Code; |
| 369 | raw_svector_ostream VecOS(Code); |
| 370 | Emitter->EncodeInstruction(Inst, VecOS); |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 371 | EmitBytes(VecOS.str(), 0); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | void MCMachOStreamer::Finish() { |
| 375 | Assembler.Finish(); |
| 376 | } |
| 377 | |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 378 | MCStreamer *llvm::createMachOStreamer(MCContext &Context, raw_ostream &OS, |
| 379 | MCCodeEmitter *CE) { |
| 380 | return new MCMachOStreamer(Context, OS, CE); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 381 | } |