Add support for assigning to . in AsmParser.
[oota-llvm.git] / lib / MC / WinCOFFStreamer.cpp
1 //===-- llvm/MC/WinCOFFStreamer.cpp -----------------------------*- C++ -*-===//
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 // This file contains an implementation of a Win32 COFF object file streamer.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "WinCOFFStreamer"
15
16 #include "llvm/MC/MCStreamer.h"
17 #include "llvm/MC/MCAsmBackend.h"
18 #include "llvm/MC/MCAsmLayout.h"
19 #include "llvm/MC/MCAssembler.h"
20 #include "llvm/MC/MCCodeEmitter.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCObjectFileInfo.h"
24 #include "llvm/MC/MCObjectStreamer.h"
25 #include "llvm/MC/MCSection.h"
26 #include "llvm/MC/MCSectionCOFF.h"
27 #include "llvm/MC/MCSymbol.h"
28 #include "llvm/MC/MCValue.h"
29 #include "llvm/MC/MCWin64EH.h"
30 #include "llvm/Support/COFF.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/TargetRegistry.h"
34 #include "llvm/Support/raw_ostream.h"
35
36 using namespace llvm;
37
38 namespace {
39 class WinCOFFStreamer : public MCObjectStreamer {
40 public:
41   MCSymbol const *CurSymbol;
42
43   WinCOFFStreamer(MCContext &Context,
44                   MCAsmBackend &MAB,
45                   MCCodeEmitter &CE,
46                   raw_ostream &OS);
47
48   void AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
49                        unsigned ByteAlignment, bool External);
50
51   // MCStreamer interface
52
53   virtual void InitSections(bool Force);
54   virtual void EmitLabel(MCSymbol *Symbol);
55   virtual void EmitDebugLabel(MCSymbol *Symbol);
56   virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
57   virtual void EmitThumbFunc(MCSymbol *Func);
58   virtual bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
59   virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
60   virtual void BeginCOFFSymbolDef(MCSymbol const *Symbol);
61   virtual void EmitCOFFSymbolStorageClass(int StorageClass);
62   virtual void EmitCOFFSymbolType(int Type);
63   virtual void EndCOFFSymbolDef();
64   virtual void EmitCOFFSectionIndex(MCSymbol const *Symbol);
65   virtual void EmitCOFFSecRel32(MCSymbol const *Symbol);
66   virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
67   virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
68                                 unsigned ByteAlignment);
69   virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
70                                      unsigned ByteAlignment);
71   virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
72                             uint64_t Size,unsigned ByteAlignment);
73   virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
74                               uint64_t Size, unsigned ByteAlignment);
75   virtual void EmitFileDirective(StringRef Filename);
76   virtual void EmitIdent(StringRef IdentString);
77   virtual void EmitWin64EHHandlerData();
78   virtual void FinishImpl();
79
80 private:
81   virtual void EmitInstToData(const MCInst &Inst, const MCSubtargetInfo &STI) {
82     MCDataFragment *DF = getOrCreateDataFragment();
83
84     SmallVector<MCFixup, 4> Fixups;
85     SmallString<256> Code;
86     raw_svector_ostream VecOS(Code);
87     getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups, STI);
88     VecOS.flush();
89
90     // Add the fixups and data.
91     for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
92       Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
93       DF->getFixups().push_back(Fixups[i]);
94     }
95     DF->getContents().append(Code.begin(), Code.end());
96   }
97 };
98 } // end anonymous namespace.
99
100 WinCOFFStreamer::WinCOFFStreamer(MCContext &Context, MCAsmBackend &MAB,
101                                  MCCodeEmitter &CE, raw_ostream &OS)
102     : MCObjectStreamer(Context, MAB, OS, &CE), CurSymbol(NULL) {}
103
104 void WinCOFFStreamer::AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
105                                       unsigned ByteAlignment, bool External) {
106   assert(!Symbol->isInSection() && "Symbol must not already have a section!");
107
108   const MCSection *Section = getContext().getObjectFileInfo()->getBSSSection();
109   MCSectionData &SectionData = getAssembler().getOrCreateSectionData(*Section);
110   if (SectionData.getAlignment() < ByteAlignment)
111     SectionData.setAlignment(ByteAlignment);
112
113   MCSymbolData &SymbolData = getAssembler().getOrCreateSymbolData(*Symbol);
114   SymbolData.setExternal(External);
115
116   AssignSection(Symbol, Section);
117
118   if (ByteAlignment != 1)
119       new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectionData);
120
121   SymbolData.setFragment(new MCFillFragment(0, 0, Size, &SectionData));
122 }
123
124 // MCStreamer interface
125
126 void WinCOFFStreamer::InitSections(bool Force) {
127   // FIXME: this is identical to the ELF one.
128   // This emulates the same behavior of GNU as. This makes it easier
129   // to compare the output as the major sections are in the same order.
130   SwitchSection(getContext().getObjectFileInfo()->getTextSection());
131   EmitCodeAlignment(4);
132
133   SwitchSection(getContext().getObjectFileInfo()->getDataSection());
134   EmitCodeAlignment(4);
135
136   SwitchSection(getContext().getObjectFileInfo()->getBSSSection());
137   EmitCodeAlignment(4);
138
139   SwitchSection(getContext().getObjectFileInfo()->getTextSection());
140 }
141
142 void WinCOFFStreamer::EmitLabel(MCSymbol *Symbol) {
143   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
144   MCObjectStreamer::EmitLabel(Symbol);
145 }
146
147 void WinCOFFStreamer::EmitDebugLabel(MCSymbol *Symbol) {
148   EmitLabel(Symbol);
149 }
150 void WinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
151   llvm_unreachable("not implemented");
152 }
153
154 void WinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
155   llvm_unreachable("not implemented");
156 }
157
158 bool WinCOFFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
159                                           MCSymbolAttr Attribute) {
160   assert(Symbol && "Symbol must be non-null!");
161   assert((Symbol->isInSection()
162          ? Symbol->getSection().getVariant() == MCSection::SV_COFF
163          : true) && "Got non-COFF section in the COFF backend!");
164   switch (Attribute) {
165   case MCSA_WeakReference:
166   case MCSA_Weak: {
167       MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
168       SD.modifyFlags(COFF::SF_WeakExternal, COFF::SF_WeakExternal);
169       SD.setExternal(true);
170     }
171     break;
172
173   case MCSA_Global:
174     getAssembler().getOrCreateSymbolData(*Symbol).setExternal(true);
175     break;
176
177   default:
178     return false;
179   }
180
181   return true;
182 }
183
184 void WinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
185   llvm_unreachable("not implemented");
186 }
187
188 void WinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
189   assert((Symbol->isInSection()
190          ? Symbol->getSection().getVariant() == MCSection::SV_COFF
191          : true) && "Got non-COFF section in the COFF backend!");
192   assert(CurSymbol == NULL && "EndCOFFSymbolDef must be called between calls "
193                               "to BeginCOFFSymbolDef!");
194   CurSymbol = Symbol;
195 }
196
197 void WinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
198   assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
199   assert((StorageClass & ~0xFF) == 0 && "StorageClass must only have data in "
200                                         "the first byte!");
201
202   getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
203     StorageClass << COFF::SF_ClassShift,
204     COFF::SF_ClassMask);
205 }
206
207 void WinCOFFStreamer::EmitCOFFSymbolType(int Type) {
208   assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
209   assert((Type & ~0xFFFF) == 0 && "Type must only have data in the first 2 "
210                                   "bytes");
211
212   getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
213     Type << COFF::SF_TypeShift,
214     COFF::SF_TypeMask);
215 }
216
217 void WinCOFFStreamer::EndCOFFSymbolDef() {
218   assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
219   CurSymbol = NULL;
220 }
221
222 void WinCOFFStreamer::EmitCOFFSectionIndex(MCSymbol const *Symbol) {
223   MCDataFragment *DF = getOrCreateDataFragment();
224   DF->getFixups().push_back(MCFixup::Create(
225       DF->getContents().size(), MCSymbolRefExpr::Create(Symbol, getContext()),
226       FK_SecRel_2));
227   DF->getContents().resize(DF->getContents().size() + 4, 0);
228 }
229
230 void WinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol) {
231   MCDataFragment *DF = getOrCreateDataFragment();
232   DF->getFixups().push_back(MCFixup::Create(
233       DF->getContents().size(), MCSymbolRefExpr::Create(Symbol, getContext()),
234       FK_SecRel_4));
235   DF->getContents().resize(DF->getContents().size() + 4, 0);
236 }
237
238 void WinCOFFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
239   llvm_unreachable("not implemented");
240 }
241
242 void WinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
243                                        unsigned ByteAlignment) {
244   assert((Symbol->isInSection()
245          ? Symbol->getSection().getVariant() == MCSection::SV_COFF
246          : true) && "Got non-COFF section in the COFF backend!");
247   AddCommonSymbol(Symbol, Size, ByteAlignment, true);
248 }
249
250 void WinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
251                                             unsigned ByteAlignment) {
252   assert((Symbol->isInSection()
253          ? Symbol->getSection().getVariant() == MCSection::SV_COFF
254          : true) && "Got non-COFF section in the COFF backend!");
255   AddCommonSymbol(Symbol, Size, ByteAlignment, false);
256 }
257
258 void WinCOFFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
259                                    uint64_t Size,unsigned ByteAlignment) {
260   llvm_unreachable("not implemented");
261 }
262
263 void WinCOFFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
264                                      uint64_t Size, unsigned ByteAlignment) {
265   llvm_unreachable("not implemented");
266 }
267
268 void WinCOFFStreamer::EmitFileDirective(StringRef Filename) {
269   // Ignore for now, linkers don't care, and proper debug
270   // info will be a much large effort.
271 }
272
273 // TODO: Implement this if you want to emit .comment section in COFF obj files.
274 void WinCOFFStreamer::EmitIdent(StringRef IdentString) {
275   llvm_unreachable("unsupported directive");
276 }
277
278 void WinCOFFStreamer::EmitWin64EHHandlerData() {
279   MCStreamer::EmitWin64EHHandlerData();
280
281   // We have to emit the unwind info now, because this directive
282   // actually switches to the .xdata section!
283   MCWin64EHUnwindEmitter::EmitUnwindInfo(*this, getCurrentW64UnwindInfo());
284 }
285
286 void WinCOFFStreamer::FinishImpl() {
287   EmitFrames(NULL, true);
288   EmitW64Tables();
289   MCObjectStreamer::FinishImpl();
290 }
291
292 namespace llvm
293 {
294   MCStreamer *createWinCOFFStreamer(MCContext &Context,
295                                     MCAsmBackend &MAB,
296                                     MCCodeEmitter &CE,
297                                     raw_ostream &OS,
298                                     bool RelaxAll) {
299     WinCOFFStreamer *S = new WinCOFFStreamer(Context, MAB, CE, OS);
300     S->getAssembler().setRelaxAll(RelaxAll);
301     return S;
302   }
303 }