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 | |
| 12 | #include "llvm/ADT/DenseMap.h" |
| 13 | #include "llvm/MC/MCAssembler.h" |
| 14 | #include "llvm/MC/MCContext.h" |
| 15 | #include "llvm/MC/MCSection.h" |
| 16 | #include "llvm/MC/MCSymbol.h" |
| 17 | #include "llvm/MC/MCValue.h" |
| 18 | #include "llvm/Support/ErrorHandling.h" |
| 19 | using namespace llvm; |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | class MCMachOStreamer : public MCStreamer { |
| 24 | MCAssembler Assembler; |
| 25 | |
| 26 | MCSectionData *CurSectionData; |
| 27 | |
| 28 | DenseMap<const MCSection*, MCSectionData*> SectionMap; |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 29 | |
| 30 | DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap; |
| 31 | |
| 32 | private: |
| 33 | MCFragment *getCurrentFragment() const { |
| 34 | assert(CurSectionData && "No current section!"); |
| 35 | |
| 36 | if (!CurSectionData->empty()) |
| 37 | return &CurSectionData->getFragmentList().back(); |
| 38 | |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | MCSymbolData &getSymbolData(MCSymbol &Symbol) { |
| 43 | MCSymbolData *&Entry = SymbolMap[&Symbol]; |
| 44 | |
| 45 | if (!Entry) |
| 46 | Entry = new MCSymbolData(Symbol, 0, 0, &Assembler); |
| 47 | |
| 48 | return *Entry; |
| 49 | } |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 50 | |
| 51 | public: |
| 52 | MCMachOStreamer(MCContext &Context, raw_ostream &_OS) |
| 53 | : MCStreamer(Context), Assembler(_OS), CurSectionData(0) {} |
| 54 | ~MCMachOStreamer() {} |
| 55 | |
| 56 | /// @name MCStreamer Interface |
| 57 | /// @{ |
| 58 | |
| 59 | virtual void SwitchSection(const MCSection *Section); |
| 60 | |
| 61 | virtual void EmitLabel(MCSymbol *Symbol); |
| 62 | |
| 63 | virtual void EmitAssemblerFlag(AssemblerFlag Flag); |
| 64 | |
| 65 | virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value, |
| 66 | bool MakeAbsolute = false); |
| 67 | |
| 68 | virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute); |
| 69 | |
| 70 | virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue); |
| 71 | |
| 72 | virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value); |
| 73 | |
| 74 | virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size, |
| 75 | unsigned Pow2Alignment, bool IsLocal); |
| 76 | |
| 77 | virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL, |
| 78 | unsigned Size = 0, unsigned Pow2Alignment = 0); |
| 79 | |
| 80 | virtual void EmitBytes(const StringRef &Data); |
| 81 | |
| 82 | virtual void EmitValue(const MCValue &Value, unsigned Size); |
| 83 | |
| 84 | virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, |
| 85 | unsigned ValueSize = 1, |
| 86 | unsigned MaxBytesToEmit = 0); |
| 87 | |
| 88 | virtual void EmitValueToOffset(const MCValue &Offset, |
| 89 | unsigned char Value = 0); |
| 90 | |
| 91 | virtual void EmitInstruction(const MCInst &Inst); |
| 92 | |
| 93 | virtual void Finish(); |
| 94 | |
| 95 | /// @} |
| 96 | }; |
| 97 | |
| 98 | } // end anonymous namespace. |
| 99 | |
| 100 | void MCMachOStreamer::SwitchSection(const MCSection *Section) { |
| 101 | assert(Section && "Cannot switch to a null section!"); |
| 102 | |
| 103 | if (Section != CurSection) { |
| 104 | CurSection = Section; |
| 105 | MCSectionData *&Entry = SectionMap[Section]; |
| 106 | |
| 107 | if (!Entry) |
| 108 | Entry = new MCSectionData(*Section, &Assembler); |
| 109 | |
| 110 | CurSectionData = Entry; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) { |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 115 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 116 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 117 | MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment()); |
| 118 | if (!F) |
| 119 | F = new MCDataFragment(CurSectionData); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 120 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 121 | MCSymbolData &SD = getSymbolData(*Symbol); |
| 122 | assert(!SD.getFragment() && "Unexpected fragment on symbol data!"); |
| 123 | SD.setFragment(F); |
| 124 | SD.setOffset(F->getContents().size()); |
| 125 | |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 126 | Symbol->setSection(*CurSection); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void MCMachOStreamer::EmitAssemblerFlag(AssemblerFlag Flag) { |
| 130 | llvm_unreachable("FIXME: Not yet implemented!"); |
| 131 | } |
| 132 | |
| 133 | void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, |
| 134 | const MCValue &Value, |
| 135 | bool MakeAbsolute) { |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 136 | // Only absolute symbols can be redefined. |
| 137 | assert((Symbol->isUndefined() || Symbol->isAbsolute()) && |
| 138 | "Cannot define a symbol twice!"); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 139 | |
| 140 | llvm_unreachable("FIXME: Not yet implemented!"); |
| 141 | } |
| 142 | |
| 143 | void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol, |
| 144 | SymbolAttr Attribute) { |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame^] | 145 | switch (Attribute) { |
| 146 | default: |
| 147 | llvm_unreachable("FIXME: Not yet implemented!"); |
| 148 | |
| 149 | case MCStreamer::Global: |
| 150 | getSymbolData(*Symbol).setExternal(true); |
| 151 | break; |
| 152 | } |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { |
| 156 | llvm_unreachable("FIXME: Not yet implemented!"); |
| 157 | } |
| 158 | |
| 159 | void MCMachOStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) { |
| 160 | llvm_unreachable("FIXME: Not yet implemented!"); |
| 161 | } |
| 162 | |
| 163 | void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size, |
| 164 | unsigned Pow2Alignment, |
| 165 | bool IsLocal) { |
| 166 | llvm_unreachable("FIXME: Not yet implemented!"); |
| 167 | } |
| 168 | |
| 169 | void MCMachOStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol, |
| 170 | unsigned Size, unsigned Pow2Alignment) { |
| 171 | llvm_unreachable("FIXME: Not yet implemented!"); |
| 172 | } |
| 173 | |
| 174 | void MCMachOStreamer::EmitBytes(const StringRef &Data) { |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 175 | MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment()); |
| 176 | if (!DF) |
| 177 | DF = new MCDataFragment(CurSectionData); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 178 | DF->getContents().append(Data.begin(), Data.end()); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | void MCMachOStreamer::EmitValue(const MCValue &Value, unsigned Size) { |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 182 | new MCFillFragment(Value, Size, 1, CurSectionData); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment, |
| 186 | int64_t Value, unsigned ValueSize, |
| 187 | unsigned MaxBytesToEmit) { |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 188 | if (MaxBytesToEmit == 0) |
| 189 | MaxBytesToEmit = ByteAlignment; |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 190 | new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit, |
| 191 | CurSectionData); |
| 192 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 193 | // Update the maximum alignment on the current section if necessary. |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 194 | if (ByteAlignment > CurSectionData->getAlignment()) |
| 195 | CurSectionData->setAlignment(ByteAlignment); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void MCMachOStreamer::EmitValueToOffset(const MCValue &Offset, |
| 199 | unsigned char Value) { |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 200 | new MCOrgFragment(Offset, Value, CurSectionData); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | void MCMachOStreamer::EmitInstruction(const MCInst &Inst) { |
| 204 | llvm_unreachable("FIXME: Not yet implemented!"); |
| 205 | } |
| 206 | |
| 207 | void MCMachOStreamer::Finish() { |
| 208 | Assembler.Finish(); |
| 209 | } |
| 210 | |
| 211 | MCStreamer *llvm::createMachOStreamer(MCContext &Context, raw_ostream &OS) { |
| 212 | return new MCMachOStreamer(Context, OS); |
| 213 | } |