blob: 053e13e5565f18cb9252b3b192705e9725627df5 [file] [log] [blame]
Jack Carter86ac5c12013-11-18 23:55:27 +00001//===-- 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
Rafael Espindola054234f2014-01-27 03:53:56 +000014#include "InstPrinter/MipsInstPrinter.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000015#include "MipsMCTargetDesc.h"
Rafael Espindola972e71a2014-01-31 23:10:26 +000016#include "MipsTargetObjectFile.h"
Jack Carter86ac5c12013-11-18 23:55:27 +000017#include "MipsTargetStreamer.h"
Rafael Espindola972e71a2014-01-31 23:10:26 +000018#include "llvm/MC/MCContext.h"
Jack Carter86ac5c12013-11-18 23:55:27 +000019#include "llvm/MC/MCELF.h"
Rafael Espindola972e71a2014-01-31 23:10:26 +000020#include "llvm/MC/MCSectionELF.h"
Rafael Espindolacb1953f2014-01-26 06:57:13 +000021#include "llvm/MC/MCSubtargetInfo.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000022#include "llvm/MC/MCSymbol.h"
Jack Carter86ac5c12013-11-18 23:55:27 +000023#include "llvm/Support/CommandLine.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000024#include "llvm/Support/ELF.h"
Jack Carter86ac5c12013-11-18 23:55:27 +000025#include "llvm/Support/ErrorHandling.h"
26#include "llvm/Support/FormattedStream.h"
27
28using namespace llvm;
29
Jack Carter0cd3c192014-01-06 23:27:31 +000030// Pin vtable to this file.
Jack Carter86ac5c12013-11-18 23:55:27 +000031void MipsTargetStreamer::anchor() {}
32
Rafael Espindola24ea09e2014-01-26 06:06:37 +000033MipsTargetStreamer::MipsTargetStreamer(MCStreamer &S) : MCTargetStreamer(S) {}
34
35MipsTargetAsmStreamer::MipsTargetAsmStreamer(MCStreamer &S,
36 formatted_raw_ostream &OS)
37 : MipsTargetStreamer(S), OS(OS) {}
Jack Carter6ef6cc52013-11-19 20:53:28 +000038
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +000039void MipsTargetAsmStreamer::emitDirectiveSetMicroMips() {
40 OS << "\t.set\tmicromips\n";
Jack Carter6ef6cc52013-11-19 20:53:28 +000041}
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +000042
43void MipsTargetAsmStreamer::emitDirectiveSetNoMicroMips() {
44 OS << "\t.set\tnomicromips\n";
45}
46
Rafael Espindola6633d572014-01-14 18:57:12 +000047void MipsTargetAsmStreamer::emitDirectiveSetMips16() {
48 OS << "\t.set\tmips16\n";
49}
50
51void MipsTargetAsmStreamer::emitDirectiveSetNoMips16() {
52 OS << "\t.set\tnomips16\n";
53}
54
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +000055void MipsTargetAsmStreamer::emitDirectiveSetReorder() {
56 OS << "\t.set\treorder\n";
57}
58
59void MipsTargetAsmStreamer::emitDirectiveSetNoReorder() {
60 OS << "\t.set\tnoreorder\n";
61}
62
63void MipsTargetAsmStreamer::emitDirectiveSetMacro() {
64 OS << "\t.set\tmacro\n";
65}
66
67void MipsTargetAsmStreamer::emitDirectiveSetNoMacro() {
68 OS << "\t.set\tnomacro\n";
69}
70
71void MipsTargetAsmStreamer::emitDirectiveSetAt() {
72 OS << "\t.set\tat\n";
73}
74
75void MipsTargetAsmStreamer::emitDirectiveSetNoAt() {
76 OS << "\t.set\tnoat\n";
77}
78
79void MipsTargetAsmStreamer::emitDirectiveEnd(StringRef Name) {
80 OS << "\t.end\t" << Name << '\n';
81}
82
Rafael Espindola6633d572014-01-14 18:57:12 +000083void MipsTargetAsmStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {
84 OS << "\t.ent\t" << Symbol.getName() << '\n';
85}
86
Jack Carter0cd3c192014-01-06 23:27:31 +000087void MipsTargetAsmStreamer::emitDirectiveAbiCalls() { OS << "\t.abicalls\n"; }
Matheus Almeida0051f2d2014-04-16 15:48:55 +000088
89void MipsTargetAsmStreamer::emitDirectiveNaN2008() { OS << "\t.nan\t2008\n"; }
90
91void MipsTargetAsmStreamer::emitDirectiveNaNLegacy() {
92 OS << "\t.nan\tlegacy\n";
93}
94
Jack Carter0cd3c192014-01-06 23:27:31 +000095void MipsTargetAsmStreamer::emitDirectiveOptionPic0() {
96 OS << "\t.option\tpic0\n";
97}
98
Matheus Almeidaf79b2812014-03-26 13:40:29 +000099void MipsTargetAsmStreamer::emitDirectiveOptionPic2() {
100 OS << "\t.option\tpic2\n";
101}
102
Rafael Espindola054234f2014-01-27 03:53:56 +0000103void MipsTargetAsmStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
104 unsigned ReturnReg) {
105 OS << "\t.frame\t$"
106 << StringRef(MipsInstPrinter::getRegisterName(StackReg)).lower() << ","
107 << StackSize << ",$"
Rafael Espindola25fa2912014-01-27 04:33:11 +0000108 << StringRef(MipsInstPrinter::getRegisterName(ReturnReg)).lower() << '\n';
109}
110
Vladimir Medic615b26e2014-03-04 09:54:09 +0000111void MipsTargetAsmStreamer::emitDirectiveSetMips32R2() {
112 OS << "\t.set\tmips32r2\n";
113}
114
Matheus Almeida3b9c63d2014-03-26 15:14:32 +0000115void MipsTargetAsmStreamer::emitDirectiveSetMips64() {
116 OS << "\t.set\tmips64\n";
117}
118
Matheus Almeidaa2cd0092014-03-26 14:52:22 +0000119void MipsTargetAsmStreamer::emitDirectiveSetMips64R2() {
120 OS << "\t.set\tmips64r2\n";
121}
122
Vladimir Medic27c398e2014-03-05 11:05:09 +0000123void MipsTargetAsmStreamer::emitDirectiveSetDsp() {
124 OS << "\t.set\tdsp\n";
125}
Rafael Espindola25fa2912014-01-27 04:33:11 +0000126// Print a 32 bit hex number with all numbers.
127static 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
133void MipsTargetAsmStreamer::emitMask(unsigned CPUBitmask,
134 int CPUTopSavedRegOff) {
135 OS << "\t.mask \t";
136 printHex32(CPUBitmask, OS);
137 OS << ',' << CPUTopSavedRegOff << '\n';
138}
139
140void MipsTargetAsmStreamer::emitFMask(unsigned FPUBitmask,
141 int FPUTopSavedRegOff) {
142 OS << "\t.fmask\t";
143 printHex32(FPUBitmask, OS);
144 OS << "," << FPUTopSavedRegOff << '\n';
Rafael Espindola054234f2014-01-27 03:53:56 +0000145}
146
Jack Carter0cd3c192014-01-06 23:27:31 +0000147// This part is for ELF object output.
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000148MipsTargetELFStreamer::MipsTargetELFStreamer(MCStreamer &S,
149 const MCSubtargetInfo &STI)
Rafael Espindola972e71a2014-01-31 23:10:26 +0000150 : MipsTargetStreamer(S), MicroMipsEnabled(false), STI(STI) {
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000151 MCAssembler &MCA = getStreamer().getAssembler();
152 uint64_t Features = STI.getFeatureBits();
153 Triple T(STI.getTargetTriple());
Matheus Almeidaf79b2812014-03-26 13:40:29 +0000154 Pic = (MCA.getContext().getObjectFileInfo()->getRelocM() == Reloc::PIC_)
155 ? true
156 : false;
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000157
158 // Update e_header flags
159 unsigned EFlags = 0;
160
161 // Architecture
162 if (Features & Mips::FeatureMips64r2)
163 EFlags |= ELF::EF_MIPS_ARCH_64R2;
164 else if (Features & Mips::FeatureMips64)
165 EFlags |= ELF::EF_MIPS_ARCH_64;
Daniel Sandersf7b32292014-04-03 12:13:36 +0000166 else if (Features & Mips::FeatureMips4)
167 EFlags |= ELF::EF_MIPS_ARCH_4;
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000168 else if (Features & Mips::FeatureMips32r2)
169 EFlags |= ELF::EF_MIPS_ARCH_32R2;
170 else if (Features & Mips::FeatureMips32)
171 EFlags |= ELF::EF_MIPS_ARCH_32;
172
173 if (T.isArch64Bit()) {
Matheus Almeida2e03f242014-01-28 19:24:11 +0000174 if (Features & Mips::FeatureN32)
175 EFlags |= ELF::EF_MIPS_ABI2;
176 else if (Features & Mips::FeatureO32) {
177 EFlags |= ELF::EF_MIPS_ABI_O32;
178 EFlags |= ELF::EF_MIPS_32BITMODE; /* Compatibility Mode */
179 }
180 // No need to set any bit for N64 which is the default ABI at the moment
181 // for 64-bit Mips architectures.
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000182 } else {
183 if (Features & Mips::FeatureMips64r2 || Features & Mips::FeatureMips64)
184 EFlags |= ELF::EF_MIPS_32BITMODE;
185
186 // ABI
187 EFlags |= ELF::EF_MIPS_ABI_O32;
188 }
189
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000190 // Other options.
191 if (Features & Mips::FeatureNaN2008)
192 EFlags |= ELF::EF_MIPS_NAN2008;
193
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000194 MCA.setELFHeaderEFlags(EFlags);
195}
Jack Carter86ac5c12013-11-18 23:55:27 +0000196
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000197void MipsTargetELFStreamer::emitLabel(MCSymbol *Symbol) {
Rafael Espindola26e917c2014-01-15 03:07:12 +0000198 if (!isMicroMipsEnabled())
199 return;
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000200 MCSymbolData &Data = getStreamer().getOrCreateSymbolData(Symbol);
Rafael Espindola26e917c2014-01-15 03:07:12 +0000201 uint8_t Type = MCELF::GetType(Data);
202 if (Type != ELF::STT_FUNC)
203 return;
204
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000205 // The "other" values are stored in the last 6 bits of the second byte
206 // The traditional defines for STO values assume the full byte and thus
207 // the shift to pack it.
Rafael Espindola26e917c2014-01-15 03:07:12 +0000208 MCELF::setOther(Data, ELF::STO_MIPS_MICROMIPS >> 2);
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000209}
210
Rafael Espindola972e71a2014-01-31 23:10:26 +0000211void MipsTargetELFStreamer::finish() {
212 MCAssembler &MCA = getStreamer().getAssembler();
213 MCContext &Context = MCA.getContext();
214 MCStreamer &OS = getStreamer();
215 Triple T(STI.getTargetTriple());
216 uint64_t Features = STI.getFeatureBits();
217
218 if (T.isArch64Bit() && (Features & Mips::FeatureN64)) {
219 const MCSectionELF *Sec = Context.getELFSection(
220 ".MIPS.options", ELF::SHT_MIPS_OPTIONS,
221 ELF::SHF_ALLOC | ELF::SHF_MIPS_NOSTRIP, SectionKind::getMetadata());
222 OS.SwitchSection(Sec);
223
224 OS.EmitIntValue(1, 1); // kind
225 OS.EmitIntValue(40, 1); // size
226 OS.EmitIntValue(0, 2); // section
227 OS.EmitIntValue(0, 4); // info
228 OS.EmitIntValue(0, 4); // ri_gprmask
229 OS.EmitIntValue(0, 4); // pad
230 OS.EmitIntValue(0, 4); // ri_cpr[0]mask
231 OS.EmitIntValue(0, 4); // ri_cpr[1]mask
232 OS.EmitIntValue(0, 4); // ri_cpr[2]mask
233 OS.EmitIntValue(0, 4); // ri_cpr[3]mask
234 OS.EmitIntValue(0, 8); // ri_gp_value
235 } else {
236 const MCSectionELF *Sec =
237 Context.getELFSection(".reginfo", ELF::SHT_MIPS_REGINFO, ELF::SHF_ALLOC,
238 SectionKind::getMetadata());
239 OS.SwitchSection(Sec);
240
241 OS.EmitIntValue(0, 4); // ri_gprmask
242 OS.EmitIntValue(0, 4); // ri_cpr[0]mask
243 OS.EmitIntValue(0, 4); // ri_cpr[1]mask
244 OS.EmitIntValue(0, 4); // ri_cpr[2]mask
245 OS.EmitIntValue(0, 4); // ri_cpr[3]mask
246 OS.EmitIntValue(0, 4); // ri_gp_value
247 }
248}
249
Zoran Jovanovic28221d82014-03-20 09:44:49 +0000250void MipsTargetELFStreamer::emitAssignment(MCSymbol *Symbol,
251 const MCExpr *Value) {
252 // If on rhs is micromips symbol then mark Symbol as microMips.
253 if (Value->getKind() != MCExpr::SymbolRef)
254 return;
255 const MCSymbol &RhsSym =
256 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
257 MCSymbolData &Data = getStreamer().getOrCreateSymbolData(&RhsSym);
258 uint8_t Type = MCELF::GetType(Data);
259 if ((Type != ELF::STT_FUNC)
260 || !(MCELF::getOther(Data) & (ELF::STO_MIPS_MICROMIPS >> 2)))
261 return;
262
263 MCSymbolData &SymbolData = getStreamer().getOrCreateSymbolData(Symbol);
264 // The "other" values are stored in the last 6 bits of the second byte.
265 // The traditional defines for STO values assume the full byte and thus
266 // the shift to pack it.
267 MCELF::setOther(SymbolData, ELF::STO_MIPS_MICROMIPS >> 2);
268}
269
Jack Carter86ac5c12013-11-18 23:55:27 +0000270MCELFStreamer &MipsTargetELFStreamer::getStreamer() {
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000271 return static_cast<MCELFStreamer &>(Streamer);
Jack Carter86ac5c12013-11-18 23:55:27 +0000272}
273
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000274void MipsTargetELFStreamer::emitDirectiveSetMicroMips() {
275 MicroMipsEnabled = true;
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000276
277 MCAssembler &MCA = getStreamer().getAssembler();
278 unsigned Flags = MCA.getELFHeaderEFlags();
279 Flags |= ELF::EF_MIPS_MICROMIPS;
280 MCA.setELFHeaderEFlags(Flags);
Jack Carter86ac5c12013-11-18 23:55:27 +0000281}
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000282
283void MipsTargetELFStreamer::emitDirectiveSetNoMicroMips() {
284 MicroMipsEnabled = false;
285}
286
Rafael Espindola6633d572014-01-14 18:57:12 +0000287void MipsTargetELFStreamer::emitDirectiveSetMips16() {
Rafael Espindolae7583752014-01-24 16:13:20 +0000288 MCAssembler &MCA = getStreamer().getAssembler();
289 unsigned Flags = MCA.getELFHeaderEFlags();
290 Flags |= ELF::EF_MIPS_ARCH_ASE_M16;
291 MCA.setELFHeaderEFlags(Flags);
Rafael Espindola6633d572014-01-14 18:57:12 +0000292}
293
294void MipsTargetELFStreamer::emitDirectiveSetNoMips16() {
295 // FIXME: implement.
296}
297
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000298void MipsTargetELFStreamer::emitDirectiveSetReorder() {
299 // FIXME: implement.
300}
301
302void MipsTargetELFStreamer::emitDirectiveSetNoReorder() {
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000303 MCAssembler &MCA = getStreamer().getAssembler();
304 unsigned Flags = MCA.getELFHeaderEFlags();
305 Flags |= ELF::EF_MIPS_NOREORDER;
306 MCA.setELFHeaderEFlags(Flags);
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000307}
308
309void MipsTargetELFStreamer::emitDirectiveSetMacro() {
310 // FIXME: implement.
311}
312
313void MipsTargetELFStreamer::emitDirectiveSetNoMacro() {
314 // FIXME: implement.
315}
316
317void MipsTargetELFStreamer::emitDirectiveSetAt() {
318 // FIXME: implement.
319}
320
321void MipsTargetELFStreamer::emitDirectiveSetNoAt() {
322 // FIXME: implement.
323}
324
325void MipsTargetELFStreamer::emitDirectiveEnd(StringRef Name) {
326 // FIXME: implement.
327}
328
Rafael Espindola6633d572014-01-14 18:57:12 +0000329void MipsTargetELFStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {
330 // FIXME: implement.
331}
332
Jack Carter0cd3c192014-01-06 23:27:31 +0000333void MipsTargetELFStreamer::emitDirectiveAbiCalls() {
334 MCAssembler &MCA = getStreamer().getAssembler();
335 unsigned Flags = MCA.getELFHeaderEFlags();
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000336 Flags |= ELF::EF_MIPS_CPIC | ELF::EF_MIPS_PIC;
Jack Carter0cd3c192014-01-06 23:27:31 +0000337 MCA.setELFHeaderEFlags(Flags);
338}
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000339
340void MipsTargetELFStreamer::emitDirectiveNaN2008() {
341 MCAssembler &MCA = getStreamer().getAssembler();
342 unsigned Flags = MCA.getELFHeaderEFlags();
343 Flags |= ELF::EF_MIPS_NAN2008;
344 MCA.setELFHeaderEFlags(Flags);
345}
346
347void MipsTargetELFStreamer::emitDirectiveNaNLegacy() {
348 MCAssembler &MCA = getStreamer().getAssembler();
349 unsigned Flags = MCA.getELFHeaderEFlags();
350 Flags &= ~ELF::EF_MIPS_NAN2008;
351 MCA.setELFHeaderEFlags(Flags);
352}
353
Jack Carter0cd3c192014-01-06 23:27:31 +0000354void MipsTargetELFStreamer::emitDirectiveOptionPic0() {
355 MCAssembler &MCA = getStreamer().getAssembler();
356 unsigned Flags = MCA.getELFHeaderEFlags();
Matheus Almeidaf79b2812014-03-26 13:40:29 +0000357 // This option overrides other PIC options like -KPIC.
358 Pic = false;
Jack Carter0cd3c192014-01-06 23:27:31 +0000359 Flags &= ~ELF::EF_MIPS_PIC;
360 MCA.setELFHeaderEFlags(Flags);
361}
Rafael Espindola054234f2014-01-27 03:53:56 +0000362
Matheus Almeidaf79b2812014-03-26 13:40:29 +0000363void MipsTargetELFStreamer::emitDirectiveOptionPic2() {
364 MCAssembler &MCA = getStreamer().getAssembler();
365 unsigned Flags = MCA.getELFHeaderEFlags();
366 Pic = true;
367 // NOTE: We are following the GAS behaviour here which means the directive
368 // 'pic2' also sets the CPIC bit in the ELF header. This is different from
369 // what is stated in the SYSV ABI which consider the bits EF_MIPS_PIC and
370 // EF_MIPS_CPIC to be mutually exclusive.
371 Flags |= ELF::EF_MIPS_PIC | ELF::EF_MIPS_CPIC;
372 MCA.setELFHeaderEFlags(Flags);
373}
374
Rafael Espindola054234f2014-01-27 03:53:56 +0000375void MipsTargetELFStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
376 unsigned ReturnReg) {
377 // FIXME: implement.
378}
Rafael Espindola25fa2912014-01-27 04:33:11 +0000379
380void MipsTargetELFStreamer::emitMask(unsigned CPUBitmask,
381 int CPUTopSavedRegOff) {
382 // FIXME: implement.
383}
384
385void MipsTargetELFStreamer::emitFMask(unsigned FPUBitmask,
386 int FPUTopSavedRegOff) {
387 // FIXME: implement.
388}
Vladimir Medic615b26e2014-03-04 09:54:09 +0000389
390void MipsTargetELFStreamer::emitDirectiveSetMips32R2() {
391 // No action required for ELF output.
392}
Vladimir Medic27c398e2014-03-05 11:05:09 +0000393
Matheus Almeida3b9c63d2014-03-26 15:14:32 +0000394void MipsTargetELFStreamer::emitDirectiveSetMips64() {
395 // No action required for ELF output.
396}
397
Matheus Almeidaa2cd0092014-03-26 14:52:22 +0000398void MipsTargetELFStreamer::emitDirectiveSetMips64R2() {
399 // No action required for ELF output.
400}
401
Vladimir Medic27c398e2014-03-05 11:05:09 +0000402void MipsTargetELFStreamer::emitDirectiveSetDsp() {
403 // No action required for ELF output.
404}