blob: 825c164472231edae6e40a4ef261af9c3ace56f0 [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"
Rafael Espindola972e71a2014-01-31 23:10:26 +000015#include "MipsTargetObjectFile.h"
Jack Carter86ac5c12013-11-18 23:55:27 +000016#include "MipsTargetStreamer.h"
Rafael Espindolacb1953f2014-01-26 06:57:13 +000017#include "MipsMCTargetDesc.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"
Jack Carter86ac5c12013-11-18 23:55:27 +000021#include "llvm/MC/MCSymbol.h"
Rafael Espindolacb1953f2014-01-26 06:57:13 +000022#include "llvm/MC/MCSubtargetInfo.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
Rafael Espindola054234f2014-01-27 03:53:56 +000092void MipsTargetAsmStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
93 unsigned ReturnReg) {
94 OS << "\t.frame\t$"
95 << StringRef(MipsInstPrinter::getRegisterName(StackReg)).lower() << ","
96 << StackSize << ",$"
Rafael Espindola25fa2912014-01-27 04:33:11 +000097 << StringRef(MipsInstPrinter::getRegisterName(ReturnReg)).lower() << '\n';
98}
99
100// Print a 32 bit hex number with all numbers.
101static void printHex32(unsigned Value, raw_ostream &OS) {
102 OS << "0x";
103 for (int i = 7; i >= 0; i--)
104 OS.write_hex((Value & (0xF << (i*4))) >> (i*4));
105}
106
107void MipsTargetAsmStreamer::emitMask(unsigned CPUBitmask,
108 int CPUTopSavedRegOff) {
109 OS << "\t.mask \t";
110 printHex32(CPUBitmask, OS);
111 OS << ',' << CPUTopSavedRegOff << '\n';
112}
113
114void MipsTargetAsmStreamer::emitFMask(unsigned FPUBitmask,
115 int FPUTopSavedRegOff) {
116 OS << "\t.fmask\t";
117 printHex32(FPUBitmask, OS);
118 OS << "," << FPUTopSavedRegOff << '\n';
Rafael Espindola054234f2014-01-27 03:53:56 +0000119}
120
Jack Carter0cd3c192014-01-06 23:27:31 +0000121// This part is for ELF object output.
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000122MipsTargetELFStreamer::MipsTargetELFStreamer(MCStreamer &S,
123 const MCSubtargetInfo &STI)
Rafael Espindola972e71a2014-01-31 23:10:26 +0000124 : MipsTargetStreamer(S), MicroMipsEnabled(false), STI(STI) {
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000125 MCAssembler &MCA = getStreamer().getAssembler();
126 uint64_t Features = STI.getFeatureBits();
127 Triple T(STI.getTargetTriple());
128
129 // Update e_header flags
130 unsigned EFlags = 0;
131
132 // Architecture
133 if (Features & Mips::FeatureMips64r2)
134 EFlags |= ELF::EF_MIPS_ARCH_64R2;
135 else if (Features & Mips::FeatureMips64)
136 EFlags |= ELF::EF_MIPS_ARCH_64;
137 else if (Features & Mips::FeatureMips32r2)
138 EFlags |= ELF::EF_MIPS_ARCH_32R2;
139 else if (Features & Mips::FeatureMips32)
140 EFlags |= ELF::EF_MIPS_ARCH_32;
141
142 if (T.isArch64Bit()) {
Matheus Almeida2e03f242014-01-28 19:24:11 +0000143 if (Features & Mips::FeatureN32)
144 EFlags |= ELF::EF_MIPS_ABI2;
145 else if (Features & Mips::FeatureO32) {
146 EFlags |= ELF::EF_MIPS_ABI_O32;
147 EFlags |= ELF::EF_MIPS_32BITMODE; /* Compatibility Mode */
148 }
149 // No need to set any bit for N64 which is the default ABI at the moment
150 // for 64-bit Mips architectures.
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000151 } else {
152 if (Features & Mips::FeatureMips64r2 || Features & Mips::FeatureMips64)
153 EFlags |= ELF::EF_MIPS_32BITMODE;
154
155 // ABI
156 EFlags |= ELF::EF_MIPS_ABI_O32;
157 }
158
159 MCA.setELFHeaderEFlags(EFlags);
160}
Jack Carter86ac5c12013-11-18 23:55:27 +0000161
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000162void MipsTargetELFStreamer::emitLabel(MCSymbol *Symbol) {
Rafael Espindola26e917c2014-01-15 03:07:12 +0000163 if (!isMicroMipsEnabled())
164 return;
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000165 MCSymbolData &Data = getStreamer().getOrCreateSymbolData(Symbol);
Rafael Espindola26e917c2014-01-15 03:07:12 +0000166 uint8_t Type = MCELF::GetType(Data);
167 if (Type != ELF::STT_FUNC)
168 return;
169
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000170 // The "other" values are stored in the last 6 bits of the second byte
171 // The traditional defines for STO values assume the full byte and thus
172 // the shift to pack it.
Rafael Espindola26e917c2014-01-15 03:07:12 +0000173 MCELF::setOther(Data, ELF::STO_MIPS_MICROMIPS >> 2);
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000174}
175
Rafael Espindola972e71a2014-01-31 23:10:26 +0000176void MipsTargetELFStreamer::finish() {
177 MCAssembler &MCA = getStreamer().getAssembler();
178 MCContext &Context = MCA.getContext();
179 MCStreamer &OS = getStreamer();
180 Triple T(STI.getTargetTriple());
181 uint64_t Features = STI.getFeatureBits();
182
183 if (T.isArch64Bit() && (Features & Mips::FeatureN64)) {
184 const MCSectionELF *Sec = Context.getELFSection(
185 ".MIPS.options", ELF::SHT_MIPS_OPTIONS,
186 ELF::SHF_ALLOC | ELF::SHF_MIPS_NOSTRIP, SectionKind::getMetadata());
187 OS.SwitchSection(Sec);
188
189 OS.EmitIntValue(1, 1); // kind
190 OS.EmitIntValue(40, 1); // size
191 OS.EmitIntValue(0, 2); // section
192 OS.EmitIntValue(0, 4); // info
193 OS.EmitIntValue(0, 4); // ri_gprmask
194 OS.EmitIntValue(0, 4); // pad
195 OS.EmitIntValue(0, 4); // ri_cpr[0]mask
196 OS.EmitIntValue(0, 4); // ri_cpr[1]mask
197 OS.EmitIntValue(0, 4); // ri_cpr[2]mask
198 OS.EmitIntValue(0, 4); // ri_cpr[3]mask
199 OS.EmitIntValue(0, 8); // ri_gp_value
200 } else {
201 const MCSectionELF *Sec =
202 Context.getELFSection(".reginfo", ELF::SHT_MIPS_REGINFO, ELF::SHF_ALLOC,
203 SectionKind::getMetadata());
204 OS.SwitchSection(Sec);
205
206 OS.EmitIntValue(0, 4); // ri_gprmask
207 OS.EmitIntValue(0, 4); // ri_cpr[0]mask
208 OS.EmitIntValue(0, 4); // ri_cpr[1]mask
209 OS.EmitIntValue(0, 4); // ri_cpr[2]mask
210 OS.EmitIntValue(0, 4); // ri_cpr[3]mask
211 OS.EmitIntValue(0, 4); // ri_gp_value
212 }
213}
214
Jack Carter86ac5c12013-11-18 23:55:27 +0000215MCELFStreamer &MipsTargetELFStreamer::getStreamer() {
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000216 return static_cast<MCELFStreamer &>(Streamer);
Jack Carter86ac5c12013-11-18 23:55:27 +0000217}
218
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000219void MipsTargetELFStreamer::emitDirectiveSetMicroMips() {
220 MicroMipsEnabled = true;
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000221
222 MCAssembler &MCA = getStreamer().getAssembler();
223 unsigned Flags = MCA.getELFHeaderEFlags();
224 Flags |= ELF::EF_MIPS_MICROMIPS;
225 MCA.setELFHeaderEFlags(Flags);
Jack Carter86ac5c12013-11-18 23:55:27 +0000226}
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000227
228void MipsTargetELFStreamer::emitDirectiveSetNoMicroMips() {
229 MicroMipsEnabled = false;
230}
231
Rafael Espindola6633d572014-01-14 18:57:12 +0000232void MipsTargetELFStreamer::emitDirectiveSetMips16() {
Rafael Espindolae7583752014-01-24 16:13:20 +0000233 MCAssembler &MCA = getStreamer().getAssembler();
234 unsigned Flags = MCA.getELFHeaderEFlags();
235 Flags |= ELF::EF_MIPS_ARCH_ASE_M16;
236 MCA.setELFHeaderEFlags(Flags);
Rafael Espindola6633d572014-01-14 18:57:12 +0000237}
238
239void MipsTargetELFStreamer::emitDirectiveSetNoMips16() {
240 // FIXME: implement.
241}
242
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000243void MipsTargetELFStreamer::emitDirectiveSetReorder() {
244 // FIXME: implement.
245}
246
247void MipsTargetELFStreamer::emitDirectiveSetNoReorder() {
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000248 MCAssembler &MCA = getStreamer().getAssembler();
249 unsigned Flags = MCA.getELFHeaderEFlags();
250 Flags |= ELF::EF_MIPS_NOREORDER;
251 MCA.setELFHeaderEFlags(Flags);
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000252}
253
254void MipsTargetELFStreamer::emitDirectiveSetMacro() {
255 // FIXME: implement.
256}
257
258void MipsTargetELFStreamer::emitDirectiveSetNoMacro() {
259 // FIXME: implement.
260}
261
262void MipsTargetELFStreamer::emitDirectiveSetAt() {
263 // FIXME: implement.
264}
265
266void MipsTargetELFStreamer::emitDirectiveSetNoAt() {
267 // FIXME: implement.
268}
269
270void MipsTargetELFStreamer::emitDirectiveEnd(StringRef Name) {
271 // FIXME: implement.
272}
273
Rafael Espindola6633d572014-01-14 18:57:12 +0000274void MipsTargetELFStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {
275 // FIXME: implement.
276}
277
Jack Carter0cd3c192014-01-06 23:27:31 +0000278void MipsTargetELFStreamer::emitDirectiveAbiCalls() {
279 MCAssembler &MCA = getStreamer().getAssembler();
280 unsigned Flags = MCA.getELFHeaderEFlags();
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000281 Flags |= ELF::EF_MIPS_CPIC | ELF::EF_MIPS_PIC;
Jack Carter0cd3c192014-01-06 23:27:31 +0000282 MCA.setELFHeaderEFlags(Flags);
283}
284void MipsTargetELFStreamer::emitDirectiveOptionPic0() {
285 MCAssembler &MCA = getStreamer().getAssembler();
286 unsigned Flags = MCA.getELFHeaderEFlags();
287 Flags &= ~ELF::EF_MIPS_PIC;
288 MCA.setELFHeaderEFlags(Flags);
289}
Rafael Espindola054234f2014-01-27 03:53:56 +0000290
291void MipsTargetELFStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
292 unsigned ReturnReg) {
293 // FIXME: implement.
294}
Rafael Espindola25fa2912014-01-27 04:33:11 +0000295
296void MipsTargetELFStreamer::emitMask(unsigned CPUBitmask,
297 int CPUTopSavedRegOff) {
298 // FIXME: implement.
299}
300
301void MipsTargetELFStreamer::emitFMask(unsigned FPUBitmask,
302 int FPUTopSavedRegOff) {
303 // FIXME: implement.
304}