1 //===- lib/MC/MCMachOStreamer.cpp - Mach-O Object Output ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCStreamer.h"
12 #include "llvm/MC/MCAssembler.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCSection.h"
15 #include "llvm/MC/MCSymbol.h"
16 #include "llvm/Support/ErrorHandling.h"
21 class MCMachOStreamer : public MCStreamer {
22 MCAssembler Assembler;
24 MCSectionData *CurSectionData;
26 DenseMap<const MCSection*, MCSectionData*> SectionMap;
28 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
31 MCFragment *getCurrentFragment() const {
32 assert(CurSectionData && "No current section!");
34 if (!CurSectionData->empty())
35 return &CurSectionData->getFragmentList().back();
40 MCSymbolData &getSymbolData(MCSymbol &Symbol) {
41 MCSymbolData *&Entry = SymbolMap[&Symbol];
44 Entry = new MCSymbolData(Symbol, 0, 0, &Assembler);
50 MCMachOStreamer(MCContext &Context, raw_ostream &_OS)
51 : MCStreamer(Context), Assembler(_OS), CurSectionData(0) {}
54 /// @name MCStreamer Interface
57 virtual void SwitchSection(const MCSection *Section);
59 virtual void EmitLabel(MCSymbol *Symbol);
61 virtual void EmitAssemblerFlag(AssemblerFlag Flag);
63 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
64 bool MakeAbsolute = false);
66 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
68 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
70 virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value);
72 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
73 unsigned Pow2Alignment, bool IsLocal);
75 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL,
76 unsigned Size = 0, unsigned Pow2Alignment = 0);
78 virtual void EmitBytes(const StringRef &Data);
80 virtual void EmitValue(const MCValue &Value, unsigned Size);
82 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
83 unsigned ValueSize = 1,
84 unsigned MaxBytesToEmit = 0);
86 virtual void EmitValueToOffset(const MCValue &Offset,
87 unsigned char Value = 0);
89 virtual void EmitInstruction(const MCInst &Inst);
91 virtual void Finish();
96 } // end anonymous namespace.
98 void MCMachOStreamer::SwitchSection(const MCSection *Section) {
99 assert(Section && "Cannot switch to a null section!");
101 // If already in this section, then this is a noop.
102 if (Section == CurSection) return;
104 CurSection = Section;
105 MCSectionData *&Entry = SectionMap[Section];
108 Entry = new MCSectionData(*Section, &Assembler);
110 CurSectionData = Entry;
113 void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
114 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
116 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
118 F = new MCDataFragment(CurSectionData);
120 MCSymbolData &SD = getSymbolData(*Symbol);
121 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
123 SD.setOffset(F->getContents().size());
125 Symbol->setSection(*CurSection);
128 void MCMachOStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
129 llvm_unreachable("FIXME: Not yet implemented!");
132 void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol,
133 const MCValue &Value,
135 // Only absolute symbols can be redefined.
136 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
137 "Cannot define a symbol twice!");
139 llvm_unreachable("FIXME: Not yet implemented!");
142 void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
143 SymbolAttr Attribute) {
146 llvm_unreachable("FIXME: Not yet implemented!");
148 case MCStreamer::Global:
149 getSymbolData(*Symbol).setExternal(true);
154 void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
155 llvm_unreachable("FIXME: Not yet implemented!");
158 void MCMachOStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
159 llvm_unreachable("FIXME: Not yet implemented!");
162 void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
163 unsigned Pow2Alignment,
165 llvm_unreachable("FIXME: Not yet implemented!");
168 void MCMachOStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
169 unsigned Size, unsigned Pow2Alignment) {
170 llvm_unreachable("FIXME: Not yet implemented!");
173 void MCMachOStreamer::EmitBytes(const StringRef &Data) {
174 MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
176 DF = new MCDataFragment(CurSectionData);
177 DF->getContents().append(Data.begin(), Data.end());
180 void MCMachOStreamer::EmitValue(const MCValue &Value, unsigned Size) {
181 new MCFillFragment(Value, Size, 1, CurSectionData);
184 void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
185 int64_t Value, unsigned ValueSize,
186 unsigned MaxBytesToEmit) {
187 if (MaxBytesToEmit == 0)
188 MaxBytesToEmit = ByteAlignment;
189 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
192 // Update the maximum alignment on the current section if necessary.
193 if (ByteAlignment > CurSectionData->getAlignment())
194 CurSectionData->setAlignment(ByteAlignment);
197 void MCMachOStreamer::EmitValueToOffset(const MCValue &Offset,
198 unsigned char Value) {
199 new MCOrgFragment(Offset, Value, CurSectionData);
202 void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
203 llvm_unreachable("FIXME: Not yet implemented!");
206 void MCMachOStreamer::Finish() {
210 MCStreamer *llvm::createMachOStreamer(MCContext &Context, raw_ostream &OS) {
211 return new MCMachOStreamer(Context, OS);