blob: 2a373ee094ca35d69688e63c0bb293b5dd3a0e10 [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"
Daniel Sanders68c37472014-07-21 13:30:55 +000015#include "MipsELFStreamer.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000016#include "MipsMCTargetDesc.h"
Rafael Espindola972e71a2014-01-31 23:10:26 +000017#include "MipsTargetObjectFile.h"
Jack Carter86ac5c12013-11-18 23:55:27 +000018#include "MipsTargetStreamer.h"
Rafael Espindola972e71a2014-01-31 23:10:26 +000019#include "llvm/MC/MCContext.h"
Jack Carter86ac5c12013-11-18 23:55:27 +000020#include "llvm/MC/MCELF.h"
Rafael Espindola972e71a2014-01-31 23:10:26 +000021#include "llvm/MC/MCSectionELF.h"
Rafael Espindolacb1953f2014-01-26 06:57:13 +000022#include "llvm/MC/MCSubtargetInfo.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000023#include "llvm/MC/MCSymbol.h"
Jack Carter86ac5c12013-11-18 23:55:27 +000024#include "llvm/Support/CommandLine.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000025#include "llvm/Support/ELF.h"
Jack Carter86ac5c12013-11-18 23:55:27 +000026#include "llvm/Support/ErrorHandling.h"
27#include "llvm/Support/FormattedStream.h"
28
29using namespace llvm;
30
Vladimir Medicfb8a2a92014-07-08 08:59:22 +000031MipsTargetStreamer::MipsTargetStreamer(MCStreamer &S)
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000032 : MCTargetStreamer(S), ModuleDirectiveAllowed(true) {
Daniel Sandersd97a6342014-08-13 10:07:34 +000033 GPRInfoSet = FPRInfoSet = FrameInfoSet = false;
34}
Rafael Espindola60890b82014-06-23 19:43:40 +000035void MipsTargetStreamer::emitDirectiveSetMicroMips() {}
36void MipsTargetStreamer::emitDirectiveSetNoMicroMips() {}
37void MipsTargetStreamer::emitDirectiveSetMips16() {}
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000038void MipsTargetStreamer::emitDirectiveSetNoMips16() { forbidModuleDirective(); }
39void MipsTargetStreamer::emitDirectiveSetReorder() { forbidModuleDirective(); }
Rafael Espindola60890b82014-06-23 19:43:40 +000040void MipsTargetStreamer::emitDirectiveSetNoReorder() {}
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000041void MipsTargetStreamer::emitDirectiveSetMacro() { forbidModuleDirective(); }
42void MipsTargetStreamer::emitDirectiveSetNoMacro() { forbidModuleDirective(); }
43void MipsTargetStreamer::emitDirectiveSetMsa() { forbidModuleDirective(); }
44void MipsTargetStreamer::emitDirectiveSetNoMsa() { forbidModuleDirective(); }
45void MipsTargetStreamer::emitDirectiveSetAt() { forbidModuleDirective(); }
46void MipsTargetStreamer::emitDirectiveSetNoAt() { forbidModuleDirective(); }
Rafael Espindola60890b82014-06-23 19:43:40 +000047void MipsTargetStreamer::emitDirectiveEnd(StringRef Name) {}
48void MipsTargetStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {}
49void MipsTargetStreamer::emitDirectiveAbiCalls() {}
50void MipsTargetStreamer::emitDirectiveNaN2008() {}
51void MipsTargetStreamer::emitDirectiveNaNLegacy() {}
52void MipsTargetStreamer::emitDirectiveOptionPic0() {}
53void MipsTargetStreamer::emitDirectiveOptionPic2() {}
54void MipsTargetStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
55 unsigned ReturnReg) {}
56void MipsTargetStreamer::emitMask(unsigned CPUBitmask, int CPUTopSavedRegOff) {}
57void MipsTargetStreamer::emitFMask(unsigned FPUBitmask, int FPUTopSavedRegOff) {
58}
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000059void MipsTargetStreamer::emitDirectiveSetMips1() { forbidModuleDirective(); }
60void MipsTargetStreamer::emitDirectiveSetMips2() { forbidModuleDirective(); }
61void MipsTargetStreamer::emitDirectiveSetMips3() { forbidModuleDirective(); }
62void MipsTargetStreamer::emitDirectiveSetMips4() { forbidModuleDirective(); }
63void MipsTargetStreamer::emitDirectiveSetMips5() { forbidModuleDirective(); }
64void MipsTargetStreamer::emitDirectiveSetMips32() { forbidModuleDirective(); }
65void MipsTargetStreamer::emitDirectiveSetMips32R2() { forbidModuleDirective(); }
66void MipsTargetStreamer::emitDirectiveSetMips32R6() { forbidModuleDirective(); }
67void MipsTargetStreamer::emitDirectiveSetMips64() { forbidModuleDirective(); }
68void MipsTargetStreamer::emitDirectiveSetMips64R2() { forbidModuleDirective(); }
69void MipsTargetStreamer::emitDirectiveSetMips64R6() { forbidModuleDirective(); }
70void MipsTargetStreamer::emitDirectiveSetDsp() { forbidModuleDirective(); }
Rafael Espindola60890b82014-06-23 19:43:40 +000071void MipsTargetStreamer::emitDirectiveCpload(unsigned RegNo) {}
72void MipsTargetStreamer::emitDirectiveCpsetup(unsigned RegNo, int RegOrOffset,
73 const MCSymbol &Sym, bool IsReg) {
74}
Daniel Sanders7e527422014-07-10 13:38:23 +000075void MipsTargetStreamer::emitDirectiveModuleOddSPReg(bool Enabled,
76 bool IsO32ABI) {
77 if (!Enabled && !IsO32ABI)
78 report_fatal_error("+nooddspreg is only valid for O32");
79}
Rafael Espindola24ea09e2014-01-26 06:06:37 +000080
81MipsTargetAsmStreamer::MipsTargetAsmStreamer(MCStreamer &S,
82 formatted_raw_ostream &OS)
83 : MipsTargetStreamer(S), OS(OS) {}
Jack Carter6ef6cc52013-11-19 20:53:28 +000084
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +000085void MipsTargetAsmStreamer::emitDirectiveSetMicroMips() {
86 OS << "\t.set\tmicromips\n";
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000087 forbidModuleDirective();
Jack Carter6ef6cc52013-11-19 20:53:28 +000088}
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +000089
90void MipsTargetAsmStreamer::emitDirectiveSetNoMicroMips() {
91 OS << "\t.set\tnomicromips\n";
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000092 forbidModuleDirective();
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +000093}
94
Rafael Espindola6633d572014-01-14 18:57:12 +000095void MipsTargetAsmStreamer::emitDirectiveSetMips16() {
96 OS << "\t.set\tmips16\n";
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000097 forbidModuleDirective();
Rafael Espindola6633d572014-01-14 18:57:12 +000098}
99
100void MipsTargetAsmStreamer::emitDirectiveSetNoMips16() {
101 OS << "\t.set\tnomips16\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000102 MipsTargetStreamer::emitDirectiveSetNoMips16();
Rafael Espindola6633d572014-01-14 18:57:12 +0000103}
104
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000105void MipsTargetAsmStreamer::emitDirectiveSetReorder() {
106 OS << "\t.set\treorder\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000107 MipsTargetStreamer::emitDirectiveSetReorder();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000108}
109
110void MipsTargetAsmStreamer::emitDirectiveSetNoReorder() {
111 OS << "\t.set\tnoreorder\n";
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000112 forbidModuleDirective();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000113}
114
115void MipsTargetAsmStreamer::emitDirectiveSetMacro() {
116 OS << "\t.set\tmacro\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000117 MipsTargetStreamer::emitDirectiveSetMacro();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000118}
119
120void MipsTargetAsmStreamer::emitDirectiveSetNoMacro() {
121 OS << "\t.set\tnomacro\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000122 MipsTargetStreamer::emitDirectiveSetNoMacro();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000123}
124
Daniel Sanders44934432014-08-07 12:03:36 +0000125void MipsTargetAsmStreamer::emitDirectiveSetMsa() {
126 OS << "\t.set\tmsa\n";
127 MipsTargetStreamer::emitDirectiveSetMsa();
128}
129
130void MipsTargetAsmStreamer::emitDirectiveSetNoMsa() {
131 OS << "\t.set\tnomsa\n";
132 MipsTargetStreamer::emitDirectiveSetNoMsa();
133}
134
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000135void MipsTargetAsmStreamer::emitDirectiveSetAt() {
136 OS << "\t.set\tat\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000137 MipsTargetStreamer::emitDirectiveSetAt();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000138}
139
140void MipsTargetAsmStreamer::emitDirectiveSetNoAt() {
141 OS << "\t.set\tnoat\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000142 MipsTargetStreamer::emitDirectiveSetNoAt();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000143}
144
145void MipsTargetAsmStreamer::emitDirectiveEnd(StringRef Name) {
146 OS << "\t.end\t" << Name << '\n';
147}
148
Rafael Espindola6633d572014-01-14 18:57:12 +0000149void MipsTargetAsmStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {
150 OS << "\t.ent\t" << Symbol.getName() << '\n';
151}
152
Jack Carter0cd3c192014-01-06 23:27:31 +0000153void MipsTargetAsmStreamer::emitDirectiveAbiCalls() { OS << "\t.abicalls\n"; }
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000154
155void MipsTargetAsmStreamer::emitDirectiveNaN2008() { OS << "\t.nan\t2008\n"; }
156
157void MipsTargetAsmStreamer::emitDirectiveNaNLegacy() {
158 OS << "\t.nan\tlegacy\n";
159}
160
Jack Carter0cd3c192014-01-06 23:27:31 +0000161void MipsTargetAsmStreamer::emitDirectiveOptionPic0() {
162 OS << "\t.option\tpic0\n";
163}
164
Matheus Almeidaf79b2812014-03-26 13:40:29 +0000165void MipsTargetAsmStreamer::emitDirectiveOptionPic2() {
166 OS << "\t.option\tpic2\n";
167}
168
Rafael Espindola054234f2014-01-27 03:53:56 +0000169void MipsTargetAsmStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
170 unsigned ReturnReg) {
171 OS << "\t.frame\t$"
172 << StringRef(MipsInstPrinter::getRegisterName(StackReg)).lower() << ","
173 << StackSize << ",$"
Rafael Espindola25fa2912014-01-27 04:33:11 +0000174 << StringRef(MipsInstPrinter::getRegisterName(ReturnReg)).lower() << '\n';
175}
176
Daniel Sandersf0df2212014-08-04 12:20:00 +0000177void MipsTargetAsmStreamer::emitDirectiveSetMips1() {
178 OS << "\t.set\tmips1\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000179 MipsTargetStreamer::emitDirectiveSetMips1();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000180}
181
182void MipsTargetAsmStreamer::emitDirectiveSetMips2() {
183 OS << "\t.set\tmips2\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000184 MipsTargetStreamer::emitDirectiveSetMips2();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000185}
186
187void MipsTargetAsmStreamer::emitDirectiveSetMips3() {
188 OS << "\t.set\tmips3\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000189 MipsTargetStreamer::emitDirectiveSetMips3();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000190}
191
192void MipsTargetAsmStreamer::emitDirectiveSetMips4() {
193 OS << "\t.set\tmips4\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000194 MipsTargetStreamer::emitDirectiveSetMips4();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000195}
196
197void MipsTargetAsmStreamer::emitDirectiveSetMips5() {
198 OS << "\t.set\tmips5\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000199 MipsTargetStreamer::emitDirectiveSetMips5();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000200}
201
202void MipsTargetAsmStreamer::emitDirectiveSetMips32() {
203 OS << "\t.set\tmips32\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000204 MipsTargetStreamer::emitDirectiveSetMips32();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000205}
206
Vladimir Medic615b26e2014-03-04 09:54:09 +0000207void MipsTargetAsmStreamer::emitDirectiveSetMips32R2() {
208 OS << "\t.set\tmips32r2\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000209 MipsTargetStreamer::emitDirectiveSetMips32R2();
Vladimir Medic615b26e2014-03-04 09:54:09 +0000210}
211
Daniel Sandersf0df2212014-08-04 12:20:00 +0000212void MipsTargetAsmStreamer::emitDirectiveSetMips32R6() {
213 OS << "\t.set\tmips32r6\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000214 MipsTargetStreamer::emitDirectiveSetMips32R6();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000215}
216
Matheus Almeida3b9c63d2014-03-26 15:14:32 +0000217void MipsTargetAsmStreamer::emitDirectiveSetMips64() {
218 OS << "\t.set\tmips64\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000219 MipsTargetStreamer::emitDirectiveSetMips64();
Matheus Almeida3b9c63d2014-03-26 15:14:32 +0000220}
221
Matheus Almeidaa2cd0092014-03-26 14:52:22 +0000222void MipsTargetAsmStreamer::emitDirectiveSetMips64R2() {
223 OS << "\t.set\tmips64r2\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000224 MipsTargetStreamer::emitDirectiveSetMips64R2();
Matheus Almeidaa2cd0092014-03-26 14:52:22 +0000225}
226
Daniel Sandersf0df2212014-08-04 12:20:00 +0000227void MipsTargetAsmStreamer::emitDirectiveSetMips64R6() {
228 OS << "\t.set\tmips64r6\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000229 MipsTargetStreamer::emitDirectiveSetMips64R6();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000230}
231
Vladimir Medic27c398e2014-03-05 11:05:09 +0000232void MipsTargetAsmStreamer::emitDirectiveSetDsp() {
233 OS << "\t.set\tdsp\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000234 MipsTargetStreamer::emitDirectiveSetDsp();
Vladimir Medic27c398e2014-03-05 11:05:09 +0000235}
Rafael Espindola25fa2912014-01-27 04:33:11 +0000236// Print a 32 bit hex number with all numbers.
237static void printHex32(unsigned Value, raw_ostream &OS) {
238 OS << "0x";
239 for (int i = 7; i >= 0; i--)
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000240 OS.write_hex((Value & (0xF << (i * 4))) >> (i * 4));
Rafael Espindola25fa2912014-01-27 04:33:11 +0000241}
242
243void MipsTargetAsmStreamer::emitMask(unsigned CPUBitmask,
244 int CPUTopSavedRegOff) {
245 OS << "\t.mask \t";
246 printHex32(CPUBitmask, OS);
247 OS << ',' << CPUTopSavedRegOff << '\n';
248}
249
250void MipsTargetAsmStreamer::emitFMask(unsigned FPUBitmask,
251 int FPUTopSavedRegOff) {
252 OS << "\t.fmask\t";
253 printHex32(FPUBitmask, OS);
254 OS << "," << FPUTopSavedRegOff << '\n';
Rafael Espindola054234f2014-01-27 03:53:56 +0000255}
256
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000257void MipsTargetAsmStreamer::emitDirectiveCpload(unsigned RegNo) {
258 OS << "\t.cpload\t$"
259 << StringRef(MipsInstPrinter::getRegisterName(RegNo)).lower() << "\n";
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000260 forbidModuleDirective();
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000261}
262
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000263void MipsTargetAsmStreamer::emitDirectiveCpsetup(unsigned RegNo,
264 int RegOrOffset,
265 const MCSymbol &Sym,
266 bool IsReg) {
267 OS << "\t.cpsetup\t$"
268 << StringRef(MipsInstPrinter::getRegisterName(RegNo)).lower() << ", ";
269
270 if (IsReg)
271 OS << "$"
272 << StringRef(MipsInstPrinter::getRegisterName(RegOrOffset)).lower();
273 else
274 OS << RegOrOffset;
275
276 OS << ", ";
277
278 OS << Sym.getName() << "\n";
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000279 forbidModuleDirective();
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000280}
281
Daniel Sanders7e527422014-07-10 13:38:23 +0000282void MipsTargetAsmStreamer::emitDirectiveModuleFP(
283 MipsABIFlagsSection::FpABIKind Value, bool Is32BitABI) {
284 MipsTargetStreamer::emitDirectiveModuleFP(Value, Is32BitABI);
Daniel Sandersc7dbc632014-07-08 10:11:38 +0000285
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000286 StringRef ModuleValue;
287 OS << "\t.module\tfp=";
Daniel Sanders7e527422014-07-10 13:38:23 +0000288 OS << ABIFlagsSection.getFpABIString(Value) << "\n";
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000289}
290
Daniel Sanders7e527422014-07-10 13:38:23 +0000291void MipsTargetAsmStreamer::emitDirectiveSetFp(
292 MipsABIFlagsSection::FpABIKind Value) {
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000293 StringRef ModuleValue;
294 OS << "\t.set\tfp=";
Daniel Sanders7e527422014-07-10 13:38:23 +0000295 OS << ABIFlagsSection.getFpABIString(Value) << "\n";
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000296}
297
298void MipsTargetAsmStreamer::emitMipsAbiFlags() {
299 // No action required for text output.
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000300}
301
Daniel Sanders7e527422014-07-10 13:38:23 +0000302void MipsTargetAsmStreamer::emitDirectiveModuleOddSPReg(bool Enabled,
303 bool IsO32ABI) {
304 MipsTargetStreamer::emitDirectiveModuleOddSPReg(Enabled, IsO32ABI);
305
306 OS << "\t.module\t" << (Enabled ? "" : "no") << "oddspreg\n";
307}
308
Jack Carter0cd3c192014-01-06 23:27:31 +0000309// This part is for ELF object output.
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000310MipsTargetELFStreamer::MipsTargetELFStreamer(MCStreamer &S,
311 const MCSubtargetInfo &STI)
Rafael Espindola972e71a2014-01-31 23:10:26 +0000312 : MipsTargetStreamer(S), MicroMipsEnabled(false), STI(STI) {
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000313 MCAssembler &MCA = getStreamer().getAssembler();
314 uint64_t Features = STI.getFeatureBits();
315 Triple T(STI.getTargetTriple());
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000316 Pic = (MCA.getContext().getObjectFileInfo()->getRelocM() == Reloc::PIC_)
Matheus Almeidaf79b2812014-03-26 13:40:29 +0000317 ? true
318 : false;
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000319
320 // Update e_header flags
321 unsigned EFlags = 0;
322
323 // Architecture
Daniel Sanders950f48d2014-07-04 15:21:53 +0000324 if (Features & Mips::FeatureMips64r6)
325 EFlags |= ELF::EF_MIPS_ARCH_64R6;
326 else if (Features & Mips::FeatureMips64r2)
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000327 EFlags |= ELF::EF_MIPS_ARCH_64R2;
328 else if (Features & Mips::FeatureMips64)
329 EFlags |= ELF::EF_MIPS_ARCH_64;
Daniel Sanders950f48d2014-07-04 15:21:53 +0000330 else if (Features & Mips::FeatureMips5)
331 EFlags |= ELF::EF_MIPS_ARCH_5;
Daniel Sandersf7b32292014-04-03 12:13:36 +0000332 else if (Features & Mips::FeatureMips4)
333 EFlags |= ELF::EF_MIPS_ARCH_4;
Daniel Sanders950f48d2014-07-04 15:21:53 +0000334 else if (Features & Mips::FeatureMips3)
335 EFlags |= ELF::EF_MIPS_ARCH_3;
336 else if (Features & Mips::FeatureMips32r6)
337 EFlags |= ELF::EF_MIPS_ARCH_32R6;
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000338 else if (Features & Mips::FeatureMips32r2)
339 EFlags |= ELF::EF_MIPS_ARCH_32R2;
340 else if (Features & Mips::FeatureMips32)
341 EFlags |= ELF::EF_MIPS_ARCH_32;
Daniel Sanders950f48d2014-07-04 15:21:53 +0000342 else if (Features & Mips::FeatureMips2)
343 EFlags |= ELF::EF_MIPS_ARCH_2;
344 else
345 EFlags |= ELF::EF_MIPS_ARCH_1;
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000346
Daniel Sanders7f705732014-07-17 10:02:08 +0000347 // ABI
348 // N64 does not require any ABI bits.
349 if (Features & Mips::FeatureO32)
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000350 EFlags |= ELF::EF_MIPS_ABI_O32;
Daniel Sanders7f705732014-07-17 10:02:08 +0000351 else if (Features & Mips::FeatureN32)
352 EFlags |= ELF::EF_MIPS_ABI2;
353
354 if (Features & Mips::FeatureGP64Bit) {
355 if (Features & Mips::FeatureO32)
356 EFlags |= ELF::EF_MIPS_32BITMODE; /* Compatibility Mode */
357 } else if (Features & Mips::FeatureMips64r2 || Features & Mips::FeatureMips64)
358 EFlags |= ELF::EF_MIPS_32BITMODE;
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000359
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000360 // Other options.
361 if (Features & Mips::FeatureNaN2008)
362 EFlags |= ELF::EF_MIPS_NAN2008;
363
Daniel Sanders16ec6c12014-07-17 09:52:56 +0000364 // -mabicalls and -mplt are not implemented but we should act as if they were
365 // given.
366 EFlags |= ELF::EF_MIPS_CPIC;
367 if (Features & Mips::FeatureN64)
368 EFlags |= ELF::EF_MIPS_PIC;
369
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000370 MCA.setELFHeaderEFlags(EFlags);
371}
Jack Carter86ac5c12013-11-18 23:55:27 +0000372
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000373void MipsTargetELFStreamer::emitLabel(MCSymbol *Symbol) {
Rafael Espindola26e917c2014-01-15 03:07:12 +0000374 if (!isMicroMipsEnabled())
375 return;
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000376 MCSymbolData &Data = getStreamer().getOrCreateSymbolData(Symbol);
Rafael Espindola26e917c2014-01-15 03:07:12 +0000377 uint8_t Type = MCELF::GetType(Data);
378 if (Type != ELF::STT_FUNC)
379 return;
380
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000381 // The "other" values are stored in the last 6 bits of the second byte
382 // The traditional defines for STO values assume the full byte and thus
383 // the shift to pack it.
Rafael Espindola26e917c2014-01-15 03:07:12 +0000384 MCELF::setOther(Data, ELF::STO_MIPS_MICROMIPS >> 2);
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000385}
386
Rafael Espindola972e71a2014-01-31 23:10:26 +0000387void MipsTargetELFStreamer::finish() {
388 MCAssembler &MCA = getStreamer().getAssembler();
Daniel Sanders68c37472014-07-21 13:30:55 +0000389 const MCObjectFileInfo &OFI = *MCA.getContext().getObjectFileInfo();
Rafael Espindola972e71a2014-01-31 23:10:26 +0000390
Daniel Sanders41ffa5d2014-07-14 15:05:51 +0000391 // .bss, .text and .data are always at least 16-byte aligned.
392 MCSectionData &TextSectionData =
393 MCA.getOrCreateSectionData(*OFI.getTextSection());
394 MCSectionData &DataSectionData =
395 MCA.getOrCreateSectionData(*OFI.getDataSection());
396 MCSectionData &BSSSectionData =
397 MCA.getOrCreateSectionData(*OFI.getBSSSection());
398
399 TextSectionData.setAlignment(std::max(16u, TextSectionData.getAlignment()));
400 DataSectionData.setAlignment(std::max(16u, DataSectionData.getAlignment()));
401 BSSSectionData.setAlignment(std::max(16u, BSSSectionData.getAlignment()));
402
Daniel Sanders68c37472014-07-21 13:30:55 +0000403 // Emit all the option records.
404 // At the moment we are only emitting .Mips.options (ODK_REGINFO) and
405 // .reginfo.
406 MipsELFStreamer &MEF = static_cast<MipsELFStreamer &>(Streamer);
407 MEF.EmitMipsOptionRecords();
Rafael Espindola972e71a2014-01-31 23:10:26 +0000408
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000409 emitMipsAbiFlags();
Rafael Espindola972e71a2014-01-31 23:10:26 +0000410}
411
Zoran Jovanovic28221d82014-03-20 09:44:49 +0000412void MipsTargetELFStreamer::emitAssignment(MCSymbol *Symbol,
413 const MCExpr *Value) {
414 // If on rhs is micromips symbol then mark Symbol as microMips.
415 if (Value->getKind() != MCExpr::SymbolRef)
416 return;
417 const MCSymbol &RhsSym =
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000418 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Zoran Jovanovic28221d82014-03-20 09:44:49 +0000419 MCSymbolData &Data = getStreamer().getOrCreateSymbolData(&RhsSym);
420 uint8_t Type = MCELF::GetType(Data);
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000421 if ((Type != ELF::STT_FUNC) ||
422 !(MCELF::getOther(Data) & (ELF::STO_MIPS_MICROMIPS >> 2)))
Zoran Jovanovic28221d82014-03-20 09:44:49 +0000423 return;
424
425 MCSymbolData &SymbolData = getStreamer().getOrCreateSymbolData(Symbol);
426 // The "other" values are stored in the last 6 bits of the second byte.
427 // The traditional defines for STO values assume the full byte and thus
428 // the shift to pack it.
429 MCELF::setOther(SymbolData, ELF::STO_MIPS_MICROMIPS >> 2);
430}
431
Jack Carter86ac5c12013-11-18 23:55:27 +0000432MCELFStreamer &MipsTargetELFStreamer::getStreamer() {
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000433 return static_cast<MCELFStreamer &>(Streamer);
Jack Carter86ac5c12013-11-18 23:55:27 +0000434}
435
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000436void MipsTargetELFStreamer::emitDirectiveSetMicroMips() {
437 MicroMipsEnabled = true;
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000438
439 MCAssembler &MCA = getStreamer().getAssembler();
440 unsigned Flags = MCA.getELFHeaderEFlags();
441 Flags |= ELF::EF_MIPS_MICROMIPS;
442 MCA.setELFHeaderEFlags(Flags);
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000443 forbidModuleDirective();
Jack Carter86ac5c12013-11-18 23:55:27 +0000444}
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000445
446void MipsTargetELFStreamer::emitDirectiveSetNoMicroMips() {
447 MicroMipsEnabled = false;
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000448 forbidModuleDirective();
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000449}
450
Rafael Espindola6633d572014-01-14 18:57:12 +0000451void MipsTargetELFStreamer::emitDirectiveSetMips16() {
Rafael Espindolae7583752014-01-24 16:13:20 +0000452 MCAssembler &MCA = getStreamer().getAssembler();
453 unsigned Flags = MCA.getELFHeaderEFlags();
454 Flags |= ELF::EF_MIPS_ARCH_ASE_M16;
455 MCA.setELFHeaderEFlags(Flags);
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000456 forbidModuleDirective();
Rafael Espindola6633d572014-01-14 18:57:12 +0000457}
458
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000459void MipsTargetELFStreamer::emitDirectiveSetNoReorder() {
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000460 MCAssembler &MCA = getStreamer().getAssembler();
461 unsigned Flags = MCA.getELFHeaderEFlags();
462 Flags |= ELF::EF_MIPS_NOREORDER;
463 MCA.setELFHeaderEFlags(Flags);
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000464 forbidModuleDirective();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000465}
466
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000467void MipsTargetELFStreamer::emitDirectiveEnd(StringRef Name) {
Daniel Sandersd97a6342014-08-13 10:07:34 +0000468 MCAssembler &MCA = getStreamer().getAssembler();
469 MCContext &Context = MCA.getContext();
470 MCStreamer &OS = getStreamer();
471
472 const MCSectionELF *Sec = Context.getELFSection(".pdr", ELF::SHT_PROGBITS,
473 ELF::SHF_ALLOC | ELF::SHT_REL,
474 SectionKind::getMetadata());
475
476 const MCSymbolRefExpr *ExprRef =
477 MCSymbolRefExpr::Create(Name, MCSymbolRefExpr::VK_None, Context);
478
479 MCSectionData &SecData = MCA.getOrCreateSectionData(*Sec);
480 SecData.setAlignment(4);
481
482 OS.PushSection();
483
484 OS.SwitchSection(Sec);
485
486 OS.EmitValueImpl(ExprRef, 4);
487
488 OS.EmitIntValue(GPRInfoSet ? GPRBitMask : 0, 4); // reg_mask
489 OS.EmitIntValue(GPRInfoSet ? GPROffset : 0, 4); // reg_offset
490
491 OS.EmitIntValue(FPRInfoSet ? FPRBitMask : 0, 4); // fpreg_mask
492 OS.EmitIntValue(FPRInfoSet ? FPROffset : 0, 4); // fpreg_offset
493
494 OS.EmitIntValue(FrameInfoSet ? FrameOffset : 0, 4); // frame_offset
495 OS.EmitIntValue(FrameInfoSet ? FrameReg : 0, 4); // frame_reg
496 OS.EmitIntValue(FrameInfoSet ? ReturnReg : 0, 4); // return_reg
497
498 // The .end directive marks the end of a procedure. Invalidate
499 // the information gathered up until this point.
500 GPRInfoSet = FPRInfoSet = FrameInfoSet = false;
501
502 OS.PopSection();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000503}
504
Rafael Espindola6633d572014-01-14 18:57:12 +0000505void MipsTargetELFStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {
Daniel Sandersd97a6342014-08-13 10:07:34 +0000506 GPRInfoSet = FPRInfoSet = FrameInfoSet = false;
Rafael Espindola6633d572014-01-14 18:57:12 +0000507}
508
Jack Carter0cd3c192014-01-06 23:27:31 +0000509void MipsTargetELFStreamer::emitDirectiveAbiCalls() {
510 MCAssembler &MCA = getStreamer().getAssembler();
511 unsigned Flags = MCA.getELFHeaderEFlags();
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000512 Flags |= ELF::EF_MIPS_CPIC | ELF::EF_MIPS_PIC;
Jack Carter0cd3c192014-01-06 23:27:31 +0000513 MCA.setELFHeaderEFlags(Flags);
514}
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000515
516void MipsTargetELFStreamer::emitDirectiveNaN2008() {
517 MCAssembler &MCA = getStreamer().getAssembler();
518 unsigned Flags = MCA.getELFHeaderEFlags();
519 Flags |= ELF::EF_MIPS_NAN2008;
520 MCA.setELFHeaderEFlags(Flags);
521}
522
523void MipsTargetELFStreamer::emitDirectiveNaNLegacy() {
524 MCAssembler &MCA = getStreamer().getAssembler();
525 unsigned Flags = MCA.getELFHeaderEFlags();
526 Flags &= ~ELF::EF_MIPS_NAN2008;
527 MCA.setELFHeaderEFlags(Flags);
528}
529
Jack Carter0cd3c192014-01-06 23:27:31 +0000530void MipsTargetELFStreamer::emitDirectiveOptionPic0() {
531 MCAssembler &MCA = getStreamer().getAssembler();
532 unsigned Flags = MCA.getELFHeaderEFlags();
Matheus Almeidaf79b2812014-03-26 13:40:29 +0000533 // This option overrides other PIC options like -KPIC.
534 Pic = false;
Jack Carter0cd3c192014-01-06 23:27:31 +0000535 Flags &= ~ELF::EF_MIPS_PIC;
536 MCA.setELFHeaderEFlags(Flags);
537}
Rafael Espindola054234f2014-01-27 03:53:56 +0000538
Matheus Almeidaf79b2812014-03-26 13:40:29 +0000539void MipsTargetELFStreamer::emitDirectiveOptionPic2() {
540 MCAssembler &MCA = getStreamer().getAssembler();
541 unsigned Flags = MCA.getELFHeaderEFlags();
542 Pic = true;
543 // NOTE: We are following the GAS behaviour here which means the directive
544 // 'pic2' also sets the CPIC bit in the ELF header. This is different from
545 // what is stated in the SYSV ABI which consider the bits EF_MIPS_PIC and
546 // EF_MIPS_CPIC to be mutually exclusive.
547 Flags |= ELF::EF_MIPS_PIC | ELF::EF_MIPS_CPIC;
548 MCA.setELFHeaderEFlags(Flags);
549}
550
Rafael Espindola054234f2014-01-27 03:53:56 +0000551void MipsTargetELFStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
Daniel Sandersd97a6342014-08-13 10:07:34 +0000552 unsigned ReturnReg_) {
553 MCContext &Context = getStreamer().getAssembler().getContext();
554 const MCRegisterInfo *RegInfo = Context.getRegisterInfo();
555
556 FrameInfoSet = true;
557 FrameReg = RegInfo->getEncodingValue(StackReg);
558 FrameOffset = StackSize;
559 ReturnReg = RegInfo->getEncodingValue(ReturnReg_);
Rafael Espindola054234f2014-01-27 03:53:56 +0000560}
Rafael Espindola25fa2912014-01-27 04:33:11 +0000561
562void MipsTargetELFStreamer::emitMask(unsigned CPUBitmask,
563 int CPUTopSavedRegOff) {
Daniel Sandersd97a6342014-08-13 10:07:34 +0000564 GPRInfoSet = true;
565 GPRBitMask = CPUBitmask;
566 GPROffset = CPUTopSavedRegOff;
Rafael Espindola25fa2912014-01-27 04:33:11 +0000567}
568
569void MipsTargetELFStreamer::emitFMask(unsigned FPUBitmask,
570 int FPUTopSavedRegOff) {
Daniel Sandersd97a6342014-08-13 10:07:34 +0000571 FPRInfoSet = true;
572 FPRBitMask = FPUBitmask;
573 FPROffset = FPUTopSavedRegOff;
Rafael Espindola25fa2912014-01-27 04:33:11 +0000574}
Vladimir Medic615b26e2014-03-04 09:54:09 +0000575
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000576void MipsTargetELFStreamer::emitDirectiveCpload(unsigned RegNo) {
577 // .cpload $reg
578 // This directive expands to:
579 // lui $gp, %hi(_gp_disp)
580 // addui $gp, $gp, %lo(_gp_disp)
581 // addu $gp, $gp, $reg
582 // when support for position independent code is enabled.
583 if (!Pic || (isN32() || isN64()))
584 return;
585
586 // There's a GNU extension controlled by -mno-shared that allows
587 // locally-binding symbols to be accessed using absolute addresses.
588 // This is currently not supported. When supported -mno-shared makes
589 // .cpload expand to:
590 // lui $gp, %hi(__gnu_local_gp)
591 // addiu $gp, $gp, %lo(__gnu_local_gp)
592
593 StringRef SymName("_gp_disp");
594 MCAssembler &MCA = getStreamer().getAssembler();
595 MCSymbol *GP_Disp = MCA.getContext().GetOrCreateSymbol(SymName);
596 MCA.getOrCreateSymbolData(*GP_Disp);
597
598 MCInst TmpInst;
599 TmpInst.setOpcode(Mips::LUi);
600 TmpInst.addOperand(MCOperand::CreateReg(Mips::GP));
601 const MCSymbolRefExpr *HiSym = MCSymbolRefExpr::Create(
602 "_gp_disp", MCSymbolRefExpr::VK_Mips_ABS_HI, MCA.getContext());
603 TmpInst.addOperand(MCOperand::CreateExpr(HiSym));
604 getStreamer().EmitInstruction(TmpInst, STI);
605
606 TmpInst.clear();
607
608 TmpInst.setOpcode(Mips::ADDiu);
609 TmpInst.addOperand(MCOperand::CreateReg(Mips::GP));
610 TmpInst.addOperand(MCOperand::CreateReg(Mips::GP));
611 const MCSymbolRefExpr *LoSym = MCSymbolRefExpr::Create(
612 "_gp_disp", MCSymbolRefExpr::VK_Mips_ABS_LO, MCA.getContext());
613 TmpInst.addOperand(MCOperand::CreateExpr(LoSym));
614 getStreamer().EmitInstruction(TmpInst, STI);
615
616 TmpInst.clear();
617
618 TmpInst.setOpcode(Mips::ADDu);
619 TmpInst.addOperand(MCOperand::CreateReg(Mips::GP));
620 TmpInst.addOperand(MCOperand::CreateReg(Mips::GP));
621 TmpInst.addOperand(MCOperand::CreateReg(RegNo));
622 getStreamer().EmitInstruction(TmpInst, STI);
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000623
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000624 forbidModuleDirective();
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000625}
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000626
627void MipsTargetELFStreamer::emitDirectiveCpsetup(unsigned RegNo,
628 int RegOrOffset,
629 const MCSymbol &Sym,
630 bool IsReg) {
631 // Only N32 and N64 emit anything for .cpsetup iff PIC is set.
632 if (!Pic || !(isN32() || isN64()))
633 return;
634
635 MCAssembler &MCA = getStreamer().getAssembler();
636 MCInst Inst;
637
638 // Either store the old $gp in a register or on the stack
639 if (IsReg) {
640 // move $save, $gpreg
641 Inst.setOpcode(Mips::DADDu);
642 Inst.addOperand(MCOperand::CreateReg(RegOrOffset));
643 Inst.addOperand(MCOperand::CreateReg(Mips::GP));
644 Inst.addOperand(MCOperand::CreateReg(Mips::ZERO));
645 } else {
646 // sd $gpreg, offset($sp)
647 Inst.setOpcode(Mips::SD);
648 Inst.addOperand(MCOperand::CreateReg(Mips::GP));
649 Inst.addOperand(MCOperand::CreateReg(Mips::SP));
650 Inst.addOperand(MCOperand::CreateImm(RegOrOffset));
651 }
652 getStreamer().EmitInstruction(Inst, STI);
653 Inst.clear();
654
655 const MCSymbolRefExpr *HiExpr = MCSymbolRefExpr::Create(
656 Sym.getName(), MCSymbolRefExpr::VK_Mips_GPOFF_HI, MCA.getContext());
657 const MCSymbolRefExpr *LoExpr = MCSymbolRefExpr::Create(
658 Sym.getName(), MCSymbolRefExpr::VK_Mips_GPOFF_LO, MCA.getContext());
659 // lui $gp, %hi(%neg(%gp_rel(funcSym)))
660 Inst.setOpcode(Mips::LUi);
661 Inst.addOperand(MCOperand::CreateReg(Mips::GP));
662 Inst.addOperand(MCOperand::CreateExpr(HiExpr));
663 getStreamer().EmitInstruction(Inst, STI);
664 Inst.clear();
665
666 // addiu $gp, $gp, %lo(%neg(%gp_rel(funcSym)))
667 Inst.setOpcode(Mips::ADDiu);
668 Inst.addOperand(MCOperand::CreateReg(Mips::GP));
669 Inst.addOperand(MCOperand::CreateReg(Mips::GP));
670 Inst.addOperand(MCOperand::CreateExpr(LoExpr));
671 getStreamer().EmitInstruction(Inst, STI);
672 Inst.clear();
673
674 // daddu $gp, $gp, $funcreg
675 Inst.setOpcode(Mips::DADDu);
676 Inst.addOperand(MCOperand::CreateReg(Mips::GP));
677 Inst.addOperand(MCOperand::CreateReg(Mips::GP));
678 Inst.addOperand(MCOperand::CreateReg(RegNo));
679 getStreamer().EmitInstruction(Inst, STI);
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000680
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000681 forbidModuleDirective();
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000682}
683
684void MipsTargetELFStreamer::emitMipsAbiFlags() {
685 MCAssembler &MCA = getStreamer().getAssembler();
686 MCContext &Context = MCA.getContext();
687 MCStreamer &OS = getStreamer();
688 const MCSectionELF *Sec =
689 Context.getELFSection(".MIPS.abiflags", ELF::SHT_MIPS_ABIFLAGS,
Daniel Sanders41ffa5d2014-07-14 15:05:51 +0000690 ELF::SHF_ALLOC, SectionKind::getMetadata(), 24, "");
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000691 MCSectionData &ABIShndxSD = MCA.getOrCreateSectionData(*Sec);
692 ABIShndxSD.setAlignment(8);
693 OS.SwitchSection(Sec);
694
Daniel Sandersc7dbc632014-07-08 10:11:38 +0000695 OS << ABIFlagsSection;
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000696}
Daniel Sanders7e527422014-07-10 13:38:23 +0000697
698void MipsTargetELFStreamer::emitDirectiveModuleOddSPReg(bool Enabled,
699 bool IsO32ABI) {
700 MipsTargetStreamer::emitDirectiveModuleOddSPReg(Enabled, IsO32ABI);
701
702 ABIFlagsSection.OddSPReg = Enabled;
703}