blob: 4e865663fc0a15aff712f0d5edbd948d818ebb18 [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"
Rafael Espindola972e71a2014-01-31 23:10:26 +000020#include "llvm/MC/MCSectionELF.h"
Rafael Espindolacb1953f2014-01-26 06:57:13 +000021#include "llvm/MC/MCSubtargetInfo.h"
Rafael Espindola95fb9b92015-06-02 20:38:46 +000022#include "llvm/MC/MCSymbolELF.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
Vladimir Medicfb8a2a92014-07-08 08:59:22 +000030MipsTargetStreamer::MipsTargetStreamer(MCStreamer &S)
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000031 : MCTargetStreamer(S), ModuleDirectiveAllowed(true) {
Daniel Sandersd97a6342014-08-13 10:07:34 +000032 GPRInfoSet = FPRInfoSet = FrameInfoSet = false;
33}
Rafael Espindola60890b82014-06-23 19:43:40 +000034void MipsTargetStreamer::emitDirectiveSetMicroMips() {}
35void MipsTargetStreamer::emitDirectiveSetNoMicroMips() {}
36void MipsTargetStreamer::emitDirectiveSetMips16() {}
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000037void MipsTargetStreamer::emitDirectiveSetNoMips16() { forbidModuleDirective(); }
38void MipsTargetStreamer::emitDirectiveSetReorder() { forbidModuleDirective(); }
Rafael Espindola60890b82014-06-23 19:43:40 +000039void MipsTargetStreamer::emitDirectiveSetNoReorder() {}
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000040void MipsTargetStreamer::emitDirectiveSetMacro() { forbidModuleDirective(); }
41void MipsTargetStreamer::emitDirectiveSetNoMacro() { forbidModuleDirective(); }
42void MipsTargetStreamer::emitDirectiveSetMsa() { forbidModuleDirective(); }
43void MipsTargetStreamer::emitDirectiveSetNoMsa() { forbidModuleDirective(); }
44void MipsTargetStreamer::emitDirectiveSetAt() { forbidModuleDirective(); }
Toma Tabacu16a74492015-02-13 10:30:57 +000045void MipsTargetStreamer::emitDirectiveSetAtWithArg(unsigned RegNo) {
46 forbidModuleDirective();
47}
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000048void MipsTargetStreamer::emitDirectiveSetNoAt() { forbidModuleDirective(); }
Rafael Espindola60890b82014-06-23 19:43:40 +000049void MipsTargetStreamer::emitDirectiveEnd(StringRef Name) {}
50void MipsTargetStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {}
51void MipsTargetStreamer::emitDirectiveAbiCalls() {}
52void MipsTargetStreamer::emitDirectiveNaN2008() {}
53void MipsTargetStreamer::emitDirectiveNaNLegacy() {}
54void MipsTargetStreamer::emitDirectiveOptionPic0() {}
55void MipsTargetStreamer::emitDirectiveOptionPic2() {}
Toma Tabacu9ca50962015-04-16 09:53:47 +000056void MipsTargetStreamer::emitDirectiveInsn() { forbidModuleDirective(); }
Rafael Espindola60890b82014-06-23 19:43:40 +000057void MipsTargetStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
58 unsigned ReturnReg) {}
59void MipsTargetStreamer::emitMask(unsigned CPUBitmask, int CPUTopSavedRegOff) {}
60void MipsTargetStreamer::emitFMask(unsigned FPUBitmask, int FPUTopSavedRegOff) {
61}
Toma Tabacu85618b32014-08-19 14:22:52 +000062void MipsTargetStreamer::emitDirectiveSetArch(StringRef Arch) {
63 forbidModuleDirective();
64}
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +000065void MipsTargetStreamer::emitDirectiveSetMips0() { forbidModuleDirective(); }
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000066void MipsTargetStreamer::emitDirectiveSetMips1() { forbidModuleDirective(); }
67void MipsTargetStreamer::emitDirectiveSetMips2() { forbidModuleDirective(); }
68void MipsTargetStreamer::emitDirectiveSetMips3() { forbidModuleDirective(); }
69void MipsTargetStreamer::emitDirectiveSetMips4() { forbidModuleDirective(); }
70void MipsTargetStreamer::emitDirectiveSetMips5() { forbidModuleDirective(); }
71void MipsTargetStreamer::emitDirectiveSetMips32() { forbidModuleDirective(); }
72void MipsTargetStreamer::emitDirectiveSetMips32R2() { forbidModuleDirective(); }
Daniel Sanders17793142015-02-18 16:24:50 +000073void MipsTargetStreamer::emitDirectiveSetMips32R3() { forbidModuleDirective(); }
74void MipsTargetStreamer::emitDirectiveSetMips32R5() { forbidModuleDirective(); }
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000075void MipsTargetStreamer::emitDirectiveSetMips32R6() { forbidModuleDirective(); }
76void MipsTargetStreamer::emitDirectiveSetMips64() { forbidModuleDirective(); }
77void MipsTargetStreamer::emitDirectiveSetMips64R2() { forbidModuleDirective(); }
Daniel Sanders17793142015-02-18 16:24:50 +000078void MipsTargetStreamer::emitDirectiveSetMips64R3() { forbidModuleDirective(); }
79void MipsTargetStreamer::emitDirectiveSetMips64R5() { forbidModuleDirective(); }
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000080void MipsTargetStreamer::emitDirectiveSetMips64R6() { forbidModuleDirective(); }
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +000081void MipsTargetStreamer::emitDirectiveSetPop() { forbidModuleDirective(); }
82void MipsTargetStreamer::emitDirectiveSetPush() { forbidModuleDirective(); }
Toma Tabacu29696502015-06-02 09:48:04 +000083void MipsTargetStreamer::emitDirectiveSetSoftFloat() {
84 forbidModuleDirective();
85}
86void MipsTargetStreamer::emitDirectiveSetHardFloat() {
87 forbidModuleDirective();
88}
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000089void MipsTargetStreamer::emitDirectiveSetDsp() { forbidModuleDirective(); }
Toma Tabacu351b2fe2014-09-17 09:01:54 +000090void MipsTargetStreamer::emitDirectiveSetNoDsp() { forbidModuleDirective(); }
Toma Tabacuc4c202a2014-10-01 14:53:19 +000091void MipsTargetStreamer::emitDirectiveCpLoad(unsigned RegNo) {}
Daniel Sanderse2982ad2015-09-17 16:08:39 +000092void MipsTargetStreamer::emitDirectiveCpRestore(
93 SmallVector<MCInst, 3> &StoreInsts, int Offset) {
94 forbidModuleDirective();
95}
Rafael Espindola60890b82014-06-23 19:43:40 +000096void MipsTargetStreamer::emitDirectiveCpsetup(unsigned RegNo, int RegOrOffset,
97 const MCSymbol &Sym, bool IsReg) {
98}
Toma Tabacubfcbfd52015-06-23 12:34:19 +000099
Toma Tabacua64e5402015-06-25 12:44:38 +0000100void MipsTargetStreamer::emitDirectiveModuleFP() {}
Toma Tabacubfcbfd52015-06-23 12:34:19 +0000101
Toma Tabacu3c499582015-06-25 10:56:57 +0000102void MipsTargetStreamer::emitDirectiveModuleOddSPReg() {
103 if (!ABIFlagsSection.OddSPReg && !ABIFlagsSection.Is32BitABI)
Daniel Sanders7e527422014-07-10 13:38:23 +0000104 report_fatal_error("+nooddspreg is only valid for O32");
105}
Toma Tabacu0f093132015-06-30 13:46:03 +0000106void MipsTargetStreamer::emitDirectiveModuleSoftFloat() {}
107void MipsTargetStreamer::emitDirectiveModuleHardFloat() {}
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +0000108void MipsTargetStreamer::emitDirectiveSetFp(
109 MipsABIFlagsSection::FpABIKind Value) {
110 forbidModuleDirective();
111}
Toma Tabacu32c72aa2015-06-30 09:36:50 +0000112void MipsTargetStreamer::emitDirectiveSetOddSPReg() { forbidModuleDirective(); }
113void MipsTargetStreamer::emitDirectiveSetNoOddSPReg() {
114 forbidModuleDirective();
115}
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000116
117MipsTargetAsmStreamer::MipsTargetAsmStreamer(MCStreamer &S,
118 formatted_raw_ostream &OS)
119 : MipsTargetStreamer(S), OS(OS) {}
Jack Carter6ef6cc52013-11-19 20:53:28 +0000120
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000121void MipsTargetAsmStreamer::emitDirectiveSetMicroMips() {
122 OS << "\t.set\tmicromips\n";
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000123 forbidModuleDirective();
Jack Carter6ef6cc52013-11-19 20:53:28 +0000124}
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000125
126void MipsTargetAsmStreamer::emitDirectiveSetNoMicroMips() {
127 OS << "\t.set\tnomicromips\n";
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000128 forbidModuleDirective();
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000129}
130
Rafael Espindola6633d572014-01-14 18:57:12 +0000131void MipsTargetAsmStreamer::emitDirectiveSetMips16() {
132 OS << "\t.set\tmips16\n";
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000133 forbidModuleDirective();
Rafael Espindola6633d572014-01-14 18:57:12 +0000134}
135
136void MipsTargetAsmStreamer::emitDirectiveSetNoMips16() {
137 OS << "\t.set\tnomips16\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000138 MipsTargetStreamer::emitDirectiveSetNoMips16();
Rafael Espindola6633d572014-01-14 18:57:12 +0000139}
140
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000141void MipsTargetAsmStreamer::emitDirectiveSetReorder() {
142 OS << "\t.set\treorder\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000143 MipsTargetStreamer::emitDirectiveSetReorder();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000144}
145
146void MipsTargetAsmStreamer::emitDirectiveSetNoReorder() {
147 OS << "\t.set\tnoreorder\n";
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000148 forbidModuleDirective();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000149}
150
151void MipsTargetAsmStreamer::emitDirectiveSetMacro() {
152 OS << "\t.set\tmacro\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000153 MipsTargetStreamer::emitDirectiveSetMacro();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000154}
155
156void MipsTargetAsmStreamer::emitDirectiveSetNoMacro() {
157 OS << "\t.set\tnomacro\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000158 MipsTargetStreamer::emitDirectiveSetNoMacro();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000159}
160
Daniel Sanders44934432014-08-07 12:03:36 +0000161void MipsTargetAsmStreamer::emitDirectiveSetMsa() {
162 OS << "\t.set\tmsa\n";
163 MipsTargetStreamer::emitDirectiveSetMsa();
164}
165
166void MipsTargetAsmStreamer::emitDirectiveSetNoMsa() {
167 OS << "\t.set\tnomsa\n";
168 MipsTargetStreamer::emitDirectiveSetNoMsa();
169}
170
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000171void MipsTargetAsmStreamer::emitDirectiveSetAt() {
172 OS << "\t.set\tat\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000173 MipsTargetStreamer::emitDirectiveSetAt();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000174}
175
Toma Tabacu16a74492015-02-13 10:30:57 +0000176void MipsTargetAsmStreamer::emitDirectiveSetAtWithArg(unsigned RegNo) {
177 OS << "\t.set\tat=$" << Twine(RegNo) << "\n";
178 MipsTargetStreamer::emitDirectiveSetAtWithArg(RegNo);
179}
180
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000181void MipsTargetAsmStreamer::emitDirectiveSetNoAt() {
182 OS << "\t.set\tnoat\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000183 MipsTargetStreamer::emitDirectiveSetNoAt();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000184}
185
186void MipsTargetAsmStreamer::emitDirectiveEnd(StringRef Name) {
187 OS << "\t.end\t" << Name << '\n';
188}
189
Rafael Espindola6633d572014-01-14 18:57:12 +0000190void MipsTargetAsmStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {
191 OS << "\t.ent\t" << Symbol.getName() << '\n';
192}
193
Jack Carter0cd3c192014-01-06 23:27:31 +0000194void MipsTargetAsmStreamer::emitDirectiveAbiCalls() { OS << "\t.abicalls\n"; }
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000195
196void MipsTargetAsmStreamer::emitDirectiveNaN2008() { OS << "\t.nan\t2008\n"; }
197
198void MipsTargetAsmStreamer::emitDirectiveNaNLegacy() {
199 OS << "\t.nan\tlegacy\n";
200}
201
Jack Carter0cd3c192014-01-06 23:27:31 +0000202void MipsTargetAsmStreamer::emitDirectiveOptionPic0() {
203 OS << "\t.option\tpic0\n";
204}
205
Matheus Almeidaf79b2812014-03-26 13:40:29 +0000206void MipsTargetAsmStreamer::emitDirectiveOptionPic2() {
207 OS << "\t.option\tpic2\n";
208}
209
Toma Tabacu9ca50962015-04-16 09:53:47 +0000210void MipsTargetAsmStreamer::emitDirectiveInsn() {
211 MipsTargetStreamer::emitDirectiveInsn();
212 OS << "\t.insn\n";
213}
214
Rafael Espindola054234f2014-01-27 03:53:56 +0000215void MipsTargetAsmStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
216 unsigned ReturnReg) {
217 OS << "\t.frame\t$"
218 << StringRef(MipsInstPrinter::getRegisterName(StackReg)).lower() << ","
219 << StackSize << ",$"
Rafael Espindola25fa2912014-01-27 04:33:11 +0000220 << StringRef(MipsInstPrinter::getRegisterName(ReturnReg)).lower() << '\n';
221}
222
Toma Tabacu85618b32014-08-19 14:22:52 +0000223void MipsTargetAsmStreamer::emitDirectiveSetArch(StringRef Arch) {
224 OS << "\t.set arch=" << Arch << "\n";
225 MipsTargetStreamer::emitDirectiveSetArch(Arch);
226}
227
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +0000228void MipsTargetAsmStreamer::emitDirectiveSetMips0() {
229 OS << "\t.set\tmips0\n";
230 MipsTargetStreamer::emitDirectiveSetMips0();
231}
Toma Tabacu26647792014-09-09 12:52:14 +0000232
Daniel Sandersf0df2212014-08-04 12:20:00 +0000233void MipsTargetAsmStreamer::emitDirectiveSetMips1() {
234 OS << "\t.set\tmips1\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000235 MipsTargetStreamer::emitDirectiveSetMips1();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000236}
237
238void MipsTargetAsmStreamer::emitDirectiveSetMips2() {
239 OS << "\t.set\tmips2\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000240 MipsTargetStreamer::emitDirectiveSetMips2();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000241}
242
243void MipsTargetAsmStreamer::emitDirectiveSetMips3() {
244 OS << "\t.set\tmips3\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000245 MipsTargetStreamer::emitDirectiveSetMips3();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000246}
247
248void MipsTargetAsmStreamer::emitDirectiveSetMips4() {
249 OS << "\t.set\tmips4\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000250 MipsTargetStreamer::emitDirectiveSetMips4();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000251}
252
253void MipsTargetAsmStreamer::emitDirectiveSetMips5() {
254 OS << "\t.set\tmips5\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000255 MipsTargetStreamer::emitDirectiveSetMips5();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000256}
257
258void MipsTargetAsmStreamer::emitDirectiveSetMips32() {
259 OS << "\t.set\tmips32\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000260 MipsTargetStreamer::emitDirectiveSetMips32();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000261}
262
Vladimir Medic615b26e2014-03-04 09:54:09 +0000263void MipsTargetAsmStreamer::emitDirectiveSetMips32R2() {
264 OS << "\t.set\tmips32r2\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000265 MipsTargetStreamer::emitDirectiveSetMips32R2();
Vladimir Medic615b26e2014-03-04 09:54:09 +0000266}
267
Daniel Sanders17793142015-02-18 16:24:50 +0000268void MipsTargetAsmStreamer::emitDirectiveSetMips32R3() {
269 OS << "\t.set\tmips32r3\n";
270 MipsTargetStreamer::emitDirectiveSetMips32R3();
271}
272
273void MipsTargetAsmStreamer::emitDirectiveSetMips32R5() {
274 OS << "\t.set\tmips32r5\n";
275 MipsTargetStreamer::emitDirectiveSetMips32R5();
276}
277
Daniel Sandersf0df2212014-08-04 12:20:00 +0000278void MipsTargetAsmStreamer::emitDirectiveSetMips32R6() {
279 OS << "\t.set\tmips32r6\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000280 MipsTargetStreamer::emitDirectiveSetMips32R6();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000281}
282
Matheus Almeida3b9c63d2014-03-26 15:14:32 +0000283void MipsTargetAsmStreamer::emitDirectiveSetMips64() {
284 OS << "\t.set\tmips64\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000285 MipsTargetStreamer::emitDirectiveSetMips64();
Matheus Almeida3b9c63d2014-03-26 15:14:32 +0000286}
287
Matheus Almeidaa2cd0092014-03-26 14:52:22 +0000288void MipsTargetAsmStreamer::emitDirectiveSetMips64R2() {
289 OS << "\t.set\tmips64r2\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000290 MipsTargetStreamer::emitDirectiveSetMips64R2();
Matheus Almeidaa2cd0092014-03-26 14:52:22 +0000291}
292
Daniel Sanders17793142015-02-18 16:24:50 +0000293void MipsTargetAsmStreamer::emitDirectiveSetMips64R3() {
294 OS << "\t.set\tmips64r3\n";
295 MipsTargetStreamer::emitDirectiveSetMips64R3();
296}
297
298void MipsTargetAsmStreamer::emitDirectiveSetMips64R5() {
299 OS << "\t.set\tmips64r5\n";
300 MipsTargetStreamer::emitDirectiveSetMips64R5();
301}
302
Daniel Sandersf0df2212014-08-04 12:20:00 +0000303void MipsTargetAsmStreamer::emitDirectiveSetMips64R6() {
304 OS << "\t.set\tmips64r6\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000305 MipsTargetStreamer::emitDirectiveSetMips64R6();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000306}
307
Vladimir Medic27c398e2014-03-05 11:05:09 +0000308void MipsTargetAsmStreamer::emitDirectiveSetDsp() {
309 OS << "\t.set\tdsp\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000310 MipsTargetStreamer::emitDirectiveSetDsp();
Vladimir Medic27c398e2014-03-05 11:05:09 +0000311}
Toma Tabacu9db22db2014-09-09 10:15:38 +0000312
Toma Tabacu351b2fe2014-09-17 09:01:54 +0000313void MipsTargetAsmStreamer::emitDirectiveSetNoDsp() {
314 OS << "\t.set\tnodsp\n";
315 MipsTargetStreamer::emitDirectiveSetNoDsp();
316}
317
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +0000318void MipsTargetAsmStreamer::emitDirectiveSetPop() {
319 OS << "\t.set\tpop\n";
320 MipsTargetStreamer::emitDirectiveSetPop();
321}
Toma Tabacu9db22db2014-09-09 10:15:38 +0000322
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +0000323void MipsTargetAsmStreamer::emitDirectiveSetPush() {
324 OS << "\t.set\tpush\n";
325 MipsTargetStreamer::emitDirectiveSetPush();
326}
Toma Tabacu9db22db2014-09-09 10:15:38 +0000327
Toma Tabacu29696502015-06-02 09:48:04 +0000328void MipsTargetAsmStreamer::emitDirectiveSetSoftFloat() {
329 OS << "\t.set\tsoftfloat\n";
330 MipsTargetStreamer::emitDirectiveSetSoftFloat();
331}
332
333void MipsTargetAsmStreamer::emitDirectiveSetHardFloat() {
334 OS << "\t.set\thardfloat\n";
335 MipsTargetStreamer::emitDirectiveSetHardFloat();
336}
337
Rafael Espindola25fa2912014-01-27 04:33:11 +0000338// Print a 32 bit hex number with all numbers.
339static void printHex32(unsigned Value, raw_ostream &OS) {
340 OS << "0x";
341 for (int i = 7; i >= 0; i--)
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000342 OS.write_hex((Value & (0xF << (i * 4))) >> (i * 4));
Rafael Espindola25fa2912014-01-27 04:33:11 +0000343}
344
345void MipsTargetAsmStreamer::emitMask(unsigned CPUBitmask,
346 int CPUTopSavedRegOff) {
347 OS << "\t.mask \t";
348 printHex32(CPUBitmask, OS);
349 OS << ',' << CPUTopSavedRegOff << '\n';
350}
351
352void MipsTargetAsmStreamer::emitFMask(unsigned FPUBitmask,
353 int FPUTopSavedRegOff) {
354 OS << "\t.fmask\t";
355 printHex32(FPUBitmask, OS);
356 OS << "," << FPUTopSavedRegOff << '\n';
Rafael Espindola054234f2014-01-27 03:53:56 +0000357}
358
Toma Tabacuc4c202a2014-10-01 14:53:19 +0000359void MipsTargetAsmStreamer::emitDirectiveCpLoad(unsigned RegNo) {
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000360 OS << "\t.cpload\t$"
361 << StringRef(MipsInstPrinter::getRegisterName(RegNo)).lower() << "\n";
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000362 forbidModuleDirective();
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000363}
364
Daniel Sanderse2982ad2015-09-17 16:08:39 +0000365void MipsTargetAsmStreamer::emitDirectiveCpRestore(
366 SmallVector<MCInst, 3> &StoreInsts, int Offset) {
367 MipsTargetStreamer::emitDirectiveCpRestore(StoreInsts, Offset);
368 OS << "\t.cprestore\t" << Offset << "\n";
369}
370
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000371void MipsTargetAsmStreamer::emitDirectiveCpsetup(unsigned RegNo,
372 int RegOrOffset,
373 const MCSymbol &Sym,
374 bool IsReg) {
375 OS << "\t.cpsetup\t$"
376 << StringRef(MipsInstPrinter::getRegisterName(RegNo)).lower() << ", ";
377
378 if (IsReg)
379 OS << "$"
380 << StringRef(MipsInstPrinter::getRegisterName(RegOrOffset)).lower();
381 else
382 OS << RegOrOffset;
383
384 OS << ", ";
385
Daniel Sanders5d796282015-09-21 09:26:55 +0000386 OS << Sym.getName();
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000387 forbidModuleDirective();
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000388}
389
Toma Tabacua64e5402015-06-25 12:44:38 +0000390void MipsTargetAsmStreamer::emitDirectiveModuleFP() {
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000391 OS << "\t.module\tfp=";
Toma Tabacua64e5402015-06-25 12:44:38 +0000392 OS << ABIFlagsSection.getFpABIString(ABIFlagsSection.getFpABI()) << "\n";
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000393}
394
Daniel Sanders7e527422014-07-10 13:38:23 +0000395void MipsTargetAsmStreamer::emitDirectiveSetFp(
396 MipsABIFlagsSection::FpABIKind Value) {
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +0000397 MipsTargetStreamer::emitDirectiveSetFp(Value);
398
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000399 OS << "\t.set\tfp=";
Daniel Sanders7e527422014-07-10 13:38:23 +0000400 OS << ABIFlagsSection.getFpABIString(Value) << "\n";
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000401}
402
Toma Tabacu3c499582015-06-25 10:56:57 +0000403void MipsTargetAsmStreamer::emitDirectiveModuleOddSPReg() {
404 MipsTargetStreamer::emitDirectiveModuleOddSPReg();
Daniel Sanders7e527422014-07-10 13:38:23 +0000405
Toma Tabacu3c499582015-06-25 10:56:57 +0000406 OS << "\t.module\t" << (ABIFlagsSection.OddSPReg ? "" : "no") << "oddspreg\n";
Daniel Sanders7e527422014-07-10 13:38:23 +0000407}
408
Toma Tabacu32c72aa2015-06-30 09:36:50 +0000409void MipsTargetAsmStreamer::emitDirectiveSetOddSPReg() {
410 MipsTargetStreamer::emitDirectiveSetOddSPReg();
411 OS << "\t.set\toddspreg\n";
412}
413
414void MipsTargetAsmStreamer::emitDirectiveSetNoOddSPReg() {
415 MipsTargetStreamer::emitDirectiveSetNoOddSPReg();
416 OS << "\t.set\tnooddspreg\n";
417}
418
Toma Tabacu0f093132015-06-30 13:46:03 +0000419void MipsTargetAsmStreamer::emitDirectiveModuleSoftFloat() {
420 OS << "\t.module\tsoftfloat\n";
421}
422
423void MipsTargetAsmStreamer::emitDirectiveModuleHardFloat() {
424 OS << "\t.module\thardfloat\n";
425}
426
Jack Carter0cd3c192014-01-06 23:27:31 +0000427// This part is for ELF object output.
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000428MipsTargetELFStreamer::MipsTargetELFStreamer(MCStreamer &S,
429 const MCSubtargetInfo &STI)
Rafael Espindola972e71a2014-01-31 23:10:26 +0000430 : MipsTargetStreamer(S), MicroMipsEnabled(false), STI(STI) {
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000431 MCAssembler &MCA = getStreamer().getAssembler();
Simon Atanasyanc99ce682015-03-24 12:24:56 +0000432 Pic = MCA.getContext().getObjectFileInfo()->getRelocM() == Reloc::PIC_;
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000433
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000434 const FeatureBitset &Features = STI.getFeatureBits();
Eric Christophera5762812015-01-26 17:33:46 +0000435
436 // Set the header flags that we can in the constructor.
437 // FIXME: This is a fairly terrible hack. We set the rest
438 // of these in the destructor. The problem here is two-fold:
439 //
440 // a: Some of the eflags can be set/reset by directives.
441 // b: There aren't any usage paths that initialize the ABI
442 // pointer until after we initialize either an assembler
443 // or the target machine.
444 // We can fix this by making the target streamer construct
445 // the ABI, but this is fraught with wide ranging dependency
446 // issues as well.
447 unsigned EFlags = MCA.getELFHeaderEFlags();
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000448
449 // Architecture
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000450 if (Features[Mips::FeatureMips64r6])
Daniel Sanders950f48d2014-07-04 15:21:53 +0000451 EFlags |= ELF::EF_MIPS_ARCH_64R6;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000452 else if (Features[Mips::FeatureMips64r2] ||
453 Features[Mips::FeatureMips64r3] ||
454 Features[Mips::FeatureMips64r5])
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000455 EFlags |= ELF::EF_MIPS_ARCH_64R2;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000456 else if (Features[Mips::FeatureMips64])
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000457 EFlags |= ELF::EF_MIPS_ARCH_64;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000458 else if (Features[Mips::FeatureMips5])
Daniel Sanders950f48d2014-07-04 15:21:53 +0000459 EFlags |= ELF::EF_MIPS_ARCH_5;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000460 else if (Features[Mips::FeatureMips4])
Daniel Sandersf7b32292014-04-03 12:13:36 +0000461 EFlags |= ELF::EF_MIPS_ARCH_4;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000462 else if (Features[Mips::FeatureMips3])
Daniel Sanders950f48d2014-07-04 15:21:53 +0000463 EFlags |= ELF::EF_MIPS_ARCH_3;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000464 else if (Features[Mips::FeatureMips32r6])
Daniel Sanders950f48d2014-07-04 15:21:53 +0000465 EFlags |= ELF::EF_MIPS_ARCH_32R6;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000466 else if (Features[Mips::FeatureMips32r2] ||
467 Features[Mips::FeatureMips32r3] ||
468 Features[Mips::FeatureMips32r5])
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000469 EFlags |= ELF::EF_MIPS_ARCH_32R2;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000470 else if (Features[Mips::FeatureMips32])
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000471 EFlags |= ELF::EF_MIPS_ARCH_32;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000472 else if (Features[Mips::FeatureMips2])
Daniel Sanders950f48d2014-07-04 15:21:53 +0000473 EFlags |= ELF::EF_MIPS_ARCH_2;
474 else
475 EFlags |= ELF::EF_MIPS_ARCH_1;
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000476
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000477 // Other options.
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000478 if (Features[Mips::FeatureNaN2008])
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000479 EFlags |= ELF::EF_MIPS_NAN2008;
480
Daniel Sanders16ec6c12014-07-17 09:52:56 +0000481 // -mabicalls and -mplt are not implemented but we should act as if they were
482 // given.
483 EFlags |= ELF::EF_MIPS_CPIC;
Daniel Sanders16ec6c12014-07-17 09:52:56 +0000484
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000485 MCA.setELFHeaderEFlags(EFlags);
486}
Jack Carter86ac5c12013-11-18 23:55:27 +0000487
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000488void MipsTargetELFStreamer::emitLabel(MCSymbol *S) {
489 auto *Symbol = cast<MCSymbolELF>(S);
Rafael Espindola26e917c2014-01-15 03:07:12 +0000490 if (!isMicroMipsEnabled())
491 return;
Rafael Espindolac73aed12015-06-03 19:03:11 +0000492 getStreamer().getAssembler().registerSymbol(*Symbol);
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000493 uint8_t Type = Symbol->getType();
Rafael Espindola26e917c2014-01-15 03:07:12 +0000494 if (Type != ELF::STT_FUNC)
495 return;
496
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000497 Symbol->setOther(ELF::STO_MIPS_MICROMIPS);
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000498}
499
Rafael Espindola972e71a2014-01-31 23:10:26 +0000500void MipsTargetELFStreamer::finish() {
501 MCAssembler &MCA = getStreamer().getAssembler();
Daniel Sanders68c37472014-07-21 13:30:55 +0000502 const MCObjectFileInfo &OFI = *MCA.getContext().getObjectFileInfo();
Rafael Espindola972e71a2014-01-31 23:10:26 +0000503
Daniel Sanders41ffa5d2014-07-14 15:05:51 +0000504 // .bss, .text and .data are always at least 16-byte aligned.
Rafael Espindola967d6a62015-05-21 21:02:35 +0000505 MCSection &TextSection = *OFI.getTextSection();
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000506 MCA.registerSection(TextSection);
Rafael Espindola967d6a62015-05-21 21:02:35 +0000507 MCSection &DataSection = *OFI.getDataSection();
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000508 MCA.registerSection(DataSection);
Rafael Espindola967d6a62015-05-21 21:02:35 +0000509 MCSection &BSSSection = *OFI.getBSSSection();
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000510 MCA.registerSection(BSSSection);
Daniel Sanders41ffa5d2014-07-14 15:05:51 +0000511
Rafael Espindola967d6a62015-05-21 21:02:35 +0000512 TextSection.setAlignment(std::max(16u, TextSection.getAlignment()));
513 DataSection.setAlignment(std::max(16u, DataSection.getAlignment()));
514 BSSSection.setAlignment(std::max(16u, BSSSection.getAlignment()));
Daniel Sanders41ffa5d2014-07-14 15:05:51 +0000515
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000516 const FeatureBitset &Features = STI.getFeatureBits();
Eric Christophera5762812015-01-26 17:33:46 +0000517
518 // Update e_header flags. See the FIXME and comment above in
519 // the constructor for a full rundown on this.
520 unsigned EFlags = MCA.getELFHeaderEFlags();
521
522 // ABI
523 // N64 does not require any ABI bits.
524 if (getABI().IsO32())
525 EFlags |= ELF::EF_MIPS_ABI_O32;
526 else if (getABI().IsN32())
527 EFlags |= ELF::EF_MIPS_ABI2;
528
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000529 if (Features[Mips::FeatureGP64Bit]) {
Eric Christophera5762812015-01-26 17:33:46 +0000530 if (getABI().IsO32())
531 EFlags |= ELF::EF_MIPS_32BITMODE; /* Compatibility Mode */
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000532 } else if (Features[Mips::FeatureMips64r2] || Features[Mips::FeatureMips64])
Eric Christophera5762812015-01-26 17:33:46 +0000533 EFlags |= ELF::EF_MIPS_32BITMODE;
534
535 // If we've set the cpic eflag and we're n64, go ahead and set the pic
536 // one as well.
537 if (EFlags & ELF::EF_MIPS_CPIC && getABI().IsN64())
538 EFlags |= ELF::EF_MIPS_PIC;
539
540 MCA.setELFHeaderEFlags(EFlags);
541
Daniel Sanders68c37472014-07-21 13:30:55 +0000542 // Emit all the option records.
543 // At the moment we are only emitting .Mips.options (ODK_REGINFO) and
544 // .reginfo.
545 MipsELFStreamer &MEF = static_cast<MipsELFStreamer &>(Streamer);
546 MEF.EmitMipsOptionRecords();
Rafael Espindola972e71a2014-01-31 23:10:26 +0000547
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000548 emitMipsAbiFlags();
Rafael Espindola972e71a2014-01-31 23:10:26 +0000549}
550
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000551void MipsTargetELFStreamer::emitAssignment(MCSymbol *S, const MCExpr *Value) {
552 auto *Symbol = cast<MCSymbolELF>(S);
Zoran Jovanovic28221d82014-03-20 09:44:49 +0000553 // If on rhs is micromips symbol then mark Symbol as microMips.
554 if (Value->getKind() != MCExpr::SymbolRef)
555 return;
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000556 const auto &RhsSym = cast<MCSymbolELF>(
557 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol());
Toma Tabacu2cc44f52015-04-16 13:37:32 +0000558
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000559 if (!(RhsSym.getOther() & ELF::STO_MIPS_MICROMIPS))
Zoran Jovanovic28221d82014-03-20 09:44:49 +0000560 return;
561
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000562 Symbol->setOther(ELF::STO_MIPS_MICROMIPS);
Zoran Jovanovic28221d82014-03-20 09:44:49 +0000563}
564
Jack Carter86ac5c12013-11-18 23:55:27 +0000565MCELFStreamer &MipsTargetELFStreamer::getStreamer() {
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000566 return static_cast<MCELFStreamer &>(Streamer);
Jack Carter86ac5c12013-11-18 23:55:27 +0000567}
568
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000569void MipsTargetELFStreamer::emitDirectiveSetMicroMips() {
570 MicroMipsEnabled = true;
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000571
572 MCAssembler &MCA = getStreamer().getAssembler();
573 unsigned Flags = MCA.getELFHeaderEFlags();
574 Flags |= ELF::EF_MIPS_MICROMIPS;
575 MCA.setELFHeaderEFlags(Flags);
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000576 forbidModuleDirective();
Jack Carter86ac5c12013-11-18 23:55:27 +0000577}
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000578
579void MipsTargetELFStreamer::emitDirectiveSetNoMicroMips() {
580 MicroMipsEnabled = false;
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000581 forbidModuleDirective();
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000582}
583
Rafael Espindola6633d572014-01-14 18:57:12 +0000584void MipsTargetELFStreamer::emitDirectiveSetMips16() {
Rafael Espindolae7583752014-01-24 16:13:20 +0000585 MCAssembler &MCA = getStreamer().getAssembler();
586 unsigned Flags = MCA.getELFHeaderEFlags();
587 Flags |= ELF::EF_MIPS_ARCH_ASE_M16;
588 MCA.setELFHeaderEFlags(Flags);
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000589 forbidModuleDirective();
Rafael Espindola6633d572014-01-14 18:57:12 +0000590}
591
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000592void MipsTargetELFStreamer::emitDirectiveSetNoReorder() {
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000593 MCAssembler &MCA = getStreamer().getAssembler();
594 unsigned Flags = MCA.getELFHeaderEFlags();
595 Flags |= ELF::EF_MIPS_NOREORDER;
596 MCA.setELFHeaderEFlags(Flags);
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000597 forbidModuleDirective();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000598}
599
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000600void MipsTargetELFStreamer::emitDirectiveEnd(StringRef Name) {
Daniel Sandersd97a6342014-08-13 10:07:34 +0000601 MCAssembler &MCA = getStreamer().getAssembler();
602 MCContext &Context = MCA.getContext();
603 MCStreamer &OS = getStreamer();
604
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000605 MCSectionELF *Sec = Context.getELFSection(".pdr", ELF::SHT_PROGBITS,
606 ELF::SHF_ALLOC | ELF::SHT_REL);
Daniel Sandersd97a6342014-08-13 10:07:34 +0000607
608 const MCSymbolRefExpr *ExprRef =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000609 MCSymbolRefExpr::create(Name, MCSymbolRefExpr::VK_None, Context);
Daniel Sandersd97a6342014-08-13 10:07:34 +0000610
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000611 MCA.registerSection(*Sec);
Rafael Espindola967d6a62015-05-21 21:02:35 +0000612 Sec->setAlignment(4);
Daniel Sandersd97a6342014-08-13 10:07:34 +0000613
614 OS.PushSection();
615
616 OS.SwitchSection(Sec);
617
618 OS.EmitValueImpl(ExprRef, 4);
619
620 OS.EmitIntValue(GPRInfoSet ? GPRBitMask : 0, 4); // reg_mask
621 OS.EmitIntValue(GPRInfoSet ? GPROffset : 0, 4); // reg_offset
622
623 OS.EmitIntValue(FPRInfoSet ? FPRBitMask : 0, 4); // fpreg_mask
624 OS.EmitIntValue(FPRInfoSet ? FPROffset : 0, 4); // fpreg_offset
625
626 OS.EmitIntValue(FrameInfoSet ? FrameOffset : 0, 4); // frame_offset
627 OS.EmitIntValue(FrameInfoSet ? FrameReg : 0, 4); // frame_reg
628 OS.EmitIntValue(FrameInfoSet ? ReturnReg : 0, 4); // return_reg
629
630 // The .end directive marks the end of a procedure. Invalidate
631 // the information gathered up until this point.
632 GPRInfoSet = FPRInfoSet = FrameInfoSet = false;
633
634 OS.PopSection();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000635}
636
Rafael Espindola6633d572014-01-14 18:57:12 +0000637void MipsTargetELFStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {
Daniel Sandersd97a6342014-08-13 10:07:34 +0000638 GPRInfoSet = FPRInfoSet = FrameInfoSet = false;
Rafael Espindola6633d572014-01-14 18:57:12 +0000639}
640
Jack Carter0cd3c192014-01-06 23:27:31 +0000641void MipsTargetELFStreamer::emitDirectiveAbiCalls() {
642 MCAssembler &MCA = getStreamer().getAssembler();
643 unsigned Flags = MCA.getELFHeaderEFlags();
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000644 Flags |= ELF::EF_MIPS_CPIC | ELF::EF_MIPS_PIC;
Jack Carter0cd3c192014-01-06 23:27:31 +0000645 MCA.setELFHeaderEFlags(Flags);
646}
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000647
648void MipsTargetELFStreamer::emitDirectiveNaN2008() {
649 MCAssembler &MCA = getStreamer().getAssembler();
650 unsigned Flags = MCA.getELFHeaderEFlags();
651 Flags |= ELF::EF_MIPS_NAN2008;
652 MCA.setELFHeaderEFlags(Flags);
653}
654
655void MipsTargetELFStreamer::emitDirectiveNaNLegacy() {
656 MCAssembler &MCA = getStreamer().getAssembler();
657 unsigned Flags = MCA.getELFHeaderEFlags();
658 Flags &= ~ELF::EF_MIPS_NAN2008;
659 MCA.setELFHeaderEFlags(Flags);
660}
661
Jack Carter0cd3c192014-01-06 23:27:31 +0000662void MipsTargetELFStreamer::emitDirectiveOptionPic0() {
663 MCAssembler &MCA = getStreamer().getAssembler();
664 unsigned Flags = MCA.getELFHeaderEFlags();
Matheus Almeidaf79b2812014-03-26 13:40:29 +0000665 // This option overrides other PIC options like -KPIC.
666 Pic = false;
Jack Carter0cd3c192014-01-06 23:27:31 +0000667 Flags &= ~ELF::EF_MIPS_PIC;
668 MCA.setELFHeaderEFlags(Flags);
669}
Rafael Espindola054234f2014-01-27 03:53:56 +0000670
Matheus Almeidaf79b2812014-03-26 13:40:29 +0000671void MipsTargetELFStreamer::emitDirectiveOptionPic2() {
672 MCAssembler &MCA = getStreamer().getAssembler();
673 unsigned Flags = MCA.getELFHeaderEFlags();
674 Pic = true;
675 // NOTE: We are following the GAS behaviour here which means the directive
676 // 'pic2' also sets the CPIC bit in the ELF header. This is different from
677 // what is stated in the SYSV ABI which consider the bits EF_MIPS_PIC and
678 // EF_MIPS_CPIC to be mutually exclusive.
679 Flags |= ELF::EF_MIPS_PIC | ELF::EF_MIPS_CPIC;
680 MCA.setELFHeaderEFlags(Flags);
681}
682
Toma Tabacu9ca50962015-04-16 09:53:47 +0000683void MipsTargetELFStreamer::emitDirectiveInsn() {
684 MipsTargetStreamer::emitDirectiveInsn();
685 MipsELFStreamer &MEF = static_cast<MipsELFStreamer &>(Streamer);
686 MEF.createPendingLabelRelocs();
687}
688
Rafael Espindola054234f2014-01-27 03:53:56 +0000689void MipsTargetELFStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
Daniel Sandersd97a6342014-08-13 10:07:34 +0000690 unsigned ReturnReg_) {
691 MCContext &Context = getStreamer().getAssembler().getContext();
692 const MCRegisterInfo *RegInfo = Context.getRegisterInfo();
693
694 FrameInfoSet = true;
695 FrameReg = RegInfo->getEncodingValue(StackReg);
696 FrameOffset = StackSize;
697 ReturnReg = RegInfo->getEncodingValue(ReturnReg_);
Rafael Espindola054234f2014-01-27 03:53:56 +0000698}
Rafael Espindola25fa2912014-01-27 04:33:11 +0000699
700void MipsTargetELFStreamer::emitMask(unsigned CPUBitmask,
701 int CPUTopSavedRegOff) {
Daniel Sandersd97a6342014-08-13 10:07:34 +0000702 GPRInfoSet = true;
703 GPRBitMask = CPUBitmask;
704 GPROffset = CPUTopSavedRegOff;
Rafael Espindola25fa2912014-01-27 04:33:11 +0000705}
706
707void MipsTargetELFStreamer::emitFMask(unsigned FPUBitmask,
708 int FPUTopSavedRegOff) {
Daniel Sandersd97a6342014-08-13 10:07:34 +0000709 FPRInfoSet = true;
710 FPRBitMask = FPUBitmask;
711 FPROffset = FPUTopSavedRegOff;
Rafael Espindola25fa2912014-01-27 04:33:11 +0000712}
Vladimir Medic615b26e2014-03-04 09:54:09 +0000713
Toma Tabacuc4c202a2014-10-01 14:53:19 +0000714void MipsTargetELFStreamer::emitDirectiveCpLoad(unsigned RegNo) {
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000715 // .cpload $reg
716 // This directive expands to:
717 // lui $gp, %hi(_gp_disp)
718 // addui $gp, $gp, %lo(_gp_disp)
719 // addu $gp, $gp, $reg
720 // when support for position independent code is enabled.
Eric Christophera5762812015-01-26 17:33:46 +0000721 if (!Pic || (getABI().IsN32() || getABI().IsN64()))
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000722 return;
723
724 // There's a GNU extension controlled by -mno-shared that allows
725 // locally-binding symbols to be accessed using absolute addresses.
726 // This is currently not supported. When supported -mno-shared makes
727 // .cpload expand to:
728 // lui $gp, %hi(__gnu_local_gp)
729 // addiu $gp, $gp, %lo(__gnu_local_gp)
730
731 StringRef SymName("_gp_disp");
732 MCAssembler &MCA = getStreamer().getAssembler();
Jim Grosbach6f482002015-05-18 18:43:14 +0000733 MCSymbol *GP_Disp = MCA.getContext().getOrCreateSymbol(SymName);
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000734 MCA.registerSymbol(*GP_Disp);
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000735
736 MCInst TmpInst;
737 TmpInst.setOpcode(Mips::LUi);
Jim Grosbache9119e42015-05-13 18:37:00 +0000738 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
Jim Grosbach13760bd2015-05-30 01:25:56 +0000739 const MCSymbolRefExpr *HiSym = MCSymbolRefExpr::create(
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000740 "_gp_disp", MCSymbolRefExpr::VK_Mips_ABS_HI, MCA.getContext());
Jim Grosbache9119e42015-05-13 18:37:00 +0000741 TmpInst.addOperand(MCOperand::createExpr(HiSym));
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000742 getStreamer().EmitInstruction(TmpInst, STI);
743
744 TmpInst.clear();
745
746 TmpInst.setOpcode(Mips::ADDiu);
Jim Grosbache9119e42015-05-13 18:37:00 +0000747 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
748 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
Jim Grosbach13760bd2015-05-30 01:25:56 +0000749 const MCSymbolRefExpr *LoSym = MCSymbolRefExpr::create(
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000750 "_gp_disp", MCSymbolRefExpr::VK_Mips_ABS_LO, MCA.getContext());
Jim Grosbache9119e42015-05-13 18:37:00 +0000751 TmpInst.addOperand(MCOperand::createExpr(LoSym));
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000752 getStreamer().EmitInstruction(TmpInst, STI);
753
754 TmpInst.clear();
755
756 TmpInst.setOpcode(Mips::ADDu);
Jim Grosbache9119e42015-05-13 18:37:00 +0000757 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
758 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
759 TmpInst.addOperand(MCOperand::createReg(RegNo));
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000760 getStreamer().EmitInstruction(TmpInst, STI);
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000761
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000762 forbidModuleDirective();
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000763}
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000764
Daniel Sanderse2982ad2015-09-17 16:08:39 +0000765void MipsTargetELFStreamer::emitDirectiveCpRestore(
766 SmallVector<MCInst, 3> &StoreInsts, int Offset) {
767 MipsTargetStreamer::emitDirectiveCpRestore(StoreInsts, Offset);
768 // .cprestore offset
769 // When PIC mode is enabled and the O32 ABI is used, this directive expands
770 // to:
771 // sw $gp, offset($sp)
772 // and adds a corresponding LW after every JAL.
773
774 // Note that .cprestore is ignored if used with the N32 and N64 ABIs or if it
775 // is used in non-PIC mode.
776 if (!Pic || (getABI().IsN32() || getABI().IsN64()))
777 return;
778
779 for (const MCInst &Inst : StoreInsts)
780 getStreamer().EmitInstruction(Inst, STI);
781}
782
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000783void MipsTargetELFStreamer::emitDirectiveCpsetup(unsigned RegNo,
784 int RegOrOffset,
785 const MCSymbol &Sym,
786 bool IsReg) {
787 // Only N32 and N64 emit anything for .cpsetup iff PIC is set.
Eric Christophera5762812015-01-26 17:33:46 +0000788 if (!Pic || !(getABI().IsN32() || getABI().IsN64()))
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000789 return;
790
791 MCAssembler &MCA = getStreamer().getAssembler();
792 MCInst Inst;
793
794 // Either store the old $gp in a register or on the stack
795 if (IsReg) {
796 // move $save, $gpreg
Vasileios Kalintiris1c78ca62015-08-11 08:56:25 +0000797 Inst.setOpcode(Mips::OR64);
Jim Grosbache9119e42015-05-13 18:37:00 +0000798 Inst.addOperand(MCOperand::createReg(RegOrOffset));
799 Inst.addOperand(MCOperand::createReg(Mips::GP));
800 Inst.addOperand(MCOperand::createReg(Mips::ZERO));
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000801 } else {
802 // sd $gpreg, offset($sp)
803 Inst.setOpcode(Mips::SD);
Jim Grosbache9119e42015-05-13 18:37:00 +0000804 Inst.addOperand(MCOperand::createReg(Mips::GP));
805 Inst.addOperand(MCOperand::createReg(Mips::SP));
806 Inst.addOperand(MCOperand::createImm(RegOrOffset));
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000807 }
808 getStreamer().EmitInstruction(Inst, STI);
809 Inst.clear();
810
Jim Grosbach13760bd2015-05-30 01:25:56 +0000811 const MCSymbolRefExpr *HiExpr = MCSymbolRefExpr::create(
Toma Tabacu8874eac2015-02-18 13:46:53 +0000812 &Sym, MCSymbolRefExpr::VK_Mips_GPOFF_HI, MCA.getContext());
Jim Grosbach13760bd2015-05-30 01:25:56 +0000813 const MCSymbolRefExpr *LoExpr = MCSymbolRefExpr::create(
Toma Tabacu8874eac2015-02-18 13:46:53 +0000814 &Sym, MCSymbolRefExpr::VK_Mips_GPOFF_LO, MCA.getContext());
815
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000816 // lui $gp, %hi(%neg(%gp_rel(funcSym)))
817 Inst.setOpcode(Mips::LUi);
Jim Grosbache9119e42015-05-13 18:37:00 +0000818 Inst.addOperand(MCOperand::createReg(Mips::GP));
819 Inst.addOperand(MCOperand::createExpr(HiExpr));
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000820 getStreamer().EmitInstruction(Inst, STI);
821 Inst.clear();
822
823 // addiu $gp, $gp, %lo(%neg(%gp_rel(funcSym)))
824 Inst.setOpcode(Mips::ADDiu);
Jim Grosbache9119e42015-05-13 18:37:00 +0000825 Inst.addOperand(MCOperand::createReg(Mips::GP));
826 Inst.addOperand(MCOperand::createReg(Mips::GP));
827 Inst.addOperand(MCOperand::createExpr(LoExpr));
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000828 getStreamer().EmitInstruction(Inst, STI);
829 Inst.clear();
830
831 // daddu $gp, $gp, $funcreg
832 Inst.setOpcode(Mips::DADDu);
Jim Grosbache9119e42015-05-13 18:37:00 +0000833 Inst.addOperand(MCOperand::createReg(Mips::GP));
834 Inst.addOperand(MCOperand::createReg(Mips::GP));
835 Inst.addOperand(MCOperand::createReg(RegNo));
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000836 getStreamer().EmitInstruction(Inst, STI);
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000837
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000838 forbidModuleDirective();
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000839}
840
841void MipsTargetELFStreamer::emitMipsAbiFlags() {
842 MCAssembler &MCA = getStreamer().getAssembler();
843 MCContext &Context = MCA.getContext();
844 MCStreamer &OS = getStreamer();
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000845 MCSectionELF *Sec = Context.getELFSection(
Rafael Espindolaba31e272015-01-29 17:33:21 +0000846 ".MIPS.abiflags", ELF::SHT_MIPS_ABIFLAGS, ELF::SHF_ALLOC, 24, "");
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000847 MCA.registerSection(*Sec);
Rafael Espindola967d6a62015-05-21 21:02:35 +0000848 Sec->setAlignment(8);
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000849 OS.SwitchSection(Sec);
850
Daniel Sandersc7dbc632014-07-08 10:11:38 +0000851 OS << ABIFlagsSection;
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000852}