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->isUndefined() && "Cannot define a symbol twice!");
95 assert(CurSection && "Cannot emit before setting section!");
97 llvm_unreachable("FIXME: Not yet implemented!");
99 Symbol->setSection(*CurSection);
102 void MCMachOStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
103 llvm_unreachable("FIXME: Not yet implemented!");
106 void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol,
107 const MCValue &Value,
109 // Only absolute symbols can be redefined.
110 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
111 "Cannot define a symbol twice!");
113 llvm_unreachable("FIXME: Not yet implemented!");
116 void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
117 SymbolAttr Attribute) {
118 llvm_unreachable("FIXME: Not yet implemented!");
121 void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
122 llvm_unreachable("FIXME: Not yet implemented!");
125 void MCMachOStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
126 llvm_unreachable("FIXME: Not yet implemented!");
129 void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
130 unsigned Pow2Alignment,
132 llvm_unreachable("FIXME: Not yet implemented!");
135 void MCMachOStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
136 unsigned Size, unsigned Pow2Alignment) {
137 llvm_unreachable("FIXME: Not yet implemented!");
140 void MCMachOStreamer::EmitBytes(const StringRef &Data) {
141 MCDataFragment *DF = new MCDataFragment(CurSectionData);
142 DF->getContents().append(Data.begin(), Data.end());
145 void MCMachOStreamer::EmitValue(const MCValue &Value, unsigned Size) {
146 new MCFillFragment(Value, Size, 1, CurSectionData);
149 void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
150 int64_t Value, unsigned ValueSize,
151 unsigned MaxBytesToEmit) {
152 if (MaxBytesToEmit == 0)
153 MaxBytesToEmit = ByteAlignment;
154 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
157 // Update the maximum alignment on the current section if necessary
158 if (ByteAlignment > CurSectionData->getAlignment())
159 CurSectionData->setAlignment(ByteAlignment);
162 void MCMachOStreamer::EmitValueToOffset(const MCValue &Offset,
163 unsigned char Value) {
164 new MCOrgFragment(Offset, Value, CurSectionData);
167 void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
168 llvm_unreachable("FIXME: Not yet implemented!");
171 void MCMachOStreamer::Finish() {
175 MCStreamer *llvm::createMachOStreamer(MCContext &Context, raw_ostream &OS) {
176 return new MCMachOStreamer(Context, OS);