0b79944933bc9f7a8bcdfc94cd1befb2313b5277
[oota-llvm.git] / lib / MC / MCMachOStreamer.cpp
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   DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
31
32 private:
33   MCFragment *getCurrentFragment() const {
34     assert(CurSectionData && "No current section!");
35
36     if (!CurSectionData->empty())
37       return &CurSectionData->getFragmentList().back();
38
39     return 0;
40   }
41
42   MCSymbolData &getSymbolData(MCSymbol &Symbol) {
43     MCSymbolData *&Entry = SymbolMap[&Symbol];
44
45     if (!Entry)
46       Entry = new MCSymbolData(Symbol, 0, 0, &Assembler);
47
48     return *Entry;
49   }
50
51 public:
52   MCMachOStreamer(MCContext &Context, raw_ostream &_OS)
53     : MCStreamer(Context), Assembler(_OS), CurSectionData(0) {}
54   ~MCMachOStreamer() {}
55
56   /// @name MCStreamer Interface
57   /// @{
58
59   virtual void SwitchSection(const MCSection *Section);
60
61   virtual void EmitLabel(MCSymbol *Symbol);
62
63   virtual void EmitAssemblerFlag(AssemblerFlag Flag);
64
65   virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
66                               bool MakeAbsolute = false);
67
68   virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
69
70   virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
71
72   virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value);
73
74   virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
75                                 unsigned Pow2Alignment, bool IsLocal);
76
77   virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL,
78                             unsigned Size = 0, unsigned Pow2Alignment = 0);
79
80   virtual void EmitBytes(const StringRef &Data);
81
82   virtual void EmitValue(const MCValue &Value, unsigned Size);
83
84   virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
85                                     unsigned ValueSize = 1,
86                                     unsigned MaxBytesToEmit = 0);
87
88   virtual void EmitValueToOffset(const MCValue &Offset,
89                                  unsigned char Value = 0);
90
91   virtual void EmitInstruction(const MCInst &Inst);
92
93   virtual void Finish();
94
95   /// @}
96 };
97
98 } // end anonymous namespace.
99
100 void MCMachOStreamer::SwitchSection(const MCSection *Section) {
101   assert(Section && "Cannot switch to a null section!");
102
103   if (Section != CurSection) {
104     CurSection = Section;
105     MCSectionData *&Entry = SectionMap[Section];
106
107     if (!Entry)
108       Entry = new MCSectionData(*Section, &Assembler);
109
110     CurSectionData = Entry;
111   }
112 }
113
114 void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
115   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
116
117   MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
118   if (!F)
119     F = new MCDataFragment(CurSectionData);
120
121   MCSymbolData &SD = getSymbolData(*Symbol);
122   assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
123   SD.setFragment(F);
124   SD.setOffset(F->getContents().size());
125   
126   Symbol->setSection(*CurSection);
127 }
128
129 void MCMachOStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
130   llvm_unreachable("FIXME: Not yet implemented!");
131 }
132
133 void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol,
134                                      const MCValue &Value,
135                                      bool MakeAbsolute) {
136   // Only absolute symbols can be redefined.
137   assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
138          "Cannot define a symbol twice!");
139
140   llvm_unreachable("FIXME: Not yet implemented!");
141 }
142
143 void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
144                                           SymbolAttr Attribute) {
145   llvm_unreachable("FIXME: Not yet implemented!");
146 }
147
148 void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
149   llvm_unreachable("FIXME: Not yet implemented!");
150 }
151
152 void MCMachOStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
153   llvm_unreachable("FIXME: Not yet implemented!");
154 }
155
156 void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
157                                        unsigned Pow2Alignment,
158                                        bool IsLocal) {
159   llvm_unreachable("FIXME: Not yet implemented!");
160 }
161
162 void MCMachOStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
163                                    unsigned Size, unsigned Pow2Alignment) {
164   llvm_unreachable("FIXME: Not yet implemented!");
165 }
166
167 void MCMachOStreamer::EmitBytes(const StringRef &Data) {
168   MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
169   if (!DF)
170     DF = new MCDataFragment(CurSectionData);
171   DF->getContents().append(Data.begin(), Data.end());
172 }
173
174 void MCMachOStreamer::EmitValue(const MCValue &Value, unsigned Size) {
175   new MCFillFragment(Value, Size, 1, CurSectionData);
176 }
177
178 void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
179                                            int64_t Value, unsigned ValueSize,
180                                            unsigned MaxBytesToEmit) {
181   if (MaxBytesToEmit == 0)
182     MaxBytesToEmit = ByteAlignment;
183   new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
184                       CurSectionData);
185
186   // Update the maximum alignment on the current section if necessary.
187   if (ByteAlignment > CurSectionData->getAlignment())
188     CurSectionData->setAlignment(ByteAlignment);
189 }
190
191 void MCMachOStreamer::EmitValueToOffset(const MCValue &Offset,
192                                         unsigned char Value) {
193   new MCOrgFragment(Offset, Value, CurSectionData);
194 }
195
196 void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
197   llvm_unreachable("FIXME: Not yet implemented!");
198 }
199
200 void MCMachOStreamer::Finish() {
201   Assembler.Finish();
202 }
203
204 MCStreamer *llvm::createMachOStreamer(MCContext &Context, raw_ostream &OS) {
205   return new MCMachOStreamer(Context, OS);
206 }