1 //===-- MipsTargetStreamer.cpp - Mips Target Streamer Methods -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides Mips specific target streamer methods.
12 //===----------------------------------------------------------------------===//
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"
30 // Pin vtable to this file.
31 void MipsTargetStreamer::anchor() {}
33 MipsTargetStreamer::MipsTargetStreamer(MCStreamer &S) : MCTargetStreamer(S) {}
35 MipsTargetAsmStreamer::MipsTargetAsmStreamer(MCStreamer &S,
36 formatted_raw_ostream &OS)
37 : MipsTargetStreamer(S), OS(OS) {}
39 void MipsTargetAsmStreamer::emitDirectiveSetMicroMips() {
40 OS << "\t.set\tmicromips\n";
43 void MipsTargetAsmStreamer::emitDirectiveSetNoMicroMips() {
44 OS << "\t.set\tnomicromips\n";
47 void MipsTargetAsmStreamer::emitDirectiveSetMips16() {
48 OS << "\t.set\tmips16\n";
51 void MipsTargetAsmStreamer::emitDirectiveSetNoMips16() {
52 OS << "\t.set\tnomips16\n";
55 void MipsTargetAsmStreamer::emitDirectiveSetReorder() {
56 OS << "\t.set\treorder\n";
59 void MipsTargetAsmStreamer::emitDirectiveSetNoReorder() {
60 OS << "\t.set\tnoreorder\n";
63 void MipsTargetAsmStreamer::emitDirectiveSetMacro() {
64 OS << "\t.set\tmacro\n";
67 void MipsTargetAsmStreamer::emitDirectiveSetNoMacro() {
68 OS << "\t.set\tnomacro\n";
71 void MipsTargetAsmStreamer::emitDirectiveSetAt() {
75 void MipsTargetAsmStreamer::emitDirectiveSetNoAt() {
76 OS << "\t.set\tnoat\n";
79 void MipsTargetAsmStreamer::emitDirectiveEnd(StringRef Name) {
80 OS << "\t.end\t" << Name << '\n';
83 void MipsTargetAsmStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {
84 OS << "\t.ent\t" << Symbol.getName() << '\n';
87 void MipsTargetAsmStreamer::emitDirectiveAbiCalls() { OS << "\t.abicalls\n"; }
89 void MipsTargetAsmStreamer::emitDirectiveNaN2008() { OS << "\t.nan\t2008\n"; }
91 void MipsTargetAsmStreamer::emitDirectiveNaNLegacy() {
92 OS << "\t.nan\tlegacy\n";
95 void MipsTargetAsmStreamer::emitDirectiveOptionPic0() {
96 OS << "\t.option\tpic0\n";
99 void MipsTargetAsmStreamer::emitDirectiveOptionPic2() {
100 OS << "\t.option\tpic2\n";
103 void MipsTargetAsmStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
104 unsigned ReturnReg) {
106 << StringRef(MipsInstPrinter::getRegisterName(StackReg)).lower() << ","
108 << StringRef(MipsInstPrinter::getRegisterName(ReturnReg)).lower() << '\n';
111 void MipsTargetAsmStreamer::emitDirectiveSetMips32R2() {
112 OS << "\t.set\tmips32r2\n";
115 void MipsTargetAsmStreamer::emitDirectiveSetMips64() {
116 OS << "\t.set\tmips64\n";
119 void MipsTargetAsmStreamer::emitDirectiveSetMips64R2() {
120 OS << "\t.set\tmips64r2\n";
123 void MipsTargetAsmStreamer::emitDirectiveSetDsp() {
124 OS << "\t.set\tdsp\n";
126 // Print a 32 bit hex number with all numbers.
127 static void printHex32(unsigned Value, raw_ostream &OS) {
129 for (int i = 7; i >= 0; i--)
130 OS.write_hex((Value & (0xF << (i*4))) >> (i*4));
133 void MipsTargetAsmStreamer::emitMask(unsigned CPUBitmask,
134 int CPUTopSavedRegOff) {
136 printHex32(CPUBitmask, OS);
137 OS << ',' << CPUTopSavedRegOff << '\n';
140 void MipsTargetAsmStreamer::emitFMask(unsigned FPUBitmask,
141 int FPUTopSavedRegOff) {
143 printHex32(FPUBitmask, OS);
144 OS << "," << FPUTopSavedRegOff << '\n';
147 // This part is for ELF object output.
148 MipsTargetELFStreamer::MipsTargetELFStreamer(MCStreamer &S,
149 const MCSubtargetInfo &STI)
150 : MipsTargetStreamer(S), MicroMipsEnabled(false), STI(STI) {
151 MCAssembler &MCA = getStreamer().getAssembler();
152 uint64_t Features = STI.getFeatureBits();
153 Triple T(STI.getTargetTriple());
154 Pic = (MCA.getContext().getObjectFileInfo()->getRelocM() == Reloc::PIC_)
158 // Update e_header flags
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;
166 else if (Features & Mips::FeatureMips4)
167 EFlags |= ELF::EF_MIPS_ARCH_4;
168 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;
173 if (T.isArch64Bit()) {
174 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 */
180 // No need to set any bit for N64 which is the default ABI at the moment
181 // for 64-bit Mips architectures.
183 if (Features & Mips::FeatureMips64r2 || Features & Mips::FeatureMips64)
184 EFlags |= ELF::EF_MIPS_32BITMODE;
187 EFlags |= ELF::EF_MIPS_ABI_O32;
191 if (Features & Mips::FeatureNaN2008)
192 EFlags |= ELF::EF_MIPS_NAN2008;
194 MCA.setELFHeaderEFlags(EFlags);
197 void MipsTargetELFStreamer::emitLabel(MCSymbol *Symbol) {
198 if (!isMicroMipsEnabled())
200 MCSymbolData &Data = getStreamer().getOrCreateSymbolData(Symbol);
201 uint8_t Type = MCELF::GetType(Data);
202 if (Type != ELF::STT_FUNC)
205 // 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.
208 MCELF::setOther(Data, ELF::STO_MIPS_MICROMIPS >> 2);
211 void 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();
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);
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
236 const MCSectionELF *Sec =
237 Context.getELFSection(".reginfo", ELF::SHT_MIPS_REGINFO, ELF::SHF_ALLOC,
238 SectionKind::getMetadata());
239 OS.SwitchSection(Sec);
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
250 void 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)
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)))
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);
270 MCELFStreamer &MipsTargetELFStreamer::getStreamer() {
271 return static_cast<MCELFStreamer &>(Streamer);
274 void MipsTargetELFStreamer::emitDirectiveSetMicroMips() {
275 MicroMipsEnabled = true;
277 MCAssembler &MCA = getStreamer().getAssembler();
278 unsigned Flags = MCA.getELFHeaderEFlags();
279 Flags |= ELF::EF_MIPS_MICROMIPS;
280 MCA.setELFHeaderEFlags(Flags);
283 void MipsTargetELFStreamer::emitDirectiveSetNoMicroMips() {
284 MicroMipsEnabled = false;
287 void MipsTargetELFStreamer::emitDirectiveSetMips16() {
288 MCAssembler &MCA = getStreamer().getAssembler();
289 unsigned Flags = MCA.getELFHeaderEFlags();
290 Flags |= ELF::EF_MIPS_ARCH_ASE_M16;
291 MCA.setELFHeaderEFlags(Flags);
294 void MipsTargetELFStreamer::emitDirectiveSetNoMips16() {
298 void MipsTargetELFStreamer::emitDirectiveSetReorder() {
302 void MipsTargetELFStreamer::emitDirectiveSetNoReorder() {
303 MCAssembler &MCA = getStreamer().getAssembler();
304 unsigned Flags = MCA.getELFHeaderEFlags();
305 Flags |= ELF::EF_MIPS_NOREORDER;
306 MCA.setELFHeaderEFlags(Flags);
309 void MipsTargetELFStreamer::emitDirectiveSetMacro() {
313 void MipsTargetELFStreamer::emitDirectiveSetNoMacro() {
317 void MipsTargetELFStreamer::emitDirectiveSetAt() {
321 void MipsTargetELFStreamer::emitDirectiveSetNoAt() {
325 void MipsTargetELFStreamer::emitDirectiveEnd(StringRef Name) {
329 void MipsTargetELFStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {
333 void MipsTargetELFStreamer::emitDirectiveAbiCalls() {
334 MCAssembler &MCA = getStreamer().getAssembler();
335 unsigned Flags = MCA.getELFHeaderEFlags();
336 Flags |= ELF::EF_MIPS_CPIC | ELF::EF_MIPS_PIC;
337 MCA.setELFHeaderEFlags(Flags);
340 void MipsTargetELFStreamer::emitDirectiveNaN2008() {
341 MCAssembler &MCA = getStreamer().getAssembler();
342 unsigned Flags = MCA.getELFHeaderEFlags();
343 Flags |= ELF::EF_MIPS_NAN2008;
344 MCA.setELFHeaderEFlags(Flags);
347 void MipsTargetELFStreamer::emitDirectiveNaNLegacy() {
348 MCAssembler &MCA = getStreamer().getAssembler();
349 unsigned Flags = MCA.getELFHeaderEFlags();
350 Flags &= ~ELF::EF_MIPS_NAN2008;
351 MCA.setELFHeaderEFlags(Flags);
354 void MipsTargetELFStreamer::emitDirectiveOptionPic0() {
355 MCAssembler &MCA = getStreamer().getAssembler();
356 unsigned Flags = MCA.getELFHeaderEFlags();
357 // This option overrides other PIC options like -KPIC.
359 Flags &= ~ELF::EF_MIPS_PIC;
360 MCA.setELFHeaderEFlags(Flags);
363 void MipsTargetELFStreamer::emitDirectiveOptionPic2() {
364 MCAssembler &MCA = getStreamer().getAssembler();
365 unsigned Flags = MCA.getELFHeaderEFlags();
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);
375 void MipsTargetELFStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
376 unsigned ReturnReg) {
380 void MipsTargetELFStreamer::emitMask(unsigned CPUBitmask,
381 int CPUTopSavedRegOff) {
385 void MipsTargetELFStreamer::emitFMask(unsigned FPUBitmask,
386 int FPUTopSavedRegOff) {
390 void MipsTargetELFStreamer::emitDirectiveSetMips32R2() {
391 // No action required for ELF output.
394 void MipsTargetELFStreamer::emitDirectiveSetMips64() {
395 // No action required for ELF output.
398 void MipsTargetELFStreamer::emitDirectiveSetMips64R2() {
399 // No action required for ELF output.
402 void MipsTargetELFStreamer::emitDirectiveSetDsp() {
403 // No action required for ELF output.