[mips] Add support for .cpload.
[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
89 void MipsTargetAsmStreamer::emitDirectiveNaN2008() { OS << "\t.nan\t2008\n"; }
90
91 void MipsTargetAsmStreamer::emitDirectiveNaNLegacy() {
92   OS << "\t.nan\tlegacy\n";
93 }
94
95 void MipsTargetAsmStreamer::emitDirectiveOptionPic0() {
96   OS << "\t.option\tpic0\n";
97 }
98
99 void MipsTargetAsmStreamer::emitDirectiveOptionPic2() {
100   OS << "\t.option\tpic2\n";
101 }
102
103 void MipsTargetAsmStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
104                                       unsigned ReturnReg) {
105   OS << "\t.frame\t$"
106      << StringRef(MipsInstPrinter::getRegisterName(StackReg)).lower() << ","
107      << StackSize << ",$"
108      << StringRef(MipsInstPrinter::getRegisterName(ReturnReg)).lower() << '\n';
109 }
110
111 void MipsTargetAsmStreamer::emitDirectiveSetMips32R2() {
112   OS << "\t.set\tmips32r2\n";
113 }
114
115 void MipsTargetAsmStreamer::emitDirectiveSetMips64() {
116   OS << "\t.set\tmips64\n";
117 }
118
119 void MipsTargetAsmStreamer::emitDirectiveSetMips64R2() {
120   OS << "\t.set\tmips64r2\n";
121 }
122
123 void MipsTargetAsmStreamer::emitDirectiveSetDsp() {
124   OS << "\t.set\tdsp\n";
125 }
126 // Print a 32 bit hex number with all numbers.
127 static void printHex32(unsigned Value, raw_ostream &OS) {
128   OS << "0x";
129   for (int i = 7; i >= 0; i--)
130     OS.write_hex((Value & (0xF << (i*4))) >> (i*4));
131 }
132
133 void MipsTargetAsmStreamer::emitMask(unsigned CPUBitmask,
134                                      int CPUTopSavedRegOff) {
135   OS << "\t.mask \t";
136   printHex32(CPUBitmask, OS);
137   OS << ',' << CPUTopSavedRegOff << '\n';
138 }
139
140 void MipsTargetAsmStreamer::emitFMask(unsigned FPUBitmask,
141                                       int FPUTopSavedRegOff) {
142   OS << "\t.fmask\t";
143   printHex32(FPUBitmask, OS);
144   OS << "," << FPUTopSavedRegOff << '\n';
145 }
146
147 void MipsTargetAsmStreamer::emitDirectiveCpload(unsigned RegNo) {
148   OS << "\t.cpload\t$"
149      << StringRef(MipsInstPrinter::getRegisterName(RegNo)).lower() << "\n";
150 }
151
152 // This part is for ELF object output.
153 MipsTargetELFStreamer::MipsTargetELFStreamer(MCStreamer &S,
154                                              const MCSubtargetInfo &STI)
155     : MipsTargetStreamer(S), MicroMipsEnabled(false), STI(STI) {
156   MCAssembler &MCA = getStreamer().getAssembler();
157   uint64_t Features = STI.getFeatureBits();
158   Triple T(STI.getTargetTriple());
159   Pic = (MCA.getContext().getObjectFileInfo()->getRelocM() ==  Reloc::PIC_)
160             ? true
161             : false;
162
163   // Update e_header flags
164   unsigned EFlags = 0;
165
166   // Architecture
167   if (Features & Mips::FeatureMips64r2)
168     EFlags |= ELF::EF_MIPS_ARCH_64R2;
169   else if (Features & Mips::FeatureMips64)
170     EFlags |= ELF::EF_MIPS_ARCH_64;
171   else if (Features & Mips::FeatureMips4)
172     EFlags |= ELF::EF_MIPS_ARCH_4;
173   else if (Features & Mips::FeatureMips32r2)
174     EFlags |= ELF::EF_MIPS_ARCH_32R2;
175   else if (Features & Mips::FeatureMips32)
176     EFlags |= ELF::EF_MIPS_ARCH_32;
177
178   if (T.isArch64Bit()) {
179     if (Features & Mips::FeatureN32)
180       EFlags |= ELF::EF_MIPS_ABI2;
181     else if (Features & Mips::FeatureO32) {
182       EFlags |= ELF::EF_MIPS_ABI_O32;
183       EFlags |= ELF::EF_MIPS_32BITMODE; /* Compatibility Mode */
184     }
185     // No need to set any bit for N64 which is the default ABI at the moment
186     // for 64-bit Mips architectures.
187   } else {
188     if (Features & Mips::FeatureMips64r2 || Features & Mips::FeatureMips64)
189       EFlags |= ELF::EF_MIPS_32BITMODE;
190
191     // ABI
192     EFlags |= ELF::EF_MIPS_ABI_O32;
193   }
194
195   // Other options.
196   if (Features & Mips::FeatureNaN2008)
197     EFlags |= ELF::EF_MIPS_NAN2008;
198
199   MCA.setELFHeaderEFlags(EFlags);
200 }
201
202 void MipsTargetELFStreamer::emitLabel(MCSymbol *Symbol) {
203   if (!isMicroMipsEnabled())
204     return;
205   MCSymbolData &Data = getStreamer().getOrCreateSymbolData(Symbol);
206   uint8_t Type = MCELF::GetType(Data);
207   if (Type != ELF::STT_FUNC)
208     return;
209
210   // The "other" values are stored in the last 6 bits of the second byte
211   // The traditional defines for STO values assume the full byte and thus
212   // the shift to pack it.
213   MCELF::setOther(Data, ELF::STO_MIPS_MICROMIPS >> 2);
214 }
215
216 void MipsTargetELFStreamer::finish() {
217   MCAssembler &MCA = getStreamer().getAssembler();
218   MCContext &Context = MCA.getContext();
219   MCStreamer &OS = getStreamer();
220   Triple T(STI.getTargetTriple());
221   uint64_t Features = STI.getFeatureBits();
222
223   if (T.isArch64Bit() && (Features & Mips::FeatureN64)) {
224     const MCSectionELF *Sec = Context.getELFSection(
225         ".MIPS.options", ELF::SHT_MIPS_OPTIONS,
226         ELF::SHF_ALLOC | ELF::SHF_MIPS_NOSTRIP, SectionKind::getMetadata());
227     OS.SwitchSection(Sec);
228
229     OS.EmitIntValue(1, 1); // kind
230     OS.EmitIntValue(40, 1); // size
231     OS.EmitIntValue(0, 2); // section
232     OS.EmitIntValue(0, 4); // info
233     OS.EmitIntValue(0, 4); // ri_gprmask
234     OS.EmitIntValue(0, 4); // pad
235     OS.EmitIntValue(0, 4); // ri_cpr[0]mask
236     OS.EmitIntValue(0, 4); // ri_cpr[1]mask
237     OS.EmitIntValue(0, 4); // ri_cpr[2]mask
238     OS.EmitIntValue(0, 4); // ri_cpr[3]mask
239     OS.EmitIntValue(0, 8); // ri_gp_value
240   } else {
241     const MCSectionELF *Sec =
242         Context.getELFSection(".reginfo", ELF::SHT_MIPS_REGINFO, ELF::SHF_ALLOC,
243                               SectionKind::getMetadata());
244     OS.SwitchSection(Sec);
245
246     OS.EmitIntValue(0, 4); // ri_gprmask
247     OS.EmitIntValue(0, 4); // ri_cpr[0]mask
248     OS.EmitIntValue(0, 4); // ri_cpr[1]mask
249     OS.EmitIntValue(0, 4); // ri_cpr[2]mask
250     OS.EmitIntValue(0, 4); // ri_cpr[3]mask
251     OS.EmitIntValue(0, 4); // ri_gp_value
252   }
253 }
254
255 void MipsTargetELFStreamer::emitAssignment(MCSymbol *Symbol,
256                                            const MCExpr *Value) {
257   // If on rhs is micromips symbol then mark Symbol as microMips.
258   if (Value->getKind() != MCExpr::SymbolRef)
259     return;
260   const MCSymbol &RhsSym =
261     static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
262   MCSymbolData &Data = getStreamer().getOrCreateSymbolData(&RhsSym);
263   uint8_t Type = MCELF::GetType(Data);
264   if ((Type != ELF::STT_FUNC)
265       || !(MCELF::getOther(Data) & (ELF::STO_MIPS_MICROMIPS >> 2)))
266     return;
267
268   MCSymbolData &SymbolData = getStreamer().getOrCreateSymbolData(Symbol);
269   // The "other" values are stored in the last 6 bits of the second byte.
270   // The traditional defines for STO values assume the full byte and thus
271   // the shift to pack it.
272   MCELF::setOther(SymbolData, ELF::STO_MIPS_MICROMIPS >> 2);
273 }
274
275 MCELFStreamer &MipsTargetELFStreamer::getStreamer() {
276   return static_cast<MCELFStreamer &>(Streamer);
277 }
278
279 void MipsTargetELFStreamer::emitDirectiveSetMicroMips() {
280   MicroMipsEnabled = true;
281
282   MCAssembler &MCA = getStreamer().getAssembler();
283   unsigned Flags = MCA.getELFHeaderEFlags();
284   Flags |= ELF::EF_MIPS_MICROMIPS;
285   MCA.setELFHeaderEFlags(Flags);
286 }
287
288 void MipsTargetELFStreamer::emitDirectiveSetNoMicroMips() {
289   MicroMipsEnabled = false;
290 }
291
292 void MipsTargetELFStreamer::emitDirectiveSetMips16() {
293   MCAssembler &MCA = getStreamer().getAssembler();
294   unsigned Flags = MCA.getELFHeaderEFlags();
295   Flags |= ELF::EF_MIPS_ARCH_ASE_M16;
296   MCA.setELFHeaderEFlags(Flags);
297 }
298
299 void MipsTargetELFStreamer::emitDirectiveSetNoMips16() {
300   // FIXME: implement.
301 }
302
303 void MipsTargetELFStreamer::emitDirectiveSetReorder() {
304   // FIXME: implement.
305 }
306
307 void MipsTargetELFStreamer::emitDirectiveSetNoReorder() {
308   MCAssembler &MCA = getStreamer().getAssembler();
309   unsigned Flags = MCA.getELFHeaderEFlags();
310   Flags |= ELF::EF_MIPS_NOREORDER;
311   MCA.setELFHeaderEFlags(Flags);
312 }
313
314 void MipsTargetELFStreamer::emitDirectiveSetMacro() {
315   // FIXME: implement.
316 }
317
318 void MipsTargetELFStreamer::emitDirectiveSetNoMacro() {
319   // FIXME: implement.
320 }
321
322 void MipsTargetELFStreamer::emitDirectiveSetAt() {
323   // FIXME: implement.
324 }
325
326 void MipsTargetELFStreamer::emitDirectiveSetNoAt() {
327   // FIXME: implement.
328 }
329
330 void MipsTargetELFStreamer::emitDirectiveEnd(StringRef Name) {
331   // FIXME: implement.
332 }
333
334 void MipsTargetELFStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {
335   // FIXME: implement.
336 }
337
338 void MipsTargetELFStreamer::emitDirectiveAbiCalls() {
339   MCAssembler &MCA = getStreamer().getAssembler();
340   unsigned Flags = MCA.getELFHeaderEFlags();
341   Flags |= ELF::EF_MIPS_CPIC | ELF::EF_MIPS_PIC;
342   MCA.setELFHeaderEFlags(Flags);
343 }
344
345 void MipsTargetELFStreamer::emitDirectiveNaN2008() {
346   MCAssembler &MCA = getStreamer().getAssembler();
347   unsigned Flags = MCA.getELFHeaderEFlags();
348   Flags |= ELF::EF_MIPS_NAN2008;
349   MCA.setELFHeaderEFlags(Flags);
350 }
351
352 void MipsTargetELFStreamer::emitDirectiveNaNLegacy() {
353   MCAssembler &MCA = getStreamer().getAssembler();
354   unsigned Flags = MCA.getELFHeaderEFlags();
355   Flags &= ~ELF::EF_MIPS_NAN2008;
356   MCA.setELFHeaderEFlags(Flags);
357 }
358
359 void MipsTargetELFStreamer::emitDirectiveOptionPic0() {
360   MCAssembler &MCA = getStreamer().getAssembler();
361   unsigned Flags = MCA.getELFHeaderEFlags();
362   // This option overrides other PIC options like -KPIC.
363   Pic = false;
364   Flags &= ~ELF::EF_MIPS_PIC;
365   MCA.setELFHeaderEFlags(Flags);
366 }
367
368 void MipsTargetELFStreamer::emitDirectiveOptionPic2() {
369   MCAssembler &MCA = getStreamer().getAssembler();
370   unsigned Flags = MCA.getELFHeaderEFlags();
371   Pic = true;
372   // NOTE: We are following the GAS behaviour here which means the directive
373   // 'pic2' also sets the CPIC bit in the ELF header. This is different from
374   // what is stated in the SYSV ABI which consider the bits EF_MIPS_PIC and
375   // EF_MIPS_CPIC to be mutually exclusive.
376   Flags |= ELF::EF_MIPS_PIC | ELF::EF_MIPS_CPIC;
377   MCA.setELFHeaderEFlags(Flags);
378 }
379
380 void MipsTargetELFStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
381                                       unsigned ReturnReg) {
382   // FIXME: implement.
383 }
384
385 void MipsTargetELFStreamer::emitMask(unsigned CPUBitmask,
386                                      int CPUTopSavedRegOff) {
387   // FIXME: implement.
388 }
389
390 void MipsTargetELFStreamer::emitFMask(unsigned FPUBitmask,
391                                       int FPUTopSavedRegOff) {
392   // FIXME: implement.
393 }
394
395 void MipsTargetELFStreamer::emitDirectiveSetMips32R2() {
396   // No action required for ELF output.
397 }
398
399 void MipsTargetELFStreamer::emitDirectiveSetMips64() {
400   // No action required for ELF output.
401 }
402
403 void MipsTargetELFStreamer::emitDirectiveSetMips64R2() {
404   // No action required for ELF output.
405 }
406
407 void MipsTargetELFStreamer::emitDirectiveSetDsp() {
408   // No action required for ELF output.
409 }
410
411 void MipsTargetELFStreamer::emitDirectiveCpload(unsigned RegNo) {
412   // .cpload $reg
413   // This directive expands to:
414   // lui   $gp, %hi(_gp_disp)
415   // addui $gp, $gp, %lo(_gp_disp)
416   // addu  $gp, $gp, $reg
417   // when support for position independent code is enabled.
418   if (!Pic || (isN32() || isN64()))
419     return;
420
421   // There's a GNU extension controlled by -mno-shared that allows
422   // locally-binding symbols to be accessed using absolute addresses.
423   // This is currently not supported. When supported -mno-shared makes
424   // .cpload expand to:
425   //   lui     $gp, %hi(__gnu_local_gp)
426   //   addiu   $gp, $gp, %lo(__gnu_local_gp)
427
428   StringRef SymName("_gp_disp");
429   MCAssembler &MCA = getStreamer().getAssembler();
430   MCSymbol *GP_Disp = MCA.getContext().GetOrCreateSymbol(SymName);
431   MCA.getOrCreateSymbolData(*GP_Disp);
432
433   MCInst TmpInst;
434   TmpInst.setOpcode(Mips::LUi);
435   TmpInst.addOperand(MCOperand::CreateReg(Mips::GP));
436   const MCSymbolRefExpr *HiSym = MCSymbolRefExpr::Create(
437       "_gp_disp", MCSymbolRefExpr::VK_Mips_ABS_HI, MCA.getContext());
438   TmpInst.addOperand(MCOperand::CreateExpr(HiSym));
439   getStreamer().EmitInstruction(TmpInst, STI);
440
441   TmpInst.clear();
442
443   TmpInst.setOpcode(Mips::ADDiu);
444   TmpInst.addOperand(MCOperand::CreateReg(Mips::GP));
445   TmpInst.addOperand(MCOperand::CreateReg(Mips::GP));
446   const MCSymbolRefExpr *LoSym = MCSymbolRefExpr::Create(
447       "_gp_disp", MCSymbolRefExpr::VK_Mips_ABS_LO, MCA.getContext());
448   TmpInst.addOperand(MCOperand::CreateExpr(LoSym));
449   getStreamer().EmitInstruction(TmpInst, STI);
450
451   TmpInst.clear();
452
453   TmpInst.setOpcode(Mips::ADDu);
454   TmpInst.addOperand(MCOperand::CreateReg(Mips::GP));
455   TmpInst.addOperand(MCOperand::CreateReg(Mips::GP));
456   TmpInst.addOperand(MCOperand::CreateReg(RegNo));
457   getStreamer().EmitInstruction(TmpInst, STI);
458 }