Enable DebugInfo support for COFF object files.
[oota-llvm.git] / lib / MC / MCPureStreamer.cpp
1 //===- lib/MC/MCPureStreamer.cpp - MC "Pure" 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 #include "llvm/MC/MCAssembler.h"
12 #include "llvm/MC/MCCodeEmitter.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCExpr.h"
15 #include "llvm/MC/MCObjectStreamer.h"
16 // FIXME: Remove this.
17 #include "llvm/MC/MCSectionMachO.h"
18 #include "llvm/MC/MCSymbol.h"
19 #include "llvm/Support/ErrorHandling.h"
20
21 using namespace llvm;
22
23 namespace {
24
25 class MCPureStreamer : public MCObjectStreamer {
26 private:
27   virtual void EmitInstToFragment(const MCInst &Inst);
28   virtual void EmitInstToData(const MCInst &Inst);
29
30 public:
31   MCPureStreamer(MCContext &Context, TargetAsmBackend &TAB,
32                  raw_ostream &OS, MCCodeEmitter *Emitter)
33     : MCObjectStreamer(Context, TAB, OS, Emitter) {}
34
35   /// @name MCStreamer Interface
36   /// @{
37
38   virtual void InitSections();
39   virtual void EmitLabel(MCSymbol *Symbol);
40   virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
41   virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
42                             unsigned Size = 0, unsigned ByteAlignment = 0);
43   virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
44   virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
45                                     unsigned ValueSize = 1,
46                                     unsigned MaxBytesToEmit = 0);
47   virtual void EmitCodeAlignment(unsigned ByteAlignment,
48                                  unsigned MaxBytesToEmit = 0);
49   virtual void EmitValueToOffset(const MCExpr *Offset,
50                                  unsigned char Value = 0);
51   virtual void Finish();
52
53
54   virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) {
55     report_fatal_error("unsupported directive in pure streamer");
56   }
57   virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
58     report_fatal_error("unsupported directive in pure streamer");
59   }
60   virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
61                               uint64_t Size, unsigned ByteAlignment = 0) {
62     report_fatal_error("unsupported directive in pure streamer");
63   }
64   virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
65     report_fatal_error("unsupported directive in pure streamer");
66   }
67   virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
68                                 unsigned ByteAlignment) {
69     report_fatal_error("unsupported directive in pure streamer");
70   }
71   virtual void EmitThumbFunc(MCSymbol *Func) {
72     report_fatal_error("unsupported directive in pure streamer");
73   }
74   virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
75     report_fatal_error("unsupported directive in pure streamer");
76   }
77   virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
78     report_fatal_error("unsupported directive in pure streamer");
79   }
80   virtual void EmitCOFFSymbolType(int Type) {
81     report_fatal_error("unsupported directive in pure streamer");
82   }
83   virtual void EndCOFFSymbolDef() {
84     report_fatal_error("unsupported directive in pure streamer");
85   }
86   virtual void EmitCOFFSecRel32(MCSymbol const *Symbol) {
87     report_fatal_error("unsupported directive in pure streamer");
88   }
89   virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
90     report_fatal_error("unsupported directive in pure streamer");
91   }
92   virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
93     report_fatal_error("unsupported directive in pure streamer");
94   }
95   virtual void EmitFileDirective(StringRef Filename) {
96     report_fatal_error("unsupported directive in pure streamer");
97   }
98   virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) {
99     report_fatal_error("unsupported directive in pure streamer");
100     return false;
101   }
102
103   /// @}
104 };
105
106 } // end anonymous namespace.
107
108 void MCPureStreamer::InitSections() {
109   // FIMXE: To what!?
110   SwitchSection(getContext().getMachOSection("__TEXT", "__text",
111                                     MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
112                                     0, SectionKind::getText()));
113
114 }
115
116 void MCPureStreamer::EmitLabel(MCSymbol *Symbol) {
117   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
118   assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
119   assert(getCurrentSection() && "Cannot emit before setting section!");
120
121   Symbol->setSection(*getCurrentSection());
122
123   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
124
125   // We have to create a new fragment if this is an atom defining symbol,
126   // fragments cannot span atoms.
127   if (getAssembler().isSymbolLinkerVisible(SD.getSymbol()))
128     new MCDataFragment(getCurrentSectionData());
129
130   // FIXME: This is wasteful, we don't necessarily need to create a data
131   // fragment. Instead, we should mark the symbol as pointing into the data
132   // fragment if it exists, otherwise we should just queue the label and set its
133   // fragment pointer when we emit the next fragment.
134   MCDataFragment *F = getOrCreateDataFragment();
135   assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
136   SD.setFragment(F);
137   SD.setOffset(F->getContents().size());
138 }
139
140 void MCPureStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
141   // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
142   // MCObjectStreamer.
143   // FIXME: Lift context changes into super class.
144   getAssembler().getOrCreateSymbolData(*Symbol);
145   Symbol->setVariableValue(AddValueSymbols(Value));
146 }
147
148 void MCPureStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
149                                   unsigned Size, unsigned ByteAlignment) {
150   report_fatal_error("not yet implemented in pure streamer");
151 }
152
153 void MCPureStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
154   // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
155   // MCObjectStreamer.
156   getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
157 }
158
159 void MCPureStreamer::EmitValueToAlignment(unsigned ByteAlignment,
160                                           int64_t Value, unsigned ValueSize,
161                                           unsigned MaxBytesToEmit) {
162   // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
163   // MCObjectStreamer.
164   if (MaxBytesToEmit == 0)
165     MaxBytesToEmit = ByteAlignment;
166   new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
167                       getCurrentSectionData());
168
169   // Update the maximum alignment on the current section if necessary.
170   if (ByteAlignment > getCurrentSectionData()->getAlignment())
171     getCurrentSectionData()->setAlignment(ByteAlignment);
172 }
173
174 void MCPureStreamer::EmitCodeAlignment(unsigned ByteAlignment,
175                                        unsigned MaxBytesToEmit) {
176   // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
177   // MCObjectStreamer.
178   if (MaxBytesToEmit == 0)
179     MaxBytesToEmit = ByteAlignment;
180   MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
181                                            getCurrentSectionData());
182   F->setEmitNops(true);
183
184   // Update the maximum alignment on the current section if necessary.
185   if (ByteAlignment > getCurrentSectionData()->getAlignment())
186     getCurrentSectionData()->setAlignment(ByteAlignment);
187 }
188
189 void MCPureStreamer::EmitValueToOffset(const MCExpr *Offset,
190                                        unsigned char Value) {
191   new MCOrgFragment(*Offset, Value, getCurrentSectionData());
192 }
193
194 void MCPureStreamer::EmitInstToFragment(const MCInst &Inst) {
195   MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
196
197   // Add the fixups and data.
198   //
199   // FIXME: Revisit this design decision when relaxation is done, we may be
200   // able to get away with not storing any extra data in the MCInst.
201   SmallVector<MCFixup, 4> Fixups;
202   SmallString<256> Code;
203   raw_svector_ostream VecOS(Code);
204   getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
205   VecOS.flush();
206
207   IF->getCode() = Code;
208   IF->getFixups() = Fixups;
209 }
210
211 void MCPureStreamer::EmitInstToData(const MCInst &Inst) {
212   MCDataFragment *DF = getOrCreateDataFragment();
213
214   SmallVector<MCFixup, 4> Fixups;
215   SmallString<256> Code;
216   raw_svector_ostream VecOS(Code);
217   getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
218   VecOS.flush();
219
220   // Add the fixups and data.
221   for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
222     Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
223     DF->addFixup(Fixups[i]);
224   }
225   DF->getContents().append(Code.begin(), Code.end());
226 }
227
228 void MCPureStreamer::Finish() {
229   // FIXME: Handle DWARF tables?
230
231   this->MCObjectStreamer::Finish();
232 }
233
234 MCStreamer *llvm::createPureStreamer(MCContext &Context, TargetAsmBackend &TAB,
235                                      raw_ostream &OS, MCCodeEmitter *CE) {
236   return new MCPureStreamer(Context, TAB, OS, CE);
237 }