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