blob: afe501b2ea53d596845a5018a012d4d9cdd77564 [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"; }
88void MipsTargetAsmStreamer::emitDirectiveOptionPic0() {
89 OS << "\t.option\tpic0\n";
90}
91
Matheus Almeidaf79b2812014-03-26 13:40:29 +000092void MipsTargetAsmStreamer::emitDirectiveOptionPic2() {
93 OS << "\t.option\tpic2\n";
94}
95
Rafael Espindola054234f2014-01-27 03:53:56 +000096void MipsTargetAsmStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
97 unsigned ReturnReg) {
98 OS << "\t.frame\t$"
99 << StringRef(MipsInstPrinter::getRegisterName(StackReg)).lower() << ","
100 << StackSize << ",$"
Rafael Espindola25fa2912014-01-27 04:33:11 +0000101 << StringRef(MipsInstPrinter::getRegisterName(ReturnReg)).lower() << '\n';
102}
103
Vladimir Medic615b26e2014-03-04 09:54:09 +0000104void MipsTargetAsmStreamer::emitDirectiveSetMips32R2() {
105 OS << "\t.set\tmips32r2\n";
106}
107
Vladimir Medic27c398e2014-03-05 11:05:09 +0000108void MipsTargetAsmStreamer::emitDirectiveSetDsp() {
109 OS << "\t.set\tdsp\n";
110}
Rafael Espindola25fa2912014-01-27 04:33:11 +0000111// Print a 32 bit hex number with all numbers.
112static void printHex32(unsigned Value, raw_ostream &OS) {
113 OS << "0x";
114 for (int i = 7; i >= 0; i--)
115 OS.write_hex((Value & (0xF << (i*4))) >> (i*4));
116}
117
118void MipsTargetAsmStreamer::emitMask(unsigned CPUBitmask,
119 int CPUTopSavedRegOff) {
120 OS << "\t.mask \t";
121 printHex32(CPUBitmask, OS);
122 OS << ',' << CPUTopSavedRegOff << '\n';
123}
124
125void MipsTargetAsmStreamer::emitFMask(unsigned FPUBitmask,
126 int FPUTopSavedRegOff) {
127 OS << "\t.fmask\t";
128 printHex32(FPUBitmask, OS);
129 OS << "," << FPUTopSavedRegOff << '\n';
Rafael Espindola054234f2014-01-27 03:53:56 +0000130}
131
Jack Carter0cd3c192014-01-06 23:27:31 +0000132// This part is for ELF object output.
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000133MipsTargetELFStreamer::MipsTargetELFStreamer(MCStreamer &S,
134 const MCSubtargetInfo &STI)
Rafael Espindola972e71a2014-01-31 23:10:26 +0000135 : MipsTargetStreamer(S), MicroMipsEnabled(false), STI(STI) {
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000136 MCAssembler &MCA = getStreamer().getAssembler();
137 uint64_t Features = STI.getFeatureBits();
138 Triple T(STI.getTargetTriple());
Matheus Almeidaf79b2812014-03-26 13:40:29 +0000139 Pic = (MCA.getContext().getObjectFileInfo()->getRelocM() == Reloc::PIC_)
140 ? true
141 : false;
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000142
143 // Update e_header flags
144 unsigned EFlags = 0;
145
146 // Architecture
147 if (Features & Mips::FeatureMips64r2)
148 EFlags |= ELF::EF_MIPS_ARCH_64R2;
149 else if (Features & Mips::FeatureMips64)
150 EFlags |= ELF::EF_MIPS_ARCH_64;
151 else if (Features & Mips::FeatureMips32r2)
152 EFlags |= ELF::EF_MIPS_ARCH_32R2;
153 else if (Features & Mips::FeatureMips32)
154 EFlags |= ELF::EF_MIPS_ARCH_32;
155
156 if (T.isArch64Bit()) {
Matheus Almeida2e03f242014-01-28 19:24:11 +0000157 if (Features & Mips::FeatureN32)
158 EFlags |= ELF::EF_MIPS_ABI2;
159 else if (Features & Mips::FeatureO32) {
160 EFlags |= ELF::EF_MIPS_ABI_O32;
161 EFlags |= ELF::EF_MIPS_32BITMODE; /* Compatibility Mode */
162 }
163 // No need to set any bit for N64 which is the default ABI at the moment
164 // for 64-bit Mips architectures.
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000165 } else {
166 if (Features & Mips::FeatureMips64r2 || Features & Mips::FeatureMips64)
167 EFlags |= ELF::EF_MIPS_32BITMODE;
168
169 // ABI
170 EFlags |= ELF::EF_MIPS_ABI_O32;
171 }
172
173 MCA.setELFHeaderEFlags(EFlags);
174}
Jack Carter86ac5c12013-11-18 23:55:27 +0000175
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000176void MipsTargetELFStreamer::emitLabel(MCSymbol *Symbol) {
Rafael Espindola26e917c2014-01-15 03:07:12 +0000177 if (!isMicroMipsEnabled())
178 return;
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000179 MCSymbolData &Data = getStreamer().getOrCreateSymbolData(Symbol);
Rafael Espindola26e917c2014-01-15 03:07:12 +0000180 uint8_t Type = MCELF::GetType(Data);
181 if (Type != ELF::STT_FUNC)
182 return;
183
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000184 // The "other" values are stored in the last 6 bits of the second byte
185 // The traditional defines for STO values assume the full byte and thus
186 // the shift to pack it.
Rafael Espindola26e917c2014-01-15 03:07:12 +0000187 MCELF::setOther(Data, ELF::STO_MIPS_MICROMIPS >> 2);
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000188}
189
Rafael Espindola972e71a2014-01-31 23:10:26 +0000190void MipsTargetELFStreamer::finish() {
191 MCAssembler &MCA = getStreamer().getAssembler();
192 MCContext &Context = MCA.getContext();
193 MCStreamer &OS = getStreamer();
194 Triple T(STI.getTargetTriple());
195 uint64_t Features = STI.getFeatureBits();
196
197 if (T.isArch64Bit() && (Features & Mips::FeatureN64)) {
198 const MCSectionELF *Sec = Context.getELFSection(
199 ".MIPS.options", ELF::SHT_MIPS_OPTIONS,
200 ELF::SHF_ALLOC | ELF::SHF_MIPS_NOSTRIP, SectionKind::getMetadata());
201 OS.SwitchSection(Sec);
202
203 OS.EmitIntValue(1, 1); // kind
204 OS.EmitIntValue(40, 1); // size
205 OS.EmitIntValue(0, 2); // section
206 OS.EmitIntValue(0, 4); // info
207 OS.EmitIntValue(0, 4); // ri_gprmask
208 OS.EmitIntValue(0, 4); // pad
209 OS.EmitIntValue(0, 4); // ri_cpr[0]mask
210 OS.EmitIntValue(0, 4); // ri_cpr[1]mask
211 OS.EmitIntValue(0, 4); // ri_cpr[2]mask
212 OS.EmitIntValue(0, 4); // ri_cpr[3]mask
213 OS.EmitIntValue(0, 8); // ri_gp_value
214 } else {
215 const MCSectionELF *Sec =
216 Context.getELFSection(".reginfo", ELF::SHT_MIPS_REGINFO, ELF::SHF_ALLOC,
217 SectionKind::getMetadata());
218 OS.SwitchSection(Sec);
219
220 OS.EmitIntValue(0, 4); // ri_gprmask
221 OS.EmitIntValue(0, 4); // ri_cpr[0]mask
222 OS.EmitIntValue(0, 4); // ri_cpr[1]mask
223 OS.EmitIntValue(0, 4); // ri_cpr[2]mask
224 OS.EmitIntValue(0, 4); // ri_cpr[3]mask
225 OS.EmitIntValue(0, 4); // ri_gp_value
226 }
227}
228
Zoran Jovanovic28221d82014-03-20 09:44:49 +0000229void MipsTargetELFStreamer::emitAssignment(MCSymbol *Symbol,
230 const MCExpr *Value) {
231 // If on rhs is micromips symbol then mark Symbol as microMips.
232 if (Value->getKind() != MCExpr::SymbolRef)
233 return;
234 const MCSymbol &RhsSym =
235 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
236 MCSymbolData &Data = getStreamer().getOrCreateSymbolData(&RhsSym);
237 uint8_t Type = MCELF::GetType(Data);
238 if ((Type != ELF::STT_FUNC)
239 || !(MCELF::getOther(Data) & (ELF::STO_MIPS_MICROMIPS >> 2)))
240 return;
241
242 MCSymbolData &SymbolData = getStreamer().getOrCreateSymbolData(Symbol);
243 // The "other" values are stored in the last 6 bits of the second byte.
244 // The traditional defines for STO values assume the full byte and thus
245 // the shift to pack it.
246 MCELF::setOther(SymbolData, ELF::STO_MIPS_MICROMIPS >> 2);
247}
248
Jack Carter86ac5c12013-11-18 23:55:27 +0000249MCELFStreamer &MipsTargetELFStreamer::getStreamer() {
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000250 return static_cast<MCELFStreamer &>(Streamer);
Jack Carter86ac5c12013-11-18 23:55:27 +0000251}
252
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000253void MipsTargetELFStreamer::emitDirectiveSetMicroMips() {
254 MicroMipsEnabled = true;
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000255
256 MCAssembler &MCA = getStreamer().getAssembler();
257 unsigned Flags = MCA.getELFHeaderEFlags();
258 Flags |= ELF::EF_MIPS_MICROMIPS;
259 MCA.setELFHeaderEFlags(Flags);
Jack Carter86ac5c12013-11-18 23:55:27 +0000260}
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000261
262void MipsTargetELFStreamer::emitDirectiveSetNoMicroMips() {
263 MicroMipsEnabled = false;
264}
265
Rafael Espindola6633d572014-01-14 18:57:12 +0000266void MipsTargetELFStreamer::emitDirectiveSetMips16() {
Rafael Espindolae7583752014-01-24 16:13:20 +0000267 MCAssembler &MCA = getStreamer().getAssembler();
268 unsigned Flags = MCA.getELFHeaderEFlags();
269 Flags |= ELF::EF_MIPS_ARCH_ASE_M16;
270 MCA.setELFHeaderEFlags(Flags);
Rafael Espindola6633d572014-01-14 18:57:12 +0000271}
272
273void MipsTargetELFStreamer::emitDirectiveSetNoMips16() {
274 // FIXME: implement.
275}
276
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000277void MipsTargetELFStreamer::emitDirectiveSetReorder() {
278 // FIXME: implement.
279}
280
281void MipsTargetELFStreamer::emitDirectiveSetNoReorder() {
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000282 MCAssembler &MCA = getStreamer().getAssembler();
283 unsigned Flags = MCA.getELFHeaderEFlags();
284 Flags |= ELF::EF_MIPS_NOREORDER;
285 MCA.setELFHeaderEFlags(Flags);
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000286}
287
288void MipsTargetELFStreamer::emitDirectiveSetMacro() {
289 // FIXME: implement.
290}
291
292void MipsTargetELFStreamer::emitDirectiveSetNoMacro() {
293 // FIXME: implement.
294}
295
296void MipsTargetELFStreamer::emitDirectiveSetAt() {
297 // FIXME: implement.
298}
299
300void MipsTargetELFStreamer::emitDirectiveSetNoAt() {
301 // FIXME: implement.
302}
303
304void MipsTargetELFStreamer::emitDirectiveEnd(StringRef Name) {
305 // FIXME: implement.
306}
307
Rafael Espindola6633d572014-01-14 18:57:12 +0000308void MipsTargetELFStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {
309 // FIXME: implement.
310}
311
Jack Carter0cd3c192014-01-06 23:27:31 +0000312void MipsTargetELFStreamer::emitDirectiveAbiCalls() {
313 MCAssembler &MCA = getStreamer().getAssembler();
314 unsigned Flags = MCA.getELFHeaderEFlags();
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000315 Flags |= ELF::EF_MIPS_CPIC | ELF::EF_MIPS_PIC;
Jack Carter0cd3c192014-01-06 23:27:31 +0000316 MCA.setELFHeaderEFlags(Flags);
317}
318void MipsTargetELFStreamer::emitDirectiveOptionPic0() {
319 MCAssembler &MCA = getStreamer().getAssembler();
320 unsigned Flags = MCA.getELFHeaderEFlags();
Matheus Almeidaf79b2812014-03-26 13:40:29 +0000321 // This option overrides other PIC options like -KPIC.
322 Pic = false;
Jack Carter0cd3c192014-01-06 23:27:31 +0000323 Flags &= ~ELF::EF_MIPS_PIC;
324 MCA.setELFHeaderEFlags(Flags);
325}
Rafael Espindola054234f2014-01-27 03:53:56 +0000326
Matheus Almeidaf79b2812014-03-26 13:40:29 +0000327void MipsTargetELFStreamer::emitDirectiveOptionPic2() {
328 MCAssembler &MCA = getStreamer().getAssembler();
329 unsigned Flags = MCA.getELFHeaderEFlags();
330 Pic = true;
331 // NOTE: We are following the GAS behaviour here which means the directive
332 // 'pic2' also sets the CPIC bit in the ELF header. This is different from
333 // what is stated in the SYSV ABI which consider the bits EF_MIPS_PIC and
334 // EF_MIPS_CPIC to be mutually exclusive.
335 Flags |= ELF::EF_MIPS_PIC | ELF::EF_MIPS_CPIC;
336 MCA.setELFHeaderEFlags(Flags);
337}
338
Rafael Espindola054234f2014-01-27 03:53:56 +0000339void MipsTargetELFStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
340 unsigned ReturnReg) {
341 // FIXME: implement.
342}
Rafael Espindola25fa2912014-01-27 04:33:11 +0000343
344void MipsTargetELFStreamer::emitMask(unsigned CPUBitmask,
345 int CPUTopSavedRegOff) {
346 // FIXME: implement.
347}
348
349void MipsTargetELFStreamer::emitFMask(unsigned FPUBitmask,
350 int FPUTopSavedRegOff) {
351 // FIXME: implement.
352}
Vladimir Medic615b26e2014-03-04 09:54:09 +0000353
354void MipsTargetELFStreamer::emitDirectiveSetMips32R2() {
355 // No action required for ELF output.
356}
Vladimir Medic27c398e2014-03-05 11:05:09 +0000357
358void MipsTargetELFStreamer::emitDirectiveSetDsp() {
359 // No action required for ELF output.
360}