Remove some really nasty uses of hasRawTextSupport.
[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/MCObjectStreamer.h"
24 #include "llvm/MC/MCSection.h"
25 #include "llvm/MC/MCSectionCOFF.h"
26 #include "llvm/MC/MCSymbol.h"
27 #include "llvm/MC/MCValue.h"
28 #include "llvm/MC/MCWin64EH.h"
29 #include "llvm/Support/COFF.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/TargetRegistry.h"
33 #include "llvm/Support/raw_ostream.h"
34
35 using namespace llvm;
36
37 namespace {
38 class WinCOFFStreamer : public MCObjectStreamer {
39 public:
40   MCSymbol const *CurSymbol;
41
42   WinCOFFStreamer(MCContext &Context,
43                   MCAsmBackend &MAB,
44                   MCCodeEmitter &CE,
45                   raw_ostream &OS);
46
47   void AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
48                        unsigned ByteAlignment, bool External);
49
50   // MCStreamer interface
51
52   virtual void InitSections();
53   virtual void InitToTextSection();
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 EmitCOFFSecRel32(MCSymbol const *Symbol);
65   virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
66   virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
67                                 unsigned ByteAlignment);
68   virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
69                                      unsigned ByteAlignment);
70   virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
71                             uint64_t Size,unsigned ByteAlignment);
72   virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
73                               uint64_t Size, unsigned ByteAlignment);
74   virtual void EmitFileDirective(StringRef Filename);
75   virtual void EmitWin64EHHandlerData();
76   virtual void FinishImpl();
77
78 private:
79   virtual void EmitInstToData(const MCInst &Inst) {
80     MCDataFragment *DF = getOrCreateDataFragment();
81
82     SmallVector<MCFixup, 4> Fixups;
83     SmallString<256> Code;
84     raw_svector_ostream VecOS(Code);
85     getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
86     VecOS.flush();
87
88     // Add the fixups and data.
89     for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
90       Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
91       DF->getFixups().push_back(Fixups[i]);
92     }
93     DF->getContents().append(Code.begin(), Code.end());
94   }
95
96   void SetSection(StringRef Section,
97                   unsigned Characteristics,
98                   SectionKind Kind) {
99     SwitchSection(getContext().getCOFFSection(Section, Characteristics, Kind));
100   }
101
102   void SetSectionText() {
103     SetSection(".text",
104                COFF::IMAGE_SCN_CNT_CODE
105              | COFF::IMAGE_SCN_MEM_EXECUTE
106              | COFF::IMAGE_SCN_MEM_READ,
107                SectionKind::getText());
108     EmitCodeAlignment(4, 0);
109   }
110
111   void SetSectionData() {
112     SetSection(".data",
113                COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
114              | COFF::IMAGE_SCN_MEM_READ
115              | COFF::IMAGE_SCN_MEM_WRITE,
116                SectionKind::getDataRel());
117     EmitCodeAlignment(4, 0);
118   }
119
120   void SetSectionBSS() {
121     SetSection(".bss",
122                COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
123              | COFF::IMAGE_SCN_MEM_READ
124              | COFF::IMAGE_SCN_MEM_WRITE,
125                SectionKind::getBSS());
126     EmitCodeAlignment(4, 0);
127   }
128 };
129 } // end anonymous namespace.
130
131 WinCOFFStreamer::WinCOFFStreamer(MCContext &Context, MCAsmBackend &MAB,
132                                  MCCodeEmitter &CE, raw_ostream &OS)
133     : MCObjectStreamer(Context, MAB, OS, &CE), CurSymbol(NULL) {}
134
135 void WinCOFFStreamer::AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
136                                       unsigned ByteAlignment, bool External) {
137   assert(!Symbol->isInSection() && "Symbol must not already have a section!");
138
139   std::string SectionName(".bss$linkonce");
140   SectionName.append(Symbol->getName().begin(), Symbol->getName().end());
141
142   MCSymbolData &SymbolData = getAssembler().getOrCreateSymbolData(*Symbol);
143
144   unsigned Characteristics =
145     COFF::IMAGE_SCN_LNK_COMDAT |
146     COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
147     COFF::IMAGE_SCN_MEM_READ |
148     COFF::IMAGE_SCN_MEM_WRITE;
149
150   int Selection = COFF::IMAGE_COMDAT_SELECT_LARGEST;
151
152   const MCSection *Section = MCStreamer::getContext().getCOFFSection(
153     SectionName, Characteristics, SectionKind::getBSS(), Selection);
154
155   MCSectionData &SectionData = getAssembler().getOrCreateSectionData(*Section);
156
157   if (SectionData.getAlignment() < ByteAlignment)
158     SectionData.setAlignment(ByteAlignment);
159
160   SymbolData.setExternal(External);
161
162   AssignSection(Symbol, Section);
163
164   if (ByteAlignment != 1)
165       new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectionData);
166
167   SymbolData.setFragment(new MCFillFragment(0, 0, Size, &SectionData));
168 }
169
170 // MCStreamer interface
171
172 void WinCOFFStreamer::InitToTextSection() {
173   SetSectionText();
174 }
175
176 void WinCOFFStreamer::InitSections() {
177   SetSectionText();
178   SetSectionData();
179   SetSectionBSS();
180   SetSectionText();
181 }
182
183 void WinCOFFStreamer::EmitLabel(MCSymbol *Symbol) {
184   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
185   MCObjectStreamer::EmitLabel(Symbol);
186 }
187
188 void WinCOFFStreamer::EmitDebugLabel(MCSymbol *Symbol) {
189   EmitLabel(Symbol);
190 }
191 void WinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
192   llvm_unreachable("not implemented");
193 }
194
195 void WinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
196   llvm_unreachable("not implemented");
197 }
198
199 bool WinCOFFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
200                                           MCSymbolAttr Attribute) {
201   assert(Symbol && "Symbol must be non-null!");
202   assert((Symbol->isInSection()
203          ? Symbol->getSection().getVariant() == MCSection::SV_COFF
204          : true) && "Got non COFF section in the COFF backend!");
205   switch (Attribute) {
206   case MCSA_WeakReference:
207   case MCSA_Weak: {
208       MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
209       SD.modifyFlags(COFF::SF_WeakExternal, COFF::SF_WeakExternal);
210       SD.setExternal(true);
211     }
212     break;
213
214   case MCSA_Global:
215     getAssembler().getOrCreateSymbolData(*Symbol).setExternal(true);
216     break;
217
218   default:
219     return false;
220   }
221
222   return true;
223 }
224
225 void WinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
226   llvm_unreachable("not implemented");
227 }
228
229 void WinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
230   assert((Symbol->isInSection()
231          ? Symbol->getSection().getVariant() == MCSection::SV_COFF
232          : true) && "Got non COFF section in the COFF backend!");
233   assert(CurSymbol == NULL && "EndCOFFSymbolDef must be called between calls "
234                               "to BeginCOFFSymbolDef!");
235   CurSymbol = Symbol;
236 }
237
238 void WinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
239   assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
240   assert((StorageClass & ~0xFF) == 0 && "StorageClass must only have data in "
241                                         "the first byte!");
242
243   getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
244     StorageClass << COFF::SF_ClassShift,
245     COFF::SF_ClassMask);
246 }
247
248 void WinCOFFStreamer::EmitCOFFSymbolType(int Type) {
249   assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
250   assert((Type & ~0xFFFF) == 0 && "Type must only have data in the first 2 "
251                                   "bytes");
252
253   getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
254     Type << COFF::SF_TypeShift,
255     COFF::SF_TypeMask);
256 }
257
258 void WinCOFFStreamer::EndCOFFSymbolDef() {
259   assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
260   CurSymbol = NULL;
261 }
262
263 void WinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol)
264 {
265   MCDataFragment *DF = getOrCreateDataFragment();
266
267   DF->getFixups().push_back(
268       MCFixup::Create(DF->getContents().size(),
269                       MCSymbolRefExpr::Create (Symbol, getContext ()),
270                       FK_SecRel_4));
271   DF->getContents().resize(DF->getContents().size() + 4, 0);
272 }
273
274 void WinCOFFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
275   llvm_unreachable("not implemented");
276 }
277
278 void WinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
279                                        unsigned ByteAlignment) {
280   assert((Symbol->isInSection()
281          ? Symbol->getSection().getVariant() == MCSection::SV_COFF
282          : true) && "Got non COFF section in the COFF backend!");
283   AddCommonSymbol(Symbol, Size, ByteAlignment, true);
284 }
285
286 void WinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
287                                             unsigned ByteAlignment) {
288   assert((Symbol->isInSection()
289          ? Symbol->getSection().getVariant() == MCSection::SV_COFF
290          : true) && "Got non COFF section in the COFF backend!");
291   AddCommonSymbol(Symbol, Size, ByteAlignment, false);
292 }
293
294 void WinCOFFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
295                                    uint64_t Size,unsigned ByteAlignment) {
296   llvm_unreachable("not implemented");
297 }
298
299 void WinCOFFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
300                                      uint64_t Size, unsigned ByteAlignment) {
301   llvm_unreachable("not implemented");
302 }
303
304 void WinCOFFStreamer::EmitFileDirective(StringRef Filename) {
305   // Ignore for now, linkers don't care, and proper debug
306   // info will be a much large effort.
307 }
308
309 void WinCOFFStreamer::EmitWin64EHHandlerData() {
310   MCStreamer::EmitWin64EHHandlerData();
311
312   // We have to emit the unwind info now, because this directive
313   // actually switches to the .xdata section!
314   MCWin64EHUnwindEmitter::EmitUnwindInfo(*this, getCurrentW64UnwindInfo());
315 }
316
317 void WinCOFFStreamer::FinishImpl() {
318   EmitW64Tables();
319   MCObjectStreamer::FinishImpl();
320 }
321
322 namespace llvm
323 {
324   MCStreamer *createWinCOFFStreamer(MCContext &Context,
325                                     MCAsmBackend &MAB,
326                                     MCCodeEmitter &CE,
327                                     raw_ostream &OS,
328                                     bool RelaxAll) {
329     WinCOFFStreamer *S = new WinCOFFStreamer(Context, MAB, CE, OS);
330     S->getAssembler().setRelaxAll(RelaxAll);
331     return S;
332   }
333 }