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