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 | d8036fb | 2010-03-23 05:09:03 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetAsmBackend.h" |
| 22 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | class MCMachOStreamer : public MCStreamer { |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 28 | /// SymbolFlags - We store the value for the 'desc' symbol field in the lowest |
| 29 | /// 16 bits of the implementation defined flags. |
| 30 | enum SymbolFlags { // See <mach-o/nlist.h>. |
| 31 | SF_DescFlagsMask = 0xFFFF, |
| 32 | |
| 33 | // Reference type flags. |
| 34 | SF_ReferenceTypeMask = 0x0007, |
| 35 | SF_ReferenceTypeUndefinedNonLazy = 0x0000, |
| 36 | SF_ReferenceTypeUndefinedLazy = 0x0001, |
| 37 | SF_ReferenceTypeDefined = 0x0002, |
| 38 | SF_ReferenceTypePrivateDefined = 0x0003, |
| 39 | SF_ReferenceTypePrivateUndefinedNonLazy = 0x0004, |
| 40 | SF_ReferenceTypePrivateUndefinedLazy = 0x0005, |
| 41 | |
| 42 | // Other 'desc' flags. |
| 43 | SF_NoDeadStrip = 0x0020, |
| 44 | SF_WeakReference = 0x0040, |
| 45 | SF_WeakDefinition = 0x0080 |
| 46 | }; |
| 47 | |
| 48 | private: |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 49 | MCAssembler Assembler; |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 50 | MCSectionData *CurSectionData; |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 51 | |
| 52 | private: |
| 53 | MCFragment *getCurrentFragment() const { |
| 54 | assert(CurSectionData && "No current section!"); |
| 55 | |
| 56 | if (!CurSectionData->empty()) |
| 57 | return &CurSectionData->getFragmentList().back(); |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
Daniel Dunbar | f70f477 | 2010-03-22 20:35:46 +0000 | [diff] [blame] | 62 | /// Get a data fragment to write into, creating a new one if the current |
| 63 | /// fragment is not a data fragment. |
| 64 | MCDataFragment *getOrCreateDataFragment() const { |
| 65 | MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment()); |
| 66 | if (!F) |
| 67 | F = new MCDataFragment(CurSectionData); |
| 68 | return F; |
| 69 | } |
| 70 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 71 | public: |
Daniel Dunbar | 1f3e445 | 2010-03-11 01:34:27 +0000 | [diff] [blame] | 72 | MCMachOStreamer(MCContext &Context, TargetAsmBackend &TAB, |
| 73 | raw_ostream &_OS, MCCodeEmitter *_Emitter) |
Daniel Dunbar | cf871e5 | 2010-03-19 10:43:18 +0000 | [diff] [blame] | 74 | : MCStreamer(Context), Assembler(Context, TAB, *_Emitter, _OS), |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 75 | CurSectionData(0) {} |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 76 | ~MCMachOStreamer() {} |
| 77 | |
Daniel Dunbar | ac2884a | 2010-03-25 22:49:09 +0000 | [diff] [blame] | 78 | MCAssembler &getAssembler() { return Assembler; } |
| 79 | |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 80 | const MCExpr *AddValueSymbols(const MCExpr *Value) { |
| 81 | switch (Value->getKind()) { |
Chris Lattner | 5d917a8 | 2010-02-08 19:41:07 +0000 | [diff] [blame] | 82 | case MCExpr::Target: assert(0 && "Can't handle target exprs yet!"); |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 83 | case MCExpr::Constant: |
| 84 | break; |
| 85 | |
| 86 | case MCExpr::Binary: { |
| 87 | const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value); |
| 88 | AddValueSymbols(BE->getLHS()); |
| 89 | AddValueSymbols(BE->getRHS()); |
| 90 | break; |
| 91 | } |
| 92 | |
| 93 | case MCExpr::SymbolRef: |
Daniel Dunbar | 46836a7 | 2010-03-10 20:58:29 +0000 | [diff] [blame] | 94 | Assembler.getOrCreateSymbolData( |
| 95 | cast<MCSymbolRefExpr>(Value)->getSymbol()); |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 96 | break; |
| 97 | |
| 98 | case MCExpr::Unary: |
| 99 | AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr()); |
| 100 | break; |
| 101 | } |
| 102 | |
| 103 | return Value; |
| 104 | } |
| 105 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 106 | /// @name MCStreamer Interface |
| 107 | /// @{ |
| 108 | |
| 109 | virtual void SwitchSection(const MCSection *Section); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 110 | virtual void EmitLabel(MCSymbol *Symbol); |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 111 | virtual void EmitAssemblerFlag(MCAssemblerFlag Flag); |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 112 | virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value); |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 113 | virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 114 | virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue); |
Chris Lattner | 9eb158d | 2010-01-23 07:47:02 +0000 | [diff] [blame] | 115 | virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 116 | unsigned ByteAlignment); |
Chris Lattner | 99328ad | 2010-01-25 07:52:13 +0000 | [diff] [blame] | 117 | virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) { |
| 118 | assert(0 && "macho doesn't support this directive"); |
| 119 | } |
Chris Lattner | 9eb158d | 2010-01-23 07:47:02 +0000 | [diff] [blame] | 120 | virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) { |
| 121 | assert(0 && "macho doesn't support this directive"); |
| 122 | } |
Daniel Dunbar | 8751b94 | 2009-08-28 05:48:22 +0000 | [diff] [blame] | 123 | virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 124 | unsigned Size = 0, unsigned ByteAlignment = 0); |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 125 | virtual void EmitBytes(StringRef Data, unsigned AddrSpace); |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 126 | virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace); |
Chris Lattner | 718fb59 | 2010-01-25 21:28:50 +0000 | [diff] [blame] | 127 | virtual void EmitGPRel32Value(const MCExpr *Value) { |
| 128 | assert(0 && "macho doesn't support this directive"); |
| 129 | } |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 130 | virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, |
| 131 | unsigned ValueSize = 1, |
| 132 | unsigned MaxBytesToEmit = 0); |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 133 | virtual void EmitCodeAlignment(unsigned ByteAlignment, |
| 134 | unsigned MaxBytesToEmit = 0); |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 135 | virtual void EmitValueToOffset(const MCExpr *Offset, |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 136 | unsigned char Value = 0); |
Chris Lattner | a6594fc | 2010-01-25 18:58:59 +0000 | [diff] [blame] | 137 | |
| 138 | virtual void EmitFileDirective(StringRef Filename) { |
| 139 | errs() << "FIXME: MCMachoStreamer:EmitFileDirective not implemented\n"; |
| 140 | } |
| 141 | virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) { |
| 142 | errs() << "FIXME: MCMachoStreamer:EmitDwarfFileDirective not implemented\n"; |
| 143 | } |
| 144 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 145 | virtual void EmitInstruction(const MCInst &Inst); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 146 | virtual void Finish(); |
| 147 | |
| 148 | /// @} |
| 149 | }; |
| 150 | |
| 151 | } // end anonymous namespace. |
| 152 | |
| 153 | void MCMachOStreamer::SwitchSection(const MCSection *Section) { |
| 154 | assert(Section && "Cannot switch to a null section!"); |
Chris Lattner | cda7f78 | 2009-08-22 19:35:08 +0000 | [diff] [blame] | 155 | |
| 156 | // If already in this section, then this is a noop. |
| 157 | if (Section == CurSection) return; |
Daniel Dunbar | 0f5fa69 | 2009-08-28 05:48:54 +0000 | [diff] [blame] | 158 | |
Chris Lattner | cda7f78 | 2009-08-22 19:35:08 +0000 | [diff] [blame] | 159 | CurSection = Section; |
Daniel Dunbar | 46836a7 | 2010-03-10 20:58:29 +0000 | [diff] [blame] | 160 | CurSectionData = &Assembler.getOrCreateSectionData(*Section); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) { |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 164 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
Daniel Dunbar | c304718 | 2010-05-05 19:01:00 +0000 | [diff] [blame^] | 165 | assert(!Symbol->isVariable() && "Cannot emit a variable symbol!"); |
| 166 | assert(CurSection && "Cannot emit before setting section!"); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 167 | |
Daniel Dunbar | f70f477 | 2010-03-22 20:35:46 +0000 | [diff] [blame] | 168 | // FIXME: This is wasteful, we don't necessarily need to create a data |
| 169 | // fragment. Instead, we should mark the symbol as pointing into the data |
| 170 | // fragment if it exists, otherwise we should just queue the label and set its |
| 171 | // fragment pointer when we emit the next fragment. |
| 172 | MCDataFragment *F = getOrCreateDataFragment(); |
Daniel Dunbar | 46836a7 | 2010-03-10 20:58:29 +0000 | [diff] [blame] | 173 | MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 174 | assert(!SD.getFragment() && "Unexpected fragment on symbol data!"); |
| 175 | SD.setFragment(F); |
| 176 | SD.setOffset(F->getContents().size()); |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 177 | |
| 178 | // This causes the reference type and weak reference flags to be cleared. |
| 179 | SD.setFlags(SD.getFlags() & ~(SF_WeakReference | SF_ReferenceTypeMask)); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 180 | |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 181 | Symbol->setSection(*CurSection); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 184 | void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) { |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame] | 185 | switch (Flag) { |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 186 | case MCAF_SubsectionsViaSymbols: |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame] | 187 | Assembler.setSubsectionsViaSymbols(true); |
Daniel Dunbar | 8c3eaf4 | 2009-08-28 07:08:47 +0000 | [diff] [blame] | 188 | return; |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame] | 189 | } |
Daniel Dunbar | 8c3eaf4 | 2009-08-28 07:08:47 +0000 | [diff] [blame] | 190 | |
| 191 | assert(0 && "invalid assembler flag!"); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 194 | void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) { |
Daniel Dunbar | a4d8667 | 2009-10-16 01:58:23 +0000 | [diff] [blame] | 195 | // FIXME: Lift context changes into super class. |
| 196 | // FIXME: Set associated section. |
Daniel Dunbar | 08a408a | 2010-05-05 17:41:00 +0000 | [diff] [blame] | 197 | Symbol->setVariableValue(AddValueSymbols(Value)); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol, |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 201 | MCSymbolAttr Attribute) { |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 202 | // Indirect symbols are handled differently, to match how 'as' handles |
| 203 | // them. This makes writing matching .o files easier. |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 204 | if (Attribute == MCSA_IndirectSymbol) { |
Daniel Dunbar | ad7c3d5 | 2009-08-26 00:18:21 +0000 | [diff] [blame] | 205 | // Note that we intentionally cannot use the symbol data here; this is |
| 206 | // important for matching the string table that 'as' generates. |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 207 | IndirectSymbolData ISD; |
| 208 | ISD.Symbol = Symbol; |
| 209 | ISD.SectionData = CurSectionData; |
| 210 | Assembler.getIndirectSymbols().push_back(ISD); |
| 211 | return; |
| 212 | } |
| 213 | |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 214 | // Adding a symbol attribute always introduces the symbol, note that an |
Daniel Dunbar | 46836a7 | 2010-03-10 20:58:29 +0000 | [diff] [blame] | 215 | // important side effect of calling getOrCreateSymbolData here is to register |
| 216 | // the symbol with the assembler. |
| 217 | MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol); |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 218 | |
| 219 | // The implementation of symbol attributes is designed to match 'as', but it |
| 220 | // leaves much to desired. It doesn't really make sense to arbitrarily add and |
| 221 | // remove flags, but 'as' allows this (in particular, see .desc). |
| 222 | // |
| 223 | // In the future it might be worth trying to make these operations more well |
| 224 | // defined. |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 225 | switch (Attribute) { |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 226 | case MCSA_Invalid: |
Chris Lattner | ed0ab15 | 2010-01-25 18:30:45 +0000 | [diff] [blame] | 227 | case MCSA_ELF_TypeFunction: |
| 228 | case MCSA_ELF_TypeIndFunction: |
| 229 | case MCSA_ELF_TypeObject: |
| 230 | case MCSA_ELF_TypeTLS: |
| 231 | case MCSA_ELF_TypeCommon: |
| 232 | case MCSA_ELF_TypeNoType: |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 233 | case MCSA_IndirectSymbol: |
| 234 | case MCSA_Hidden: |
| 235 | case MCSA_Internal: |
| 236 | case MCSA_Protected: |
| 237 | case MCSA_Weak: |
| 238 | case MCSA_Local: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 239 | assert(0 && "Invalid symbol attribute for Mach-O!"); |
| 240 | break; |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 241 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 242 | case MCSA_Global: |
Daniel Dunbar | 8f4d146 | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 243 | SD.setExternal(true); |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 244 | break; |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 245 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 246 | case MCSA_LazyReference: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 247 | // FIXME: This requires -dynamic. |
| 248 | SD.setFlags(SD.getFlags() | SF_NoDeadStrip); |
| 249 | if (Symbol->isUndefined()) |
| 250 | SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy); |
| 251 | break; |
| 252 | |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 253 | // Since .reference sets the no dead strip bit, it is equivalent to |
| 254 | // .no_dead_strip in practice. |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 255 | case MCSA_Reference: |
| 256 | case MCSA_NoDeadStrip: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 257 | SD.setFlags(SD.getFlags() | SF_NoDeadStrip); |
| 258 | break; |
| 259 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 260 | case MCSA_PrivateExtern: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 261 | SD.setExternal(true); |
| 262 | SD.setPrivateExtern(true); |
| 263 | break; |
| 264 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 265 | case MCSA_WeakReference: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 266 | // FIXME: This requires -dynamic. |
| 267 | if (Symbol->isUndefined()) |
| 268 | SD.setFlags(SD.getFlags() | SF_WeakReference); |
| 269 | break; |
| 270 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 271 | case MCSA_WeakDefinition: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 272 | // FIXME: 'as' enforces that this is defined and global. The manual claims |
| 273 | // it has to be in a coalesced section, but this isn't enforced. |
| 274 | SD.setFlags(SD.getFlags() | SF_WeakDefinition); |
| 275 | break; |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 276 | } |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 280 | // Encode the 'desc' value into the lowest implementation defined bits. |
| 281 | assert(DescValue == (DescValue & SF_DescFlagsMask) && |
| 282 | "Invalid .desc value!"); |
Daniel Dunbar | 46836a7 | 2010-03-10 20:58:29 +0000 | [diff] [blame] | 283 | Assembler.getOrCreateSymbolData(*Symbol).setFlags(DescValue&SF_DescFlagsMask); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Chris Lattner | 9eb158d | 2010-01-23 07:47:02 +0000 | [diff] [blame] | 286 | void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 287 | unsigned ByteAlignment) { |
Daniel Dunbar | 8f4d146 | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 288 | // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself. |
| 289 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
| 290 | |
Daniel Dunbar | 46836a7 | 2010-03-10 20:58:29 +0000 | [diff] [blame] | 291 | MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol); |
Daniel Dunbar | 8f4d146 | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 292 | SD.setExternal(true); |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 293 | SD.setCommon(Size, ByteAlignment); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Daniel Dunbar | 8751b94 | 2009-08-28 05:48:22 +0000 | [diff] [blame] | 296 | void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 297 | unsigned Size, unsigned ByteAlignment) { |
Daniel Dunbar | 46836a7 | 2010-03-10 20:58:29 +0000 | [diff] [blame] | 298 | MCSectionData &SectData = Assembler.getOrCreateSectionData(*Section); |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 299 | |
| 300 | // The symbol may not be present, which only creates the section. |
| 301 | if (!Symbol) |
| 302 | return; |
| 303 | |
| 304 | // FIXME: Assert that this section has the zerofill type. |
| 305 | |
| 306 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
| 307 | |
Daniel Dunbar | 46836a7 | 2010-03-10 20:58:29 +0000 | [diff] [blame] | 308 | MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol); |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 309 | |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 310 | MCFragment *F = new MCZeroFillFragment(Size, ByteAlignment, &SectData); |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 311 | SD.setFragment(F); |
| 312 | |
| 313 | Symbol->setSection(*Section); |
| 314 | |
| 315 | // Update the maximum alignment on the zero fill section if necessary. |
| 316 | if (ByteAlignment > SectData.getAlignment()) |
| 317 | SectData.setAlignment(ByteAlignment); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 320 | void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) { |
Daniel Dunbar | f70f477 | 2010-03-22 20:35:46 +0000 | [diff] [blame] | 321 | getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end()); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 324 | void MCMachOStreamer::EmitValue(const MCExpr *Value, unsigned Size, |
| 325 | unsigned AddrSpace) { |
Daniel Dunbar | f70f477 | 2010-03-22 20:35:46 +0000 | [diff] [blame] | 326 | MCDataFragment *DF = getOrCreateDataFragment(); |
Daniel Dunbar | 45f4874 | 2010-02-13 09:28:22 +0000 | [diff] [blame] | 327 | |
| 328 | // Avoid fixups when possible. |
| 329 | int64_t AbsValue; |
Daniel Dunbar | 40ebe247 | 2010-02-22 22:08:57 +0000 | [diff] [blame] | 330 | if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) { |
Daniel Dunbar | 45f4874 | 2010-02-13 09:28:22 +0000 | [diff] [blame] | 331 | // FIXME: Endianness assumption. |
| 332 | for (unsigned i = 0; i != Size; ++i) |
| 333 | DF->getContents().push_back(uint8_t(AbsValue >> (i * 8))); |
| 334 | } else { |
Daniel Dunbar | 8315a35 | 2010-03-12 21:00:38 +0000 | [diff] [blame] | 335 | DF->addFixup(MCAsmFixup(DF->getContents().size(), *AddValueSymbols(Value), |
| 336 | MCFixup::getKindForSize(Size))); |
Daniel Dunbar | 45f4874 | 2010-02-13 09:28:22 +0000 | [diff] [blame] | 337 | DF->getContents().resize(DF->getContents().size() + Size, 0); |
| 338 | } |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment, |
| 342 | int64_t Value, unsigned ValueSize, |
| 343 | unsigned MaxBytesToEmit) { |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 344 | if (MaxBytesToEmit == 0) |
| 345 | MaxBytesToEmit = ByteAlignment; |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 346 | new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit, |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 347 | false /* EmitNops */, CurSectionData); |
| 348 | |
| 349 | // Update the maximum alignment on the current section if necessary. |
| 350 | if (ByteAlignment > CurSectionData->getAlignment()) |
| 351 | CurSectionData->setAlignment(ByteAlignment); |
| 352 | } |
| 353 | |
| 354 | void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment, |
| 355 | unsigned MaxBytesToEmit) { |
| 356 | if (MaxBytesToEmit == 0) |
| 357 | MaxBytesToEmit = ByteAlignment; |
Daniel Dunbar | 8f9b80e | 2010-03-23 02:36:58 +0000 | [diff] [blame] | 358 | new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit, |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 359 | true /* EmitNops */, CurSectionData); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 360 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 361 | // Update the maximum alignment on the current section if necessary. |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 362 | if (ByteAlignment > CurSectionData->getAlignment()) |
| 363 | CurSectionData->setAlignment(ByteAlignment); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 366 | void MCMachOStreamer::EmitValueToOffset(const MCExpr *Offset, |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 367 | unsigned char Value) { |
Daniel Dunbar | 1253a6f | 2009-10-16 01:58:03 +0000 | [diff] [blame] | 368 | new MCOrgFragment(*Offset, Value, CurSectionData); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | void MCMachOStreamer::EmitInstruction(const MCInst &Inst) { |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 372 | // Scan for values. |
| 373 | for (unsigned i = 0; i != Inst.getNumOperands(); ++i) |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 374 | if (Inst.getOperand(i).isExpr()) |
| 375 | AddValueSymbols(Inst.getOperand(i).getExpr()); |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 376 | |
Daniel Dunbar | e1ec617 | 2010-02-02 21:44:01 +0000 | [diff] [blame] | 377 | CurSectionData->setHasInstructions(true); |
| 378 | |
Daniel Dunbar | 3f4dcd9 | 2010-03-22 23:16:48 +0000 | [diff] [blame] | 379 | // FIXME-PERF: Common case is that we don't need to relax, encode directly |
| 380 | // onto the data fragments buffers. |
| 381 | |
Daniel Dunbar | 73c5574 | 2010-02-09 22:59:55 +0000 | [diff] [blame] | 382 | SmallVector<MCFixup, 4> Fixups; |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 383 | SmallString<256> Code; |
| 384 | raw_svector_ostream VecOS(Code); |
Daniel Dunbar | cf871e5 | 2010-03-19 10:43:18 +0000 | [diff] [blame] | 385 | Assembler.getEmitter().EncodeInstruction(Inst, VecOS, Fixups); |
Daniel Dunbar | f634676 | 2010-02-13 09:29:02 +0000 | [diff] [blame] | 386 | VecOS.flush(); |
| 387 | |
Daniel Dunbar | 337055e | 2010-03-23 03:13:05 +0000 | [diff] [blame] | 388 | // FIXME: Eliminate this copy. |
| 389 | SmallVector<MCAsmFixup, 4> AsmFixups; |
Daniel Dunbar | f634676 | 2010-02-13 09:29:02 +0000 | [diff] [blame] | 390 | for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { |
| 391 | MCFixup &F = Fixups[i]; |
Daniel Dunbar | 337055e | 2010-03-23 03:13:05 +0000 | [diff] [blame] | 392 | AsmFixups.push_back(MCAsmFixup(F.getOffset(), *F.getValue(), |
| 393 | F.getKind())); |
| 394 | } |
| 395 | |
Daniel Dunbar | d8036fb | 2010-03-23 05:09:03 +0000 | [diff] [blame] | 396 | // See if we might need to relax this instruction, if so it needs its own |
| 397 | // fragment. |
| 398 | // |
| 399 | // FIXME-PERF: Support target hook to do a fast path that avoids the encoder, |
| 400 | // when we can immediately tell that we will get something which might need |
| 401 | // relaxation (and compute its size). |
| 402 | // |
| 403 | // FIXME-PERF: We should also be smart about immediately relaxing instructions |
| 404 | // which we can already show will never possibly fit (we can also do a very |
| 405 | // good job of this before we do the first relaxation pass, because we have |
| 406 | // total knowledge about undefined symbols at that point). Even now, though, |
| 407 | // we can do a decent job, especially on Darwin where scattering means that we |
| 408 | // are going to often know that we can never fully resolve a fixup. |
| 409 | if (Assembler.getBackend().MayNeedRelaxation(Inst, AsmFixups)) { |
| 410 | MCInstFragment *IF = new MCInstFragment(Inst, CurSectionData); |
| 411 | |
| 412 | // Add the fixups and data. |
| 413 | // |
| 414 | // FIXME: Revisit this design decision when relaxation is done, we may be |
| 415 | // able to get away with not storing any extra data in the MCInst. |
| 416 | IF->getCode() = Code; |
| 417 | IF->getFixups() = AsmFixups; |
| 418 | |
| 419 | return; |
| 420 | } |
| 421 | |
Daniel Dunbar | 337055e | 2010-03-23 03:13:05 +0000 | [diff] [blame] | 422 | // Add the fixups and data. |
| 423 | MCDataFragment *DF = getOrCreateDataFragment(); |
| 424 | for (unsigned i = 0, e = AsmFixups.size(); i != e; ++i) { |
| 425 | AsmFixups[i].Offset += DF->getContents().size(); |
| 426 | DF->addFixup(AsmFixups[i]); |
Daniel Dunbar | f634676 | 2010-02-13 09:29:02 +0000 | [diff] [blame] | 427 | } |
| 428 | DF->getContents().append(Code.begin(), Code.end()); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | void MCMachOStreamer::Finish() { |
| 432 | Assembler.Finish(); |
| 433 | } |
| 434 | |
Daniel Dunbar | 1f3e445 | 2010-03-11 01:34:27 +0000 | [diff] [blame] | 435 | MCStreamer *llvm::createMachOStreamer(MCContext &Context, TargetAsmBackend &TAB, |
Daniel Dunbar | ac2884a | 2010-03-25 22:49:09 +0000 | [diff] [blame] | 436 | raw_ostream &OS, MCCodeEmitter *CE, |
| 437 | bool RelaxAll) { |
| 438 | MCMachOStreamer *S = new MCMachOStreamer(Context, TAB, OS, CE); |
| 439 | if (RelaxAll) |
| 440 | S->getAssembler().setRelaxAll(true); |
| 441 | return S; |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 442 | } |