[cleanup] Re-sort all the includes with utils/sort_includes.py.
[oota-llvm.git] / lib / Target / Mips / MCTargetDesc / MipsTargetStreamer.cpp
1 //===-- MipsTargetStreamer.cpp - Mips Target Streamer Methods -------------===//
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 provides Mips specific target streamer methods.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "InstPrinter/MipsInstPrinter.h"
15 #include "MipsMCTargetDesc.h"
16 #include "MipsTargetObjectFile.h"
17 #include "MipsTargetStreamer.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCELF.h"
20 #include "llvm/MC/MCSectionELF.h"
21 #include "llvm/MC/MCSubtargetInfo.h"
22 #include "llvm/MC/MCSymbol.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/ELF.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/FormattedStream.h"
27
28 using namespace llvm;
29
30 // Pin vtable to this file.
31 void MipsTargetStreamer::anchor() {}
32
33 MipsTargetStreamer::MipsTargetStreamer(MCStreamer &S) : MCTargetStreamer(S) {}
34
35 MipsTargetAsmStreamer::MipsTargetAsmStreamer(MCStreamer &S,
36                                              formatted_raw_ostream &OS)
37     : MipsTargetStreamer(S), OS(OS) {}
38
39 void MipsTargetAsmStreamer::emitDirectiveSetMicroMips() {
40   OS << "\t.set\tmicromips\n";
41 }
42
43 void MipsTargetAsmStreamer::emitDirectiveSetNoMicroMips() {
44   OS << "\t.set\tnomicromips\n";
45 }
46
47 void MipsTargetAsmStreamer::emitDirectiveSetMips16() {
48   OS << "\t.set\tmips16\n";
49 }
50
51 void MipsTargetAsmStreamer::emitDirectiveSetNoMips16() {
52   OS << "\t.set\tnomips16\n";
53 }
54
55 void MipsTargetAsmStreamer::emitDirectiveSetReorder() {
56   OS << "\t.set\treorder\n";
57 }
58
59 void MipsTargetAsmStreamer::emitDirectiveSetNoReorder() {
60   OS << "\t.set\tnoreorder\n";
61 }
62
63 void MipsTargetAsmStreamer::emitDirectiveSetMacro() {
64   OS << "\t.set\tmacro\n";
65 }
66
67 void MipsTargetAsmStreamer::emitDirectiveSetNoMacro() {
68   OS << "\t.set\tnomacro\n";
69 }
70
71 void MipsTargetAsmStreamer::emitDirectiveSetAt() {
72   OS << "\t.set\tat\n";
73 }
74
75 void MipsTargetAsmStreamer::emitDirectiveSetNoAt() {
76   OS << "\t.set\tnoat\n";
77 }
78
79 void MipsTargetAsmStreamer::emitDirectiveEnd(StringRef Name) {
80   OS << "\t.end\t" << Name << '\n';
81 }
82
83 void MipsTargetAsmStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {
84   OS << "\t.ent\t" << Symbol.getName() << '\n';
85 }
86
87 void MipsTargetAsmStreamer::emitDirectiveAbiCalls() { OS << "\t.abicalls\n"; }
88 void MipsTargetAsmStreamer::emitDirectiveOptionPic0() {
89   OS << "\t.option\tpic0\n";
90 }
91
92 void MipsTargetAsmStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
93                                       unsigned ReturnReg) {
94   OS << "\t.frame\t$"
95      << StringRef(MipsInstPrinter::getRegisterName(StackReg)).lower() << ","
96      << StackSize << ",$"
97      << StringRef(MipsInstPrinter::getRegisterName(ReturnReg)).lower() << '\n';
98 }
99
100 void MipsTargetAsmStreamer::emitDirectiveSetMips32R2() {
101   OS << "\t.set\tmips32r2\n";
102 }
103
104 // Print a 32 bit hex number with all numbers.
105 static void printHex32(unsigned Value, raw_ostream &OS) {
106   OS << "0x";
107   for (int i = 7; i >= 0; i--)
108     OS.write_hex((Value & (0xF << (i*4))) >> (i*4));
109 }
110
111 void MipsTargetAsmStreamer::emitMask(unsigned CPUBitmask,
112                                      int CPUTopSavedRegOff) {
113   OS << "\t.mask \t";
114   printHex32(CPUBitmask, OS);
115   OS << ',' << CPUTopSavedRegOff << '\n';
116 }
117
118 void MipsTargetAsmStreamer::emitFMask(unsigned FPUBitmask,
119                                       int FPUTopSavedRegOff) {
120   OS << "\t.fmask\t";
121   printHex32(FPUBitmask, OS);
122   OS << "," << FPUTopSavedRegOff << '\n';
123 }
124
125 // This part is for ELF object output.
126 MipsTargetELFStreamer::MipsTargetELFStreamer(MCStreamer &S,
127                                              const MCSubtargetInfo &STI)
128     : MipsTargetStreamer(S), MicroMipsEnabled(false), STI(STI) {
129   MCAssembler &MCA = getStreamer().getAssembler();
130   uint64_t Features = STI.getFeatureBits();
131   Triple T(STI.getTargetTriple());
132
133   // Update e_header flags
134   unsigned EFlags = 0;
135
136   // Architecture
137   if (Features & Mips::FeatureMips64r2)
138     EFlags |= ELF::EF_MIPS_ARCH_64R2;
139   else if (Features & Mips::FeatureMips64)
140     EFlags |= ELF::EF_MIPS_ARCH_64;
141   else if (Features & Mips::FeatureMips32r2)
142     EFlags |= ELF::EF_MIPS_ARCH_32R2;
143   else if (Features & Mips::FeatureMips32)
144     EFlags |= ELF::EF_MIPS_ARCH_32;
145
146   if (T.isArch64Bit()) {
147     if (Features & Mips::FeatureN32)
148       EFlags |= ELF::EF_MIPS_ABI2;
149     else if (Features & Mips::FeatureO32) {
150       EFlags |= ELF::EF_MIPS_ABI_O32;
151       EFlags |= ELF::EF_MIPS_32BITMODE; /* Compatibility Mode */
152     }
153     // No need to set any bit for N64 which is the default ABI at the moment
154     // for 64-bit Mips architectures.
155   } else {
156     if (Features & Mips::FeatureMips64r2 || Features & Mips::FeatureMips64)
157       EFlags |= ELF::EF_MIPS_32BITMODE;
158
159     // ABI
160     EFlags |= ELF::EF_MIPS_ABI_O32;
161   }
162
163   MCA.setELFHeaderEFlags(EFlags);
164 }
165
166 void MipsTargetELFStreamer::emitLabel(MCSymbol *Symbol) {
167   if (!isMicroMipsEnabled())
168     return;
169   MCSymbolData &Data = getStreamer().getOrCreateSymbolData(Symbol);
170   uint8_t Type = MCELF::GetType(Data);
171   if (Type != ELF::STT_FUNC)
172     return;
173
174   // The "other" values are stored in the last 6 bits of the second byte
175   // The traditional defines for STO values assume the full byte and thus
176   // the shift to pack it.
177   MCELF::setOther(Data, ELF::STO_MIPS_MICROMIPS >> 2);
178 }
179
180 void MipsTargetELFStreamer::finish() {
181   MCAssembler &MCA = getStreamer().getAssembler();
182   MCContext &Context = MCA.getContext();
183   MCStreamer &OS = getStreamer();
184   Triple T(STI.getTargetTriple());
185   uint64_t Features = STI.getFeatureBits();
186
187   if (T.isArch64Bit() && (Features & Mips::FeatureN64)) {
188     const MCSectionELF *Sec = Context.getELFSection(
189         ".MIPS.options", ELF::SHT_MIPS_OPTIONS,
190         ELF::SHF_ALLOC | ELF::SHF_MIPS_NOSTRIP, SectionKind::getMetadata());
191     OS.SwitchSection(Sec);
192
193     OS.EmitIntValue(1, 1); // kind
194     OS.EmitIntValue(40, 1); // size
195     OS.EmitIntValue(0, 2); // section
196     OS.EmitIntValue(0, 4); // info
197     OS.EmitIntValue(0, 4); // ri_gprmask
198     OS.EmitIntValue(0, 4); // pad
199     OS.EmitIntValue(0, 4); // ri_cpr[0]mask
200     OS.EmitIntValue(0, 4); // ri_cpr[1]mask
201     OS.EmitIntValue(0, 4); // ri_cpr[2]mask
202     OS.EmitIntValue(0, 4); // ri_cpr[3]mask
203     OS.EmitIntValue(0, 8); // ri_gp_value
204   } else {
205     const MCSectionELF *Sec =
206         Context.getELFSection(".reginfo", ELF::SHT_MIPS_REGINFO, ELF::SHF_ALLOC,
207                               SectionKind::getMetadata());
208     OS.SwitchSection(Sec);
209
210     OS.EmitIntValue(0, 4); // ri_gprmask
211     OS.EmitIntValue(0, 4); // ri_cpr[0]mask
212     OS.EmitIntValue(0, 4); // ri_cpr[1]mask
213     OS.EmitIntValue(0, 4); // ri_cpr[2]mask
214     OS.EmitIntValue(0, 4); // ri_cpr[3]mask
215     OS.EmitIntValue(0, 4); // ri_gp_value
216   }
217 }
218
219 MCELFStreamer &MipsTargetELFStreamer::getStreamer() {
220   return static_cast<MCELFStreamer &>(Streamer);
221 }
222
223 void MipsTargetELFStreamer::emitDirectiveSetMicroMips() {
224   MicroMipsEnabled = true;
225
226   MCAssembler &MCA = getStreamer().getAssembler();
227   unsigned Flags = MCA.getELFHeaderEFlags();
228   Flags |= ELF::EF_MIPS_MICROMIPS;
229   MCA.setELFHeaderEFlags(Flags);
230 }
231
232 void MipsTargetELFStreamer::emitDirectiveSetNoMicroMips() {
233   MicroMipsEnabled = false;
234 }
235
236 void MipsTargetELFStreamer::emitDirectiveSetMips16() {
237   MCAssembler &MCA = getStreamer().getAssembler();
238   unsigned Flags = MCA.getELFHeaderEFlags();
239   Flags |= ELF::EF_MIPS_ARCH_ASE_M16;
240   MCA.setELFHeaderEFlags(Flags);
241 }
242
243 void MipsTargetELFStreamer::emitDirectiveSetNoMips16() {
244   // FIXME: implement.
245 }
246
247 void MipsTargetELFStreamer::emitDirectiveSetReorder() {
248   // FIXME: implement.
249 }
250
251 void MipsTargetELFStreamer::emitDirectiveSetNoReorder() {
252   MCAssembler &MCA = getStreamer().getAssembler();
253   unsigned Flags = MCA.getELFHeaderEFlags();
254   Flags |= ELF::EF_MIPS_NOREORDER;
255   MCA.setELFHeaderEFlags(Flags);
256 }
257
258 void MipsTargetELFStreamer::emitDirectiveSetMacro() {
259   // FIXME: implement.
260 }
261
262 void MipsTargetELFStreamer::emitDirectiveSetNoMacro() {
263   // FIXME: implement.
264 }
265
266 void MipsTargetELFStreamer::emitDirectiveSetAt() {
267   // FIXME: implement.
268 }
269
270 void MipsTargetELFStreamer::emitDirectiveSetNoAt() {
271   // FIXME: implement.
272 }
273
274 void MipsTargetELFStreamer::emitDirectiveEnd(StringRef Name) {
275   // FIXME: implement.
276 }
277
278 void MipsTargetELFStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {
279   // FIXME: implement.
280 }
281
282 void MipsTargetELFStreamer::emitDirectiveAbiCalls() {
283   MCAssembler &MCA = getStreamer().getAssembler();
284   unsigned Flags = MCA.getELFHeaderEFlags();
285   Flags |= ELF::EF_MIPS_CPIC | ELF::EF_MIPS_PIC;
286   MCA.setELFHeaderEFlags(Flags);
287 }
288 void MipsTargetELFStreamer::emitDirectiveOptionPic0() {
289   MCAssembler &MCA = getStreamer().getAssembler();
290   unsigned Flags = MCA.getELFHeaderEFlags();
291   Flags &= ~ELF::EF_MIPS_PIC;
292   MCA.setELFHeaderEFlags(Flags);
293 }
294
295 void MipsTargetELFStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
296                                       unsigned ReturnReg) {
297   // FIXME: implement.
298 }
299
300 void MipsTargetELFStreamer::emitMask(unsigned CPUBitmask,
301                                      int CPUTopSavedRegOff) {
302   // FIXME: implement.
303 }
304
305 void MipsTargetELFStreamer::emitFMask(unsigned FPUBitmask,
306                                       int FPUTopSavedRegOff) {
307   // FIXME: implement.
308 }
309
310 void MipsTargetELFStreamer::emitDirectiveSetMips32R2() {
311   // No action required for ELF output.
312 }