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;
31 MCMachOStreamer(MCContext &Context, raw_ostream &_OS)
32 : MCStreamer(Context), Assembler(_OS), CurSectionData(0) {}
35 /// @name MCStreamer Interface
38 virtual void SwitchSection(const MCSection *Section);
40 virtual void EmitLabel(MCSymbol *Symbol);
42 virtual void EmitAssemblerFlag(AssemblerFlag Flag);
44 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
45 bool MakeAbsolute = false);
47 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
49 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
51 virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value);
53 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
54 unsigned Pow2Alignment, bool IsLocal);
56 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL,
57 unsigned Size = 0, unsigned Pow2Alignment = 0);
59 virtual void EmitBytes(const StringRef &Data);
61 virtual void EmitValue(const MCValue &Value, unsigned Size);
63 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
64 unsigned ValueSize = 1,
65 unsigned MaxBytesToEmit = 0);
67 virtual void EmitValueToOffset(const MCValue &Offset,
68 unsigned char Value = 0);
70 virtual void EmitInstruction(const MCInst &Inst);
72 virtual void Finish();
77 } // end anonymous namespace.
79 void MCMachOStreamer::SwitchSection(const MCSection *Section) {
80 assert(Section && "Cannot switch to a null section!");
82 if (Section != CurSection) {
84 MCSectionData *&Entry = SectionMap[Section];
87 Entry = new MCSectionData(*Section, &Assembler);
89 CurSectionData = Entry;
93 void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
94 assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
95 assert(CurSection && "Cannot emit before setting section!");
96 assert(!getContext().GetSymbolValue(Symbol) &&
97 "Cannot emit symbol which was directly assigned to!");
99 llvm_unreachable("FIXME: Not yet implemented!");
101 Symbol->setSection(CurSection);
102 Symbol->setExternal(false);
105 void MCMachOStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
106 llvm_unreachable("FIXME: Not yet implemented!");
109 void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol,
110 const MCValue &Value,
112 assert(!Symbol->getSection() && "Cannot assign to a label!");
114 llvm_unreachable("FIXME: Not yet implemented!");
117 void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
118 SymbolAttr Attribute) {
119 llvm_unreachable("FIXME: Not yet implemented!");
122 void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
123 llvm_unreachable("FIXME: Not yet implemented!");
126 void MCMachOStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
127 llvm_unreachable("FIXME: Not yet implemented!");
130 void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
131 unsigned Pow2Alignment,
133 llvm_unreachable("FIXME: Not yet implemented!");
136 void MCMachOStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
137 unsigned Size, unsigned Pow2Alignment) {
138 llvm_unreachable("FIXME: Not yet implemented!");
141 void MCMachOStreamer::EmitBytes(const StringRef &Data) {
142 MCDataFragment *DF = new MCDataFragment(CurSectionData);
143 DF->getContents().append(Data.begin(), Data.end());
146 void MCMachOStreamer::EmitValue(const MCValue &Value, unsigned Size) {
147 new MCFillFragment(Value, Size, 1, CurSectionData);
150 void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
151 int64_t Value, unsigned ValueSize,
152 unsigned MaxBytesToEmit) {
153 if (MaxBytesToEmit == 0)
154 MaxBytesToEmit = ByteAlignment;
155 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
158 // Update the maximum alignment on the current section if necessary
159 if (ByteAlignment > CurSectionData->getAlignment())
160 CurSectionData->setAlignment(ByteAlignment);
163 void MCMachOStreamer::EmitValueToOffset(const MCValue &Offset,
164 unsigned char Value) {
165 new MCOrgFragment(Offset, Value, CurSectionData);
168 void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
169 llvm_unreachable("FIXME: Not yet implemented!");
172 void MCMachOStreamer::Finish() {
176 MCStreamer *llvm::createMachOStreamer(MCContext &Context, raw_ostream &OS) {
177 return new MCMachOStreamer(Context, OS);