Added select flavour for ABS and NEG(ABS)
[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 Windows COFF object file streamer.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/ADT/StringExtras.h"
15 #include "llvm/MC/MCStreamer.h"
16 #include "llvm/MC/MCAsmBackend.h"
17 #include "llvm/MC/MCAsmLayout.h"
18 #include "llvm/MC/MCAssembler.h"
19 #include "llvm/MC/MCCodeEmitter.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCObjectFileInfo.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/MC/MCWinCOFFStreamer.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 #define DEBUG_TYPE "WinCOFFStreamer"
39
40 namespace llvm {
41 MCWinCOFFStreamer::MCWinCOFFStreamer(MCContext &Context, MCAsmBackend &MAB,
42                                      MCCodeEmitter &CE, raw_ostream &OS)
43     : MCObjectStreamer(Context, MAB, OS, &CE), CurSymbol(nullptr) {}
44
45 void MCWinCOFFStreamer::EmitInstToData(const MCInst &Inst,
46                                        const MCSubtargetInfo &STI) {
47   MCDataFragment *DF = getOrCreateDataFragment();
48
49   SmallVector<MCFixup, 4> Fixups;
50   SmallString<256> Code;
51   raw_svector_ostream VecOS(Code);
52   getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups, STI);
53   VecOS.flush();
54
55   // Add the fixups and data.
56   for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
57     Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
58     DF->getFixups().push_back(Fixups[i]);
59   }
60
61   DF->getContents().append(Code.begin(), Code.end());
62 }
63
64 void MCWinCOFFStreamer::InitSections() {
65   // FIXME: this is identical to the ELF one.
66   // This emulates the same behavior of GNU as. This makes it easier
67   // to compare the output as the major sections are in the same order.
68   SwitchSection(getContext().getObjectFileInfo()->getTextSection());
69   EmitCodeAlignment(4);
70
71   SwitchSection(getContext().getObjectFileInfo()->getDataSection());
72   EmitCodeAlignment(4);
73
74   SwitchSection(getContext().getObjectFileInfo()->getBSSSection());
75   EmitCodeAlignment(4);
76
77   SwitchSection(getContext().getObjectFileInfo()->getTextSection());
78 }
79
80 void MCWinCOFFStreamer::EmitLabel(MCSymbol *Symbol) {
81   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
82   MCObjectStreamer::EmitLabel(Symbol);
83 }
84
85 void MCWinCOFFStreamer::EmitDebugLabel(MCSymbol *Symbol) {
86   EmitLabel(Symbol);
87 }
88
89 void MCWinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
90   llvm_unreachable("not implemented");
91 }
92
93 void MCWinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
94   llvm_unreachable("not implemented");
95 }
96
97 bool MCWinCOFFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
98                                             MCSymbolAttr Attribute) {
99   assert(Symbol && "Symbol must be non-null!");
100   assert((!Symbol->isInSection() ||
101           Symbol->getSection().getVariant() == MCSection::SV_COFF) &&
102          "Got non-COFF section in the COFF backend!");
103
104   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
105
106   switch (Attribute) {
107   default: return false;
108   case MCSA_WeakReference:
109   case MCSA_Weak:
110     SD.modifyFlags(COFF::SF_WeakExternal, COFF::SF_WeakExternal);
111     SD.setExternal(true);
112     break;
113   case MCSA_Global:
114     SD.setExternal(true);
115     break;
116   }
117
118   return true;
119 }
120
121 void MCWinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
122   llvm_unreachable("not implemented");
123 }
124
125 void MCWinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
126   assert((!Symbol->isInSection() ||
127           Symbol->getSection().getVariant() == MCSection::SV_COFF) &&
128          "Got non-COFF section in the COFF backend!");
129
130   if (CurSymbol)
131     FatalError("starting a new symbol definition without completing the "
132                "previous one");
133   CurSymbol = Symbol;
134 }
135
136 void MCWinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
137   if (!CurSymbol)
138     FatalError("storage class specified outside of symbol definition");
139
140   if (StorageClass & ~0xff)
141     FatalError(Twine("storage class value '") + itostr(StorageClass) +
142                "' out of range");
143
144   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*CurSymbol);
145   SD.modifyFlags(StorageClass << COFF::SF_ClassShift, COFF::SF_ClassMask);
146 }
147
148 void MCWinCOFFStreamer::EmitCOFFSymbolType(int Type) {
149   if (!CurSymbol)
150     FatalError("symbol type specified outside of a symbol definition");
151
152   if (Type & ~0xffff)
153     FatalError(Twine("type value '") + itostr(Type) + "' out of range");
154
155   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*CurSymbol);
156   SD.modifyFlags(Type << COFF::SF_TypeShift, COFF::SF_TypeMask);
157 }
158
159 void MCWinCOFFStreamer::EndCOFFSymbolDef() {
160   if (!CurSymbol)
161     FatalError("ending symbol definition without starting one");
162   CurSymbol = nullptr;
163 }
164
165 void MCWinCOFFStreamer::EmitCOFFSectionIndex(MCSymbol const *Symbol) {
166   MCDataFragment *DF = getOrCreateDataFragment();
167   const MCSymbolRefExpr *SRE = MCSymbolRefExpr::Create(Symbol, getContext());
168   MCFixup Fixup = MCFixup::Create(DF->getContents().size(), SRE, FK_SecRel_2);
169   DF->getFixups().push_back(Fixup);
170   DF->getContents().resize(DF->getContents().size() + 4, 0);
171 }
172
173 void MCWinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol) {
174   MCDataFragment *DF = getOrCreateDataFragment();
175   const MCSymbolRefExpr *SRE = MCSymbolRefExpr::Create(Symbol, getContext());
176   MCFixup Fixup = MCFixup::Create(DF->getContents().size(), SRE, FK_SecRel_4);
177   DF->getFixups().push_back(Fixup);
178   DF->getContents().resize(DF->getContents().size() + 4, 0);
179 }
180
181 void MCWinCOFFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
182   llvm_unreachable("not supported");
183 }
184
185 void MCWinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
186                                          unsigned ByteAlignment) {
187   assert((!Symbol->isInSection() ||
188           Symbol->getSection().getVariant() == MCSection::SV_COFF) &&
189          "Got non-COFF section in the COFF backend!");
190
191   if (ByteAlignment > 32)
192     report_fatal_error("alignment is limited to 32-bytes");
193
194   AssignSection(Symbol, nullptr);
195
196   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
197   SD.setExternal(true);
198   SD.setCommon(Size, ByteAlignment);
199 }
200
201 void MCWinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
202                                               unsigned ByteAlignment) {
203   assert(!Symbol->isInSection() && "Symbol must not already have a section!");
204
205   const MCSection *Section = getContext().getObjectFileInfo()->getBSSSection();
206   MCSectionData &SectionData = getAssembler().getOrCreateSectionData(*Section);
207   if (SectionData.getAlignment() < ByteAlignment)
208     SectionData.setAlignment(ByteAlignment);
209
210   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
211   SD.setExternal(false);
212
213   AssignSection(Symbol, Section);
214
215   if (ByteAlignment != 1)
216     new MCAlignFragment(ByteAlignment, /*_Value=*/0, /*_ValueSize=*/0,
217                         ByteAlignment, &SectionData);
218
219   MCFillFragment *Fragment =
220       new MCFillFragment(/*_Value=*/0, /*_ValueSize=*/0, Size, &SectionData);
221   SD.setFragment(Fragment);
222 }
223
224 void MCWinCOFFStreamer::EmitZerofill(const MCSection *Section,
225                                      MCSymbol *Symbol, uint64_t Size,
226                                      unsigned ByteAlignment) {
227   llvm_unreachable("not implemented");
228 }
229
230 void MCWinCOFFStreamer::EmitTBSSSymbol(const MCSection *Section,
231                                        MCSymbol *Symbol, uint64_t Size,
232                                        unsigned ByteAlignment) {
233   llvm_unreachable("not implemented");
234 }
235
236 void MCWinCOFFStreamer::EmitFileDirective(StringRef Filename) {
237   getAssembler().addFileName(Filename);
238 }
239
240 // TODO: Implement this if you want to emit .comment section in COFF obj files.
241 void MCWinCOFFStreamer::EmitIdent(StringRef IdentString) {
242   llvm_unreachable("not implemented");
243 }
244
245 void MCWinCOFFStreamer::EmitWin64EHHandlerData() {
246   llvm_unreachable("not implemented");
247 }
248
249 void MCWinCOFFStreamer::FinishImpl() {
250   MCObjectStreamer::FinishImpl();
251 }
252
253 LLVM_ATTRIBUTE_NORETURN
254 void MCWinCOFFStreamer::FatalError(const Twine &Msg) const {
255   getContext().FatalError(SMLoc(), Msg);
256 }
257 }
258