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; |
| 29 | |
| 30 | public: |
| 31 | MCMachOStreamer(MCContext &Context, raw_ostream &_OS) |
| 32 | : MCStreamer(Context), Assembler(_OS), CurSectionData(0) {} |
| 33 | ~MCMachOStreamer() {} |
| 34 | |
| 35 | /// @name MCStreamer Interface |
| 36 | /// @{ |
| 37 | |
| 38 | virtual void SwitchSection(const MCSection *Section); |
| 39 | |
| 40 | virtual void EmitLabel(MCSymbol *Symbol); |
| 41 | |
| 42 | virtual void EmitAssemblerFlag(AssemblerFlag Flag); |
| 43 | |
| 44 | virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value, |
| 45 | bool MakeAbsolute = false); |
| 46 | |
| 47 | virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute); |
| 48 | |
| 49 | virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue); |
| 50 | |
| 51 | virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value); |
| 52 | |
| 53 | virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size, |
| 54 | unsigned Pow2Alignment, bool IsLocal); |
| 55 | |
| 56 | virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL, |
| 57 | unsigned Size = 0, unsigned Pow2Alignment = 0); |
| 58 | |
| 59 | virtual void EmitBytes(const StringRef &Data); |
| 60 | |
| 61 | virtual void EmitValue(const MCValue &Value, unsigned Size); |
| 62 | |
| 63 | virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, |
| 64 | unsigned ValueSize = 1, |
| 65 | unsigned MaxBytesToEmit = 0); |
| 66 | |
| 67 | virtual void EmitValueToOffset(const MCValue &Offset, |
| 68 | unsigned char Value = 0); |
| 69 | |
| 70 | virtual void EmitInstruction(const MCInst &Inst); |
| 71 | |
| 72 | virtual void Finish(); |
| 73 | |
| 74 | /// @} |
| 75 | }; |
| 76 | |
| 77 | } // end anonymous namespace. |
| 78 | |
| 79 | void MCMachOStreamer::SwitchSection(const MCSection *Section) { |
| 80 | assert(Section && "Cannot switch to a null section!"); |
| 81 | |
| 82 | if (Section != CurSection) { |
| 83 | CurSection = Section; |
| 84 | MCSectionData *&Entry = SectionMap[Section]; |
| 85 | |
| 86 | if (!Entry) |
| 87 | Entry = new MCSectionData(*Section, &Assembler); |
| 88 | |
| 89 | CurSectionData = Entry; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) { |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame^] | 94 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 95 | assert(CurSection && "Cannot emit before setting section!"); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 96 | |
| 97 | llvm_unreachable("FIXME: Not yet implemented!"); |
| 98 | |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame^] | 99 | Symbol->setSection(*CurSection); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void MCMachOStreamer::EmitAssemblerFlag(AssemblerFlag Flag) { |
| 103 | llvm_unreachable("FIXME: Not yet implemented!"); |
| 104 | } |
| 105 | |
| 106 | void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, |
| 107 | const MCValue &Value, |
| 108 | bool MakeAbsolute) { |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame^] | 109 | // Only absolute symbols can be redefined. |
| 110 | assert((Symbol->isUndefined() || Symbol->isAbsolute()) && |
| 111 | "Cannot define a symbol twice!"); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 112 | |
| 113 | llvm_unreachable("FIXME: Not yet implemented!"); |
| 114 | } |
| 115 | |
| 116 | void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol, |
| 117 | SymbolAttr Attribute) { |
| 118 | llvm_unreachable("FIXME: Not yet implemented!"); |
| 119 | } |
| 120 | |
| 121 | void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { |
| 122 | llvm_unreachable("FIXME: Not yet implemented!"); |
| 123 | } |
| 124 | |
| 125 | void MCMachOStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) { |
| 126 | llvm_unreachable("FIXME: Not yet implemented!"); |
| 127 | } |
| 128 | |
| 129 | void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size, |
| 130 | unsigned Pow2Alignment, |
| 131 | bool IsLocal) { |
| 132 | llvm_unreachable("FIXME: Not yet implemented!"); |
| 133 | } |
| 134 | |
| 135 | void MCMachOStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol, |
| 136 | unsigned Size, unsigned Pow2Alignment) { |
| 137 | llvm_unreachable("FIXME: Not yet implemented!"); |
| 138 | } |
| 139 | |
| 140 | void MCMachOStreamer::EmitBytes(const StringRef &Data) { |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 141 | MCDataFragment *DF = new MCDataFragment(CurSectionData); |
| 142 | DF->getContents().append(Data.begin(), Data.end()); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void MCMachOStreamer::EmitValue(const MCValue &Value, unsigned Size) { |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 146 | new MCFillFragment(Value, Size, 1, CurSectionData); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment, |
| 150 | int64_t Value, unsigned ValueSize, |
| 151 | unsigned MaxBytesToEmit) { |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 152 | if (MaxBytesToEmit == 0) |
| 153 | MaxBytesToEmit = ByteAlignment; |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 154 | new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit, |
| 155 | CurSectionData); |
| 156 | |
| 157 | // Update the maximum alignment on the current section if necessary |
| 158 | if (ByteAlignment > CurSectionData->getAlignment()) |
| 159 | CurSectionData->setAlignment(ByteAlignment); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | void MCMachOStreamer::EmitValueToOffset(const MCValue &Offset, |
| 163 | unsigned char Value) { |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 164 | new MCOrgFragment(Offset, Value, CurSectionData); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | void MCMachOStreamer::EmitInstruction(const MCInst &Inst) { |
| 168 | llvm_unreachable("FIXME: Not yet implemented!"); |
| 169 | } |
| 170 | |
| 171 | void MCMachOStreamer::Finish() { |
| 172 | Assembler.Finish(); |
| 173 | } |
| 174 | |
| 175 | MCStreamer *llvm::createMachOStreamer(MCContext &Context, raw_ostream &OS) { |
| 176 | return new MCMachOStreamer(Context, OS); |
| 177 | } |