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::emitFrame(unsigned StackReg, unsigned StackSize,
95 << StringRef(MipsInstPrinter::getRegisterName(StackReg)).lower() << ","
97 << StringRef(MipsInstPrinter::getRegisterName(ReturnReg)).lower() << '\n';
100 void MipsTargetAsmStreamer::emitDirectiveSetMips32R2() {
101 OS << "\t.set\tmips32r2\n";
104 // Print a 32 bit hex number with all numbers.
105 static void printHex32(unsigned Value, raw_ostream &OS) {
107 for (int i = 7; i >= 0; i--)
108 OS.write_hex((Value & (0xF << (i*4))) >> (i*4));
111 void MipsTargetAsmStreamer::emitMask(unsigned CPUBitmask,
112 int CPUTopSavedRegOff) {
114 printHex32(CPUBitmask, OS);
115 OS << ',' << CPUTopSavedRegOff << '\n';
118 void MipsTargetAsmStreamer::emitFMask(unsigned FPUBitmask,
119 int FPUTopSavedRegOff) {
121 printHex32(FPUBitmask, OS);
122 OS << "," << FPUTopSavedRegOff << '\n';
125 // This part is for ELF object output.
126 MipsTargetELFStreamer::MipsTargetELFStreamer(MCStreamer &S,
127 const MCSubtargetInfo &STI)
128 : MipsTargetStreamer(S), MicroMipsEnabled(false), STI(STI) {
129 MCAssembler &MCA = getStreamer().getAssembler();
130 uint64_t Features = STI.getFeatureBits();
131 Triple T(STI.getTargetTriple());
133 // Update e_header flags
137 if (Features & Mips::FeatureMips64r2)
138 EFlags |= ELF::EF_MIPS_ARCH_64R2;
139 else if (Features & Mips::FeatureMips64)
140 EFlags |= ELF::EF_MIPS_ARCH_64;
141 else if (Features & Mips::FeatureMips32r2)
142 EFlags |= ELF::EF_MIPS_ARCH_32R2;
143 else if (Features & Mips::FeatureMips32)
144 EFlags |= ELF::EF_MIPS_ARCH_32;
146 if (T.isArch64Bit()) {
147 if (Features & Mips::FeatureN32)
148 EFlags |= ELF::EF_MIPS_ABI2;
149 else if (Features & Mips::FeatureO32) {
150 EFlags |= ELF::EF_MIPS_ABI_O32;
151 EFlags |= ELF::EF_MIPS_32BITMODE; /* Compatibility Mode */
153 // No need to set any bit for N64 which is the default ABI at the moment
154 // for 64-bit Mips architectures.
156 if (Features & Mips::FeatureMips64r2 || Features & Mips::FeatureMips64)
157 EFlags |= ELF::EF_MIPS_32BITMODE;
160 EFlags |= ELF::EF_MIPS_ABI_O32;
163 MCA.setELFHeaderEFlags(EFlags);
166 void MipsTargetELFStreamer::emitLabel(MCSymbol *Symbol) {
167 if (!isMicroMipsEnabled())
169 MCSymbolData &Data = getStreamer().getOrCreateSymbolData(Symbol);
170 uint8_t Type = MCELF::GetType(Data);
171 if (Type != ELF::STT_FUNC)
174 // The "other" values are stored in the last 6 bits of the second byte
175 // The traditional defines for STO values assume the full byte and thus
176 // the shift to pack it.
177 MCELF::setOther(Data, ELF::STO_MIPS_MICROMIPS >> 2);
180 void MipsTargetELFStreamer::finish() {
181 MCAssembler &MCA = getStreamer().getAssembler();
182 MCContext &Context = MCA.getContext();
183 MCStreamer &OS = getStreamer();
184 Triple T(STI.getTargetTriple());
185 uint64_t Features = STI.getFeatureBits();
187 if (T.isArch64Bit() && (Features & Mips::FeatureN64)) {
188 const MCSectionELF *Sec = Context.getELFSection(
189 ".MIPS.options", ELF::SHT_MIPS_OPTIONS,
190 ELF::SHF_ALLOC | ELF::SHF_MIPS_NOSTRIP, SectionKind::getMetadata());
191 OS.SwitchSection(Sec);
193 OS.EmitIntValue(1, 1); // kind
194 OS.EmitIntValue(40, 1); // size
195 OS.EmitIntValue(0, 2); // section
196 OS.EmitIntValue(0, 4); // info
197 OS.EmitIntValue(0, 4); // ri_gprmask
198 OS.EmitIntValue(0, 4); // pad
199 OS.EmitIntValue(0, 4); // ri_cpr[0]mask
200 OS.EmitIntValue(0, 4); // ri_cpr[1]mask
201 OS.EmitIntValue(0, 4); // ri_cpr[2]mask
202 OS.EmitIntValue(0, 4); // ri_cpr[3]mask
203 OS.EmitIntValue(0, 8); // ri_gp_value
205 const MCSectionELF *Sec =
206 Context.getELFSection(".reginfo", ELF::SHT_MIPS_REGINFO, ELF::SHF_ALLOC,
207 SectionKind::getMetadata());
208 OS.SwitchSection(Sec);
210 OS.EmitIntValue(0, 4); // ri_gprmask
211 OS.EmitIntValue(0, 4); // ri_cpr[0]mask
212 OS.EmitIntValue(0, 4); // ri_cpr[1]mask
213 OS.EmitIntValue(0, 4); // ri_cpr[2]mask
214 OS.EmitIntValue(0, 4); // ri_cpr[3]mask
215 OS.EmitIntValue(0, 4); // ri_gp_value
219 MCELFStreamer &MipsTargetELFStreamer::getStreamer() {
220 return static_cast<MCELFStreamer &>(Streamer);
223 void MipsTargetELFStreamer::emitDirectiveSetMicroMips() {
224 MicroMipsEnabled = true;
226 MCAssembler &MCA = getStreamer().getAssembler();
227 unsigned Flags = MCA.getELFHeaderEFlags();
228 Flags |= ELF::EF_MIPS_MICROMIPS;
229 MCA.setELFHeaderEFlags(Flags);
232 void MipsTargetELFStreamer::emitDirectiveSetNoMicroMips() {
233 MicroMipsEnabled = false;
236 void MipsTargetELFStreamer::emitDirectiveSetMips16() {
237 MCAssembler &MCA = getStreamer().getAssembler();
238 unsigned Flags = MCA.getELFHeaderEFlags();
239 Flags |= ELF::EF_MIPS_ARCH_ASE_M16;
240 MCA.setELFHeaderEFlags(Flags);
243 void MipsTargetELFStreamer::emitDirectiveSetNoMips16() {
247 void MipsTargetELFStreamer::emitDirectiveSetReorder() {
251 void MipsTargetELFStreamer::emitDirectiveSetNoReorder() {
252 MCAssembler &MCA = getStreamer().getAssembler();
253 unsigned Flags = MCA.getELFHeaderEFlags();
254 Flags |= ELF::EF_MIPS_NOREORDER;
255 MCA.setELFHeaderEFlags(Flags);
258 void MipsTargetELFStreamer::emitDirectiveSetMacro() {
262 void MipsTargetELFStreamer::emitDirectiveSetNoMacro() {
266 void MipsTargetELFStreamer::emitDirectiveSetAt() {
270 void MipsTargetELFStreamer::emitDirectiveSetNoAt() {
274 void MipsTargetELFStreamer::emitDirectiveEnd(StringRef Name) {
278 void MipsTargetELFStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {
282 void MipsTargetELFStreamer::emitDirectiveAbiCalls() {
283 MCAssembler &MCA = getStreamer().getAssembler();
284 unsigned Flags = MCA.getELFHeaderEFlags();
285 Flags |= ELF::EF_MIPS_CPIC | ELF::EF_MIPS_PIC;
286 MCA.setELFHeaderEFlags(Flags);
288 void MipsTargetELFStreamer::emitDirectiveOptionPic0() {
289 MCAssembler &MCA = getStreamer().getAssembler();
290 unsigned Flags = MCA.getELFHeaderEFlags();
291 Flags &= ~ELF::EF_MIPS_PIC;
292 MCA.setELFHeaderEFlags(Flags);
295 void MipsTargetELFStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
296 unsigned ReturnReg) {
300 void MipsTargetELFStreamer::emitMask(unsigned CPUBitmask,
301 int CPUTopSavedRegOff) {
305 void MipsTargetELFStreamer::emitFMask(unsigned FPUBitmask,
306 int FPUTopSavedRegOff) {
310 void MipsTargetELFStreamer::emitDirectiveSetMips32R2() {
311 // No action required for ELF output.