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/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"
23 class MCMachOStreamer : public MCStreamer {
24 MCAssembler Assembler;
26 MCSectionData *CurSectionData;
28 DenseMap<const MCSection*, MCSectionData*> SectionMap;
30 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
33 MCFragment *getCurrentFragment() const {
34 assert(CurSectionData && "No current section!");
36 if (!CurSectionData->empty())
37 return &CurSectionData->getFragmentList().back();
42 MCSymbolData &getSymbolData(MCSymbol &Symbol) {
43 MCSymbolData *&Entry = SymbolMap[&Symbol];
46 Entry = new MCSymbolData(Symbol, 0, 0, &Assembler);
52 MCMachOStreamer(MCContext &Context, raw_ostream &_OS)
53 : MCStreamer(Context), Assembler(_OS), CurSectionData(0) {}
56 /// @name MCStreamer Interface
59 virtual void SwitchSection(const MCSection *Section);
61 virtual void EmitLabel(MCSymbol *Symbol);
63 virtual void EmitAssemblerFlag(AssemblerFlag Flag);
65 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
66 bool MakeAbsolute = false);
68 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
70 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
72 virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value);
74 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
75 unsigned Pow2Alignment, bool IsLocal);
77 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL,
78 unsigned Size = 0, unsigned Pow2Alignment = 0);
80 virtual void EmitBytes(const StringRef &Data);
82 virtual void EmitValue(const MCValue &Value, unsigned Size);
84 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
85 unsigned ValueSize = 1,
86 unsigned MaxBytesToEmit = 0);
88 virtual void EmitValueToOffset(const MCValue &Offset,
89 unsigned char Value = 0);
91 virtual void EmitInstruction(const MCInst &Inst);
93 virtual void Finish();
98 } // end anonymous namespace.
100 void MCMachOStreamer::SwitchSection(const MCSection *Section) {
101 assert(Section && "Cannot switch to a null section!");
103 if (Section != CurSection) {
104 CurSection = Section;
105 MCSectionData *&Entry = SectionMap[Section];
108 Entry = new MCSectionData(*Section, &Assembler);
110 CurSectionData = Entry;
114 void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
115 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
117 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
119 F = new MCDataFragment(CurSectionData);
121 MCSymbolData &SD = getSymbolData(*Symbol);
122 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
124 SD.setOffset(F->getContents().size());
126 Symbol->setSection(*CurSection);
129 void MCMachOStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
130 llvm_unreachable("FIXME: Not yet implemented!");
133 void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol,
134 const MCValue &Value,
136 // Only absolute symbols can be redefined.
137 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
138 "Cannot define a symbol twice!");
140 llvm_unreachable("FIXME: Not yet implemented!");
143 void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
144 SymbolAttr Attribute) {
147 llvm_unreachable("FIXME: Not yet implemented!");
149 case MCStreamer::Global:
150 getSymbolData(*Symbol).setExternal(true);
155 void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
156 llvm_unreachable("FIXME: Not yet implemented!");
159 void MCMachOStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
160 llvm_unreachable("FIXME: Not yet implemented!");
163 void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
164 unsigned Pow2Alignment,
166 llvm_unreachable("FIXME: Not yet implemented!");
169 void MCMachOStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
170 unsigned Size, unsigned Pow2Alignment) {
171 llvm_unreachable("FIXME: Not yet implemented!");
174 void MCMachOStreamer::EmitBytes(const StringRef &Data) {
175 MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
177 DF = new MCDataFragment(CurSectionData);
178 DF->getContents().append(Data.begin(), Data.end());
181 void MCMachOStreamer::EmitValue(const MCValue &Value, unsigned Size) {
182 new MCFillFragment(Value, Size, 1, CurSectionData);
185 void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
186 int64_t Value, unsigned ValueSize,
187 unsigned MaxBytesToEmit) {
188 if (MaxBytesToEmit == 0)
189 MaxBytesToEmit = ByteAlignment;
190 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
193 // Update the maximum alignment on the current section if necessary.
194 if (ByteAlignment > CurSectionData->getAlignment())
195 CurSectionData->setAlignment(ByteAlignment);
198 void MCMachOStreamer::EmitValueToOffset(const MCValue &Offset,
199 unsigned char Value) {
200 new MCOrgFragment(Offset, Value, CurSectionData);
203 void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
204 llvm_unreachable("FIXME: Not yet implemented!");
207 void MCMachOStreamer::Finish() {
211 MCStreamer *llvm::createMachOStreamer(MCContext &Context, raw_ostream &OS) {
212 return new MCMachOStreamer(Context, OS);