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"; }
88 void MipsTargetAsmStreamer::emitDirectiveOptionPic0() {
89 OS << "\t.option\tpic0\n";
92 void MipsTargetAsmStreamer::emitDirectiveOptionPic2() {
93 OS << "\t.option\tpic2\n";
96 void MipsTargetAsmStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
99 << StringRef(MipsInstPrinter::getRegisterName(StackReg)).lower() << ","
101 << StringRef(MipsInstPrinter::getRegisterName(ReturnReg)).lower() << '\n';
104 void MipsTargetAsmStreamer::emitDirectiveSetMips32R2() {
105 OS << "\t.set\tmips32r2\n";
108 void MipsTargetAsmStreamer::emitDirectiveSetDsp() {
109 OS << "\t.set\tdsp\n";
111 // Print a 32 bit hex number with all numbers.
112 static void printHex32(unsigned Value, raw_ostream &OS) {
114 for (int i = 7; i >= 0; i--)
115 OS.write_hex((Value & (0xF << (i*4))) >> (i*4));
118 void MipsTargetAsmStreamer::emitMask(unsigned CPUBitmask,
119 int CPUTopSavedRegOff) {
121 printHex32(CPUBitmask, OS);
122 OS << ',' << CPUTopSavedRegOff << '\n';
125 void MipsTargetAsmStreamer::emitFMask(unsigned FPUBitmask,
126 int FPUTopSavedRegOff) {
128 printHex32(FPUBitmask, OS);
129 OS << "," << FPUTopSavedRegOff << '\n';
132 // This part is for ELF object output.
133 MipsTargetELFStreamer::MipsTargetELFStreamer(MCStreamer &S,
134 const MCSubtargetInfo &STI)
135 : MipsTargetStreamer(S), MicroMipsEnabled(false), STI(STI) {
136 MCAssembler &MCA = getStreamer().getAssembler();
137 uint64_t Features = STI.getFeatureBits();
138 Triple T(STI.getTargetTriple());
139 Pic = (MCA.getContext().getObjectFileInfo()->getRelocM() == Reloc::PIC_)
143 // Update e_header flags
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;
156 if (T.isArch64Bit()) {
157 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 */
163 // No need to set any bit for N64 which is the default ABI at the moment
164 // for 64-bit Mips architectures.
166 if (Features & Mips::FeatureMips64r2 || Features & Mips::FeatureMips64)
167 EFlags |= ELF::EF_MIPS_32BITMODE;
170 EFlags |= ELF::EF_MIPS_ABI_O32;
173 MCA.setELFHeaderEFlags(EFlags);
176 void MipsTargetELFStreamer::emitLabel(MCSymbol *Symbol) {
177 if (!isMicroMipsEnabled())
179 MCSymbolData &Data = getStreamer().getOrCreateSymbolData(Symbol);
180 uint8_t Type = MCELF::GetType(Data);
181 if (Type != ELF::STT_FUNC)
184 // 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.
187 MCELF::setOther(Data, ELF::STO_MIPS_MICROMIPS >> 2);
190 void 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();
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);
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
215 const MCSectionELF *Sec =
216 Context.getELFSection(".reginfo", ELF::SHT_MIPS_REGINFO, ELF::SHF_ALLOC,
217 SectionKind::getMetadata());
218 OS.SwitchSection(Sec);
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
229 void 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)
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)))
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);
249 MCELFStreamer &MipsTargetELFStreamer::getStreamer() {
250 return static_cast<MCELFStreamer &>(Streamer);
253 void MipsTargetELFStreamer::emitDirectiveSetMicroMips() {
254 MicroMipsEnabled = true;
256 MCAssembler &MCA = getStreamer().getAssembler();
257 unsigned Flags = MCA.getELFHeaderEFlags();
258 Flags |= ELF::EF_MIPS_MICROMIPS;
259 MCA.setELFHeaderEFlags(Flags);
262 void MipsTargetELFStreamer::emitDirectiveSetNoMicroMips() {
263 MicroMipsEnabled = false;
266 void MipsTargetELFStreamer::emitDirectiveSetMips16() {
267 MCAssembler &MCA = getStreamer().getAssembler();
268 unsigned Flags = MCA.getELFHeaderEFlags();
269 Flags |= ELF::EF_MIPS_ARCH_ASE_M16;
270 MCA.setELFHeaderEFlags(Flags);
273 void MipsTargetELFStreamer::emitDirectiveSetNoMips16() {
277 void MipsTargetELFStreamer::emitDirectiveSetReorder() {
281 void MipsTargetELFStreamer::emitDirectiveSetNoReorder() {
282 MCAssembler &MCA = getStreamer().getAssembler();
283 unsigned Flags = MCA.getELFHeaderEFlags();
284 Flags |= ELF::EF_MIPS_NOREORDER;
285 MCA.setELFHeaderEFlags(Flags);
288 void MipsTargetELFStreamer::emitDirectiveSetMacro() {
292 void MipsTargetELFStreamer::emitDirectiveSetNoMacro() {
296 void MipsTargetELFStreamer::emitDirectiveSetAt() {
300 void MipsTargetELFStreamer::emitDirectiveSetNoAt() {
304 void MipsTargetELFStreamer::emitDirectiveEnd(StringRef Name) {
308 void MipsTargetELFStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {
312 void MipsTargetELFStreamer::emitDirectiveAbiCalls() {
313 MCAssembler &MCA = getStreamer().getAssembler();
314 unsigned Flags = MCA.getELFHeaderEFlags();
315 Flags |= ELF::EF_MIPS_CPIC | ELF::EF_MIPS_PIC;
316 MCA.setELFHeaderEFlags(Flags);
318 void MipsTargetELFStreamer::emitDirectiveOptionPic0() {
319 MCAssembler &MCA = getStreamer().getAssembler();
320 unsigned Flags = MCA.getELFHeaderEFlags();
321 // This option overrides other PIC options like -KPIC.
323 Flags &= ~ELF::EF_MIPS_PIC;
324 MCA.setELFHeaderEFlags(Flags);
327 void MipsTargetELFStreamer::emitDirectiveOptionPic2() {
328 MCAssembler &MCA = getStreamer().getAssembler();
329 unsigned Flags = MCA.getELFHeaderEFlags();
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);
339 void MipsTargetELFStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
340 unsigned ReturnReg) {
344 void MipsTargetELFStreamer::emitMask(unsigned CPUBitmask,
345 int CPUTopSavedRegOff) {
349 void MipsTargetELFStreamer::emitFMask(unsigned FPUBitmask,
350 int FPUTopSavedRegOff) {
354 void MipsTargetELFStreamer::emitDirectiveSetMips32R2() {
355 // No action required for ELF output.
358 void MipsTargetELFStreamer::emitDirectiveSetDsp() {
359 // No action required for ELF output.