[mips] Add support to '.set mips64r2'.
[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::emitDirectiveOptionPic2() {
93   OS << "\t.option\tpic2\n";
94 }
95
96 void MipsTargetAsmStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
97                                       unsigned ReturnReg) {
98   OS << "\t.frame\t$"
99      << StringRef(MipsInstPrinter::getRegisterName(StackReg)).lower() << ","
100      << StackSize << ",$"
101      << StringRef(MipsInstPrinter::getRegisterName(ReturnReg)).lower() << '\n';
102 }
103
104 void MipsTargetAsmStreamer::emitDirectiveSetMips32R2() {
105   OS << "\t.set\tmips32r2\n";
106 }
107
108 void MipsTargetAsmStreamer::emitDirectiveSetMips64R2() {
109   OS << "\t.set\tmips64r2\n";
110 }
111
112 void MipsTargetAsmStreamer::emitDirectiveSetDsp() {
113   OS << "\t.set\tdsp\n";
114 }
115 // Print a 32 bit hex number with all numbers.
116 static void printHex32(unsigned Value, raw_ostream &OS) {
117   OS << "0x";
118   for (int i = 7; i >= 0; i--)
119     OS.write_hex((Value & (0xF << (i*4))) >> (i*4));
120 }
121
122 void MipsTargetAsmStreamer::emitMask(unsigned CPUBitmask,
123                                      int CPUTopSavedRegOff) {
124   OS << "\t.mask \t";
125   printHex32(CPUBitmask, OS);
126   OS << ',' << CPUTopSavedRegOff << '\n';
127 }
128
129 void MipsTargetAsmStreamer::emitFMask(unsigned FPUBitmask,
130                                       int FPUTopSavedRegOff) {
131   OS << "\t.fmask\t";
132   printHex32(FPUBitmask, OS);
133   OS << "," << FPUTopSavedRegOff << '\n';
134 }
135
136 // This part is for ELF object output.
137 MipsTargetELFStreamer::MipsTargetELFStreamer(MCStreamer &S,
138                                              const MCSubtargetInfo &STI)
139     : MipsTargetStreamer(S), MicroMipsEnabled(false), STI(STI) {
140   MCAssembler &MCA = getStreamer().getAssembler();
141   uint64_t Features = STI.getFeatureBits();
142   Triple T(STI.getTargetTriple());
143   Pic = (MCA.getContext().getObjectFileInfo()->getRelocM() ==  Reloc::PIC_)
144             ? true
145             : false;
146
147   // Update e_header flags
148   unsigned EFlags = 0;
149
150   // Architecture
151   if (Features & Mips::FeatureMips64r2)
152     EFlags |= ELF::EF_MIPS_ARCH_64R2;
153   else if (Features & Mips::FeatureMips64)
154     EFlags |= ELF::EF_MIPS_ARCH_64;
155   else if (Features & Mips::FeatureMips32r2)
156     EFlags |= ELF::EF_MIPS_ARCH_32R2;
157   else if (Features & Mips::FeatureMips32)
158     EFlags |= ELF::EF_MIPS_ARCH_32;
159
160   if (T.isArch64Bit()) {
161     if (Features & Mips::FeatureN32)
162       EFlags |= ELF::EF_MIPS_ABI2;
163     else if (Features & Mips::FeatureO32) {
164       EFlags |= ELF::EF_MIPS_ABI_O32;
165       EFlags |= ELF::EF_MIPS_32BITMODE; /* Compatibility Mode */
166     }
167     // No need to set any bit for N64 which is the default ABI at the moment
168     // for 64-bit Mips architectures.
169   } else {
170     if (Features & Mips::FeatureMips64r2 || Features & Mips::FeatureMips64)
171       EFlags |= ELF::EF_MIPS_32BITMODE;
172
173     // ABI
174     EFlags |= ELF::EF_MIPS_ABI_O32;
175   }
176
177   MCA.setELFHeaderEFlags(EFlags);
178 }
179
180 void MipsTargetELFStreamer::emitLabel(MCSymbol *Symbol) {
181   if (!isMicroMipsEnabled())
182     return;
183   MCSymbolData &Data = getStreamer().getOrCreateSymbolData(Symbol);
184   uint8_t Type = MCELF::GetType(Data);
185   if (Type != ELF::STT_FUNC)
186     return;
187
188   // The "other" values are stored in the last 6 bits of the second byte
189   // The traditional defines for STO values assume the full byte and thus
190   // the shift to pack it.
191   MCELF::setOther(Data, ELF::STO_MIPS_MICROMIPS >> 2);
192 }
193
194 void MipsTargetELFStreamer::finish() {
195   MCAssembler &MCA = getStreamer().getAssembler();
196   MCContext &Context = MCA.getContext();
197   MCStreamer &OS = getStreamer();
198   Triple T(STI.getTargetTriple());
199   uint64_t Features = STI.getFeatureBits();
200
201   if (T.isArch64Bit() && (Features & Mips::FeatureN64)) {
202     const MCSectionELF *Sec = Context.getELFSection(
203         ".MIPS.options", ELF::SHT_MIPS_OPTIONS,
204         ELF::SHF_ALLOC | ELF::SHF_MIPS_NOSTRIP, SectionKind::getMetadata());
205     OS.SwitchSection(Sec);
206
207     OS.EmitIntValue(1, 1); // kind
208     OS.EmitIntValue(40, 1); // size
209     OS.EmitIntValue(0, 2); // section
210     OS.EmitIntValue(0, 4); // info
211     OS.EmitIntValue(0, 4); // ri_gprmask
212     OS.EmitIntValue(0, 4); // pad
213     OS.EmitIntValue(0, 4); // ri_cpr[0]mask
214     OS.EmitIntValue(0, 4); // ri_cpr[1]mask
215     OS.EmitIntValue(0, 4); // ri_cpr[2]mask
216     OS.EmitIntValue(0, 4); // ri_cpr[3]mask
217     OS.EmitIntValue(0, 8); // ri_gp_value
218   } else {
219     const MCSectionELF *Sec =
220         Context.getELFSection(".reginfo", ELF::SHT_MIPS_REGINFO, ELF::SHF_ALLOC,
221                               SectionKind::getMetadata());
222     OS.SwitchSection(Sec);
223
224     OS.EmitIntValue(0, 4); // ri_gprmask
225     OS.EmitIntValue(0, 4); // ri_cpr[0]mask
226     OS.EmitIntValue(0, 4); // ri_cpr[1]mask
227     OS.EmitIntValue(0, 4); // ri_cpr[2]mask
228     OS.EmitIntValue(0, 4); // ri_cpr[3]mask
229     OS.EmitIntValue(0, 4); // ri_gp_value
230   }
231 }
232
233 void MipsTargetELFStreamer::emitAssignment(MCSymbol *Symbol,
234                                            const MCExpr *Value) {
235   // If on rhs is micromips symbol then mark Symbol as microMips.
236   if (Value->getKind() != MCExpr::SymbolRef)
237     return;
238   const MCSymbol &RhsSym =
239     static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
240   MCSymbolData &Data = getStreamer().getOrCreateSymbolData(&RhsSym);
241   uint8_t Type = MCELF::GetType(Data);
242   if ((Type != ELF::STT_FUNC)
243       || !(MCELF::getOther(Data) & (ELF::STO_MIPS_MICROMIPS >> 2)))
244     return;
245
246   MCSymbolData &SymbolData = getStreamer().getOrCreateSymbolData(Symbol);
247   // The "other" values are stored in the last 6 bits of the second byte.
248   // The traditional defines for STO values assume the full byte and thus
249   // the shift to pack it.
250   MCELF::setOther(SymbolData, ELF::STO_MIPS_MICROMIPS >> 2);
251 }
252
253 MCELFStreamer &MipsTargetELFStreamer::getStreamer() {
254   return static_cast<MCELFStreamer &>(Streamer);
255 }
256
257 void MipsTargetELFStreamer::emitDirectiveSetMicroMips() {
258   MicroMipsEnabled = true;
259
260   MCAssembler &MCA = getStreamer().getAssembler();
261   unsigned Flags = MCA.getELFHeaderEFlags();
262   Flags |= ELF::EF_MIPS_MICROMIPS;
263   MCA.setELFHeaderEFlags(Flags);
264 }
265
266 void MipsTargetELFStreamer::emitDirectiveSetNoMicroMips() {
267   MicroMipsEnabled = false;
268 }
269
270 void MipsTargetELFStreamer::emitDirectiveSetMips16() {
271   MCAssembler &MCA = getStreamer().getAssembler();
272   unsigned Flags = MCA.getELFHeaderEFlags();
273   Flags |= ELF::EF_MIPS_ARCH_ASE_M16;
274   MCA.setELFHeaderEFlags(Flags);
275 }
276
277 void MipsTargetELFStreamer::emitDirectiveSetNoMips16() {
278   // FIXME: implement.
279 }
280
281 void MipsTargetELFStreamer::emitDirectiveSetReorder() {
282   // FIXME: implement.
283 }
284
285 void MipsTargetELFStreamer::emitDirectiveSetNoReorder() {
286   MCAssembler &MCA = getStreamer().getAssembler();
287   unsigned Flags = MCA.getELFHeaderEFlags();
288   Flags |= ELF::EF_MIPS_NOREORDER;
289   MCA.setELFHeaderEFlags(Flags);
290 }
291
292 void MipsTargetELFStreamer::emitDirectiveSetMacro() {
293   // FIXME: implement.
294 }
295
296 void MipsTargetELFStreamer::emitDirectiveSetNoMacro() {
297   // FIXME: implement.
298 }
299
300 void MipsTargetELFStreamer::emitDirectiveSetAt() {
301   // FIXME: implement.
302 }
303
304 void MipsTargetELFStreamer::emitDirectiveSetNoAt() {
305   // FIXME: implement.
306 }
307
308 void MipsTargetELFStreamer::emitDirectiveEnd(StringRef Name) {
309   // FIXME: implement.
310 }
311
312 void MipsTargetELFStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {
313   // FIXME: implement.
314 }
315
316 void MipsTargetELFStreamer::emitDirectiveAbiCalls() {
317   MCAssembler &MCA = getStreamer().getAssembler();
318   unsigned Flags = MCA.getELFHeaderEFlags();
319   Flags |= ELF::EF_MIPS_CPIC | ELF::EF_MIPS_PIC;
320   MCA.setELFHeaderEFlags(Flags);
321 }
322 void MipsTargetELFStreamer::emitDirectiveOptionPic0() {
323   MCAssembler &MCA = getStreamer().getAssembler();
324   unsigned Flags = MCA.getELFHeaderEFlags();
325   // This option overrides other PIC options like -KPIC.
326   Pic = false;
327   Flags &= ~ELF::EF_MIPS_PIC;
328   MCA.setELFHeaderEFlags(Flags);
329 }
330
331 void MipsTargetELFStreamer::emitDirectiveOptionPic2() {
332   MCAssembler &MCA = getStreamer().getAssembler();
333   unsigned Flags = MCA.getELFHeaderEFlags();
334   Pic = true;
335   // NOTE: We are following the GAS behaviour here which means the directive
336   // 'pic2' also sets the CPIC bit in the ELF header. This is different from
337   // what is stated in the SYSV ABI which consider the bits EF_MIPS_PIC and
338   // EF_MIPS_CPIC to be mutually exclusive.
339   Flags |= ELF::EF_MIPS_PIC | ELF::EF_MIPS_CPIC;
340   MCA.setELFHeaderEFlags(Flags);
341 }
342
343 void MipsTargetELFStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
344                                       unsigned ReturnReg) {
345   // FIXME: implement.
346 }
347
348 void MipsTargetELFStreamer::emitMask(unsigned CPUBitmask,
349                                      int CPUTopSavedRegOff) {
350   // FIXME: implement.
351 }
352
353 void MipsTargetELFStreamer::emitFMask(unsigned FPUBitmask,
354                                       int FPUTopSavedRegOff) {
355   // FIXME: implement.
356 }
357
358 void MipsTargetELFStreamer::emitDirectiveSetMips32R2() {
359   // No action required for ELF output.
360 }
361
362 void MipsTargetELFStreamer::emitDirectiveSetMips64R2() {
363   // No action required for ELF output.
364 }
365
366 void MipsTargetELFStreamer::emitDirectiveSetDsp() {
367   // No action required for ELF output.
368 }