blob: d4e7e58146adfb143bb17afec8ce3f56147936a9 [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
Mehdi Aminib550cb12016-04-18 09:17:29 +000014#include "MipsTargetStreamer.h"
Rafael Espindola054234f2014-01-27 03:53:56 +000015#include "InstPrinter/MipsInstPrinter.h"
Daniel Sanders68c37472014-07-21 13:30:55 +000016#include "MipsELFStreamer.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000017#include "MipsMCTargetDesc.h"
Rafael Espindola972e71a2014-01-31 23:10:26 +000018#include "MipsTargetObjectFile.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"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000023#include "llvm/Support/ELF.h"
Jack Carter86ac5c12013-11-18 23:55:27 +000024#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/FormattedStream.h"
26
27using namespace llvm;
28
Vladimir Medicfb8a2a92014-07-08 08:59:22 +000029MipsTargetStreamer::MipsTargetStreamer(MCStreamer &S)
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000030 : MCTargetStreamer(S), ModuleDirectiveAllowed(true) {
Daniel Sandersd97a6342014-08-13 10:07:34 +000031 GPRInfoSet = FPRInfoSet = FrameInfoSet = false;
32}
Rafael Espindola60890b82014-06-23 19:43:40 +000033void MipsTargetStreamer::emitDirectiveSetMicroMips() {}
34void MipsTargetStreamer::emitDirectiveSetNoMicroMips() {}
35void MipsTargetStreamer::emitDirectiveSetMips16() {}
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000036void MipsTargetStreamer::emitDirectiveSetNoMips16() { forbidModuleDirective(); }
37void MipsTargetStreamer::emitDirectiveSetReorder() { forbidModuleDirective(); }
Rafael Espindola60890b82014-06-23 19:43:40 +000038void MipsTargetStreamer::emitDirectiveSetNoReorder() {}
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000039void MipsTargetStreamer::emitDirectiveSetMacro() { forbidModuleDirective(); }
40void MipsTargetStreamer::emitDirectiveSetNoMacro() { forbidModuleDirective(); }
41void MipsTargetStreamer::emitDirectiveSetMsa() { forbidModuleDirective(); }
42void MipsTargetStreamer::emitDirectiveSetNoMsa() { forbidModuleDirective(); }
43void MipsTargetStreamer::emitDirectiveSetAt() { forbidModuleDirective(); }
Toma Tabacu16a74492015-02-13 10:30:57 +000044void MipsTargetStreamer::emitDirectiveSetAtWithArg(unsigned RegNo) {
45 forbidModuleDirective();
46}
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000047void MipsTargetStreamer::emitDirectiveSetNoAt() { forbidModuleDirective(); }
Rafael Espindola60890b82014-06-23 19:43:40 +000048void MipsTargetStreamer::emitDirectiveEnd(StringRef Name) {}
49void MipsTargetStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {}
50void MipsTargetStreamer::emitDirectiveAbiCalls() {}
51void MipsTargetStreamer::emitDirectiveNaN2008() {}
52void MipsTargetStreamer::emitDirectiveNaNLegacy() {}
53void MipsTargetStreamer::emitDirectiveOptionPic0() {}
54void MipsTargetStreamer::emitDirectiveOptionPic2() {}
Toma Tabacu9ca50962015-04-16 09:53:47 +000055void MipsTargetStreamer::emitDirectiveInsn() { forbidModuleDirective(); }
Rafael Espindola60890b82014-06-23 19:43:40 +000056void MipsTargetStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
57 unsigned ReturnReg) {}
58void MipsTargetStreamer::emitMask(unsigned CPUBitmask, int CPUTopSavedRegOff) {}
59void MipsTargetStreamer::emitFMask(unsigned FPUBitmask, int FPUTopSavedRegOff) {
60}
Toma Tabacu85618b32014-08-19 14:22:52 +000061void MipsTargetStreamer::emitDirectiveSetArch(StringRef Arch) {
62 forbidModuleDirective();
63}
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +000064void MipsTargetStreamer::emitDirectiveSetMips0() { forbidModuleDirective(); }
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000065void MipsTargetStreamer::emitDirectiveSetMips1() { forbidModuleDirective(); }
66void MipsTargetStreamer::emitDirectiveSetMips2() { forbidModuleDirective(); }
67void MipsTargetStreamer::emitDirectiveSetMips3() { forbidModuleDirective(); }
68void MipsTargetStreamer::emitDirectiveSetMips4() { forbidModuleDirective(); }
69void MipsTargetStreamer::emitDirectiveSetMips5() { forbidModuleDirective(); }
70void MipsTargetStreamer::emitDirectiveSetMips32() { forbidModuleDirective(); }
71void MipsTargetStreamer::emitDirectiveSetMips32R2() { forbidModuleDirective(); }
Daniel Sanders17793142015-02-18 16:24:50 +000072void MipsTargetStreamer::emitDirectiveSetMips32R3() { forbidModuleDirective(); }
73void MipsTargetStreamer::emitDirectiveSetMips32R5() { forbidModuleDirective(); }
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000074void MipsTargetStreamer::emitDirectiveSetMips32R6() { forbidModuleDirective(); }
75void MipsTargetStreamer::emitDirectiveSetMips64() { forbidModuleDirective(); }
76void MipsTargetStreamer::emitDirectiveSetMips64R2() { forbidModuleDirective(); }
Daniel Sanders17793142015-02-18 16:24:50 +000077void MipsTargetStreamer::emitDirectiveSetMips64R3() { forbidModuleDirective(); }
78void MipsTargetStreamer::emitDirectiveSetMips64R5() { forbidModuleDirective(); }
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000079void MipsTargetStreamer::emitDirectiveSetMips64R6() { forbidModuleDirective(); }
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +000080void MipsTargetStreamer::emitDirectiveSetPop() { forbidModuleDirective(); }
81void MipsTargetStreamer::emitDirectiveSetPush() { forbidModuleDirective(); }
Toma Tabacu29696502015-06-02 09:48:04 +000082void MipsTargetStreamer::emitDirectiveSetSoftFloat() {
83 forbidModuleDirective();
84}
85void MipsTargetStreamer::emitDirectiveSetHardFloat() {
86 forbidModuleDirective();
87}
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000088void MipsTargetStreamer::emitDirectiveSetDsp() { forbidModuleDirective(); }
Toma Tabacu351b2fe2014-09-17 09:01:54 +000089void MipsTargetStreamer::emitDirectiveSetNoDsp() { forbidModuleDirective(); }
Toma Tabacuc4c202a2014-10-01 14:53:19 +000090void MipsTargetStreamer::emitDirectiveCpLoad(unsigned RegNo) {}
Daniel Sandersc6924fa2016-04-18 12:06:15 +000091void MipsTargetStreamer::emitDirectiveCpRestore(int Offset) {
Daniel Sanderse2982ad2015-09-17 16:08:39 +000092 forbidModuleDirective();
93}
Rafael Espindola60890b82014-06-23 19:43:40 +000094void MipsTargetStreamer::emitDirectiveCpsetup(unsigned RegNo, int RegOrOffset,
95 const MCSymbol &Sym, bool IsReg) {
96}
Daniel Sandersf173dda2015-09-22 10:50:09 +000097void MipsTargetStreamer::emitDirectiveCpreturn(unsigned SaveLocation,
98 bool SaveLocationIsRegister) {}
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 Sandersc6924fa2016-04-18 12:06:15 +0000365void MipsTargetAsmStreamer::emitDirectiveCpRestore(int Offset) {
366 MipsTargetStreamer::emitDirectiveCpRestore(Offset);
Daniel Sanderse2982ad2015-09-17 16:08:39 +0000367 OS << "\t.cprestore\t" << Offset << "\n";
368}
369
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000370void MipsTargetAsmStreamer::emitDirectiveCpsetup(unsigned RegNo,
371 int RegOrOffset,
372 const MCSymbol &Sym,
373 bool IsReg) {
374 OS << "\t.cpsetup\t$"
375 << StringRef(MipsInstPrinter::getRegisterName(RegNo)).lower() << ", ";
376
377 if (IsReg)
378 OS << "$"
379 << StringRef(MipsInstPrinter::getRegisterName(RegOrOffset)).lower();
380 else
381 OS << RegOrOffset;
382
383 OS << ", ";
384
Daniel Sanders5d796282015-09-21 09:26:55 +0000385 OS << Sym.getName();
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000386 forbidModuleDirective();
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000387}
388
Daniel Sandersf173dda2015-09-22 10:50:09 +0000389void MipsTargetAsmStreamer::emitDirectiveCpreturn(unsigned SaveLocation,
390 bool SaveLocationIsRegister) {
391 OS << "\t.cpreturn";
392 forbidModuleDirective();
393}
394
Toma Tabacua64e5402015-06-25 12:44:38 +0000395void MipsTargetAsmStreamer::emitDirectiveModuleFP() {
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000396 OS << "\t.module\tfp=";
Toma Tabacua64e5402015-06-25 12:44:38 +0000397 OS << ABIFlagsSection.getFpABIString(ABIFlagsSection.getFpABI()) << "\n";
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000398}
399
Daniel Sanders7e527422014-07-10 13:38:23 +0000400void MipsTargetAsmStreamer::emitDirectiveSetFp(
401 MipsABIFlagsSection::FpABIKind Value) {
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +0000402 MipsTargetStreamer::emitDirectiveSetFp(Value);
403
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000404 OS << "\t.set\tfp=";
Daniel Sanders7e527422014-07-10 13:38:23 +0000405 OS << ABIFlagsSection.getFpABIString(Value) << "\n";
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000406}
407
Toma Tabacu3c499582015-06-25 10:56:57 +0000408void MipsTargetAsmStreamer::emitDirectiveModuleOddSPReg() {
409 MipsTargetStreamer::emitDirectiveModuleOddSPReg();
Daniel Sanders7e527422014-07-10 13:38:23 +0000410
Toma Tabacu3c499582015-06-25 10:56:57 +0000411 OS << "\t.module\t" << (ABIFlagsSection.OddSPReg ? "" : "no") << "oddspreg\n";
Daniel Sanders7e527422014-07-10 13:38:23 +0000412}
413
Toma Tabacu32c72aa2015-06-30 09:36:50 +0000414void MipsTargetAsmStreamer::emitDirectiveSetOddSPReg() {
415 MipsTargetStreamer::emitDirectiveSetOddSPReg();
416 OS << "\t.set\toddspreg\n";
417}
418
419void MipsTargetAsmStreamer::emitDirectiveSetNoOddSPReg() {
420 MipsTargetStreamer::emitDirectiveSetNoOddSPReg();
421 OS << "\t.set\tnooddspreg\n";
422}
423
Toma Tabacu0f093132015-06-30 13:46:03 +0000424void MipsTargetAsmStreamer::emitDirectiveModuleSoftFloat() {
425 OS << "\t.module\tsoftfloat\n";
426}
427
428void MipsTargetAsmStreamer::emitDirectiveModuleHardFloat() {
429 OS << "\t.module\thardfloat\n";
430}
431
Jack Carter0cd3c192014-01-06 23:27:31 +0000432// This part is for ELF object output.
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000433MipsTargetELFStreamer::MipsTargetELFStreamer(MCStreamer &S,
434 const MCSubtargetInfo &STI)
Rafael Espindola972e71a2014-01-31 23:10:26 +0000435 : MipsTargetStreamer(S), MicroMipsEnabled(false), STI(STI) {
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000436 MCAssembler &MCA = getStreamer().getAssembler();
Simon Atanasyanc99ce682015-03-24 12:24:56 +0000437 Pic = MCA.getContext().getObjectFileInfo()->getRelocM() == Reloc::PIC_;
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000438
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000439 const FeatureBitset &Features = STI.getFeatureBits();
Eric Christophera5762812015-01-26 17:33:46 +0000440
441 // Set the header flags that we can in the constructor.
442 // FIXME: This is a fairly terrible hack. We set the rest
443 // of these in the destructor. The problem here is two-fold:
444 //
445 // a: Some of the eflags can be set/reset by directives.
446 // b: There aren't any usage paths that initialize the ABI
447 // pointer until after we initialize either an assembler
448 // or the target machine.
449 // We can fix this by making the target streamer construct
450 // the ABI, but this is fraught with wide ranging dependency
451 // issues as well.
452 unsigned EFlags = MCA.getELFHeaderEFlags();
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000453
454 // Architecture
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000455 if (Features[Mips::FeatureMips64r6])
Daniel Sanders950f48d2014-07-04 15:21:53 +0000456 EFlags |= ELF::EF_MIPS_ARCH_64R6;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000457 else if (Features[Mips::FeatureMips64r2] ||
458 Features[Mips::FeatureMips64r3] ||
459 Features[Mips::FeatureMips64r5])
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000460 EFlags |= ELF::EF_MIPS_ARCH_64R2;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000461 else if (Features[Mips::FeatureMips64])
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000462 EFlags |= ELF::EF_MIPS_ARCH_64;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000463 else if (Features[Mips::FeatureMips5])
Daniel Sanders950f48d2014-07-04 15:21:53 +0000464 EFlags |= ELF::EF_MIPS_ARCH_5;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000465 else if (Features[Mips::FeatureMips4])
Daniel Sandersf7b32292014-04-03 12:13:36 +0000466 EFlags |= ELF::EF_MIPS_ARCH_4;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000467 else if (Features[Mips::FeatureMips3])
Daniel Sanders950f48d2014-07-04 15:21:53 +0000468 EFlags |= ELF::EF_MIPS_ARCH_3;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000469 else if (Features[Mips::FeatureMips32r6])
Daniel Sanders950f48d2014-07-04 15:21:53 +0000470 EFlags |= ELF::EF_MIPS_ARCH_32R6;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000471 else if (Features[Mips::FeatureMips32r2] ||
472 Features[Mips::FeatureMips32r3] ||
473 Features[Mips::FeatureMips32r5])
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000474 EFlags |= ELF::EF_MIPS_ARCH_32R2;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000475 else if (Features[Mips::FeatureMips32])
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000476 EFlags |= ELF::EF_MIPS_ARCH_32;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000477 else if (Features[Mips::FeatureMips2])
Daniel Sanders950f48d2014-07-04 15:21:53 +0000478 EFlags |= ELF::EF_MIPS_ARCH_2;
479 else
480 EFlags |= ELF::EF_MIPS_ARCH_1;
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000481
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000482 // Other options.
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000483 if (Features[Mips::FeatureNaN2008])
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000484 EFlags |= ELF::EF_MIPS_NAN2008;
485
Daniel Sanders16ec6c12014-07-17 09:52:56 +0000486 // -mabicalls and -mplt are not implemented but we should act as if they were
487 // given.
488 EFlags |= ELF::EF_MIPS_CPIC;
Daniel Sanders16ec6c12014-07-17 09:52:56 +0000489
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000490 MCA.setELFHeaderEFlags(EFlags);
491}
Jack Carter86ac5c12013-11-18 23:55:27 +0000492
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000493void MipsTargetELFStreamer::emitLabel(MCSymbol *S) {
494 auto *Symbol = cast<MCSymbolELF>(S);
Rafael Espindola26e917c2014-01-15 03:07:12 +0000495 if (!isMicroMipsEnabled())
496 return;
Rafael Espindolac73aed12015-06-03 19:03:11 +0000497 getStreamer().getAssembler().registerSymbol(*Symbol);
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000498 uint8_t Type = Symbol->getType();
Rafael Espindola26e917c2014-01-15 03:07:12 +0000499 if (Type != ELF::STT_FUNC)
500 return;
501
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000502 Symbol->setOther(ELF::STO_MIPS_MICROMIPS);
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000503}
504
Rafael Espindola972e71a2014-01-31 23:10:26 +0000505void MipsTargetELFStreamer::finish() {
506 MCAssembler &MCA = getStreamer().getAssembler();
Daniel Sanders68c37472014-07-21 13:30:55 +0000507 const MCObjectFileInfo &OFI = *MCA.getContext().getObjectFileInfo();
Rafael Espindola972e71a2014-01-31 23:10:26 +0000508
Daniel Sanders41ffa5d2014-07-14 15:05:51 +0000509 // .bss, .text and .data are always at least 16-byte aligned.
Rafael Espindola967d6a62015-05-21 21:02:35 +0000510 MCSection &TextSection = *OFI.getTextSection();
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000511 MCA.registerSection(TextSection);
Rafael Espindola967d6a62015-05-21 21:02:35 +0000512 MCSection &DataSection = *OFI.getDataSection();
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000513 MCA.registerSection(DataSection);
Rafael Espindola967d6a62015-05-21 21:02:35 +0000514 MCSection &BSSSection = *OFI.getBSSSection();
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000515 MCA.registerSection(BSSSection);
Daniel Sanders41ffa5d2014-07-14 15:05:51 +0000516
Rafael Espindola967d6a62015-05-21 21:02:35 +0000517 TextSection.setAlignment(std::max(16u, TextSection.getAlignment()));
518 DataSection.setAlignment(std::max(16u, DataSection.getAlignment()));
519 BSSSection.setAlignment(std::max(16u, BSSSection.getAlignment()));
Daniel Sanders41ffa5d2014-07-14 15:05:51 +0000520
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000521 const FeatureBitset &Features = STI.getFeatureBits();
Eric Christophera5762812015-01-26 17:33:46 +0000522
523 // Update e_header flags. See the FIXME and comment above in
524 // the constructor for a full rundown on this.
525 unsigned EFlags = MCA.getELFHeaderEFlags();
526
527 // ABI
528 // N64 does not require any ABI bits.
529 if (getABI().IsO32())
530 EFlags |= ELF::EF_MIPS_ABI_O32;
531 else if (getABI().IsN32())
532 EFlags |= ELF::EF_MIPS_ABI2;
533
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000534 if (Features[Mips::FeatureGP64Bit]) {
Eric Christophera5762812015-01-26 17:33:46 +0000535 if (getABI().IsO32())
536 EFlags |= ELF::EF_MIPS_32BITMODE; /* Compatibility Mode */
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000537 } else if (Features[Mips::FeatureMips64r2] || Features[Mips::FeatureMips64])
Eric Christophera5762812015-01-26 17:33:46 +0000538 EFlags |= ELF::EF_MIPS_32BITMODE;
539
540 // If we've set the cpic eflag and we're n64, go ahead and set the pic
541 // one as well.
542 if (EFlags & ELF::EF_MIPS_CPIC && getABI().IsN64())
543 EFlags |= ELF::EF_MIPS_PIC;
544
545 MCA.setELFHeaderEFlags(EFlags);
546
Daniel Sanders68c37472014-07-21 13:30:55 +0000547 // Emit all the option records.
548 // At the moment we are only emitting .Mips.options (ODK_REGINFO) and
549 // .reginfo.
550 MipsELFStreamer &MEF = static_cast<MipsELFStreamer &>(Streamer);
551 MEF.EmitMipsOptionRecords();
Rafael Espindola972e71a2014-01-31 23:10:26 +0000552
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000553 emitMipsAbiFlags();
Rafael Espindola972e71a2014-01-31 23:10:26 +0000554}
555
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000556void MipsTargetELFStreamer::emitAssignment(MCSymbol *S, const MCExpr *Value) {
557 auto *Symbol = cast<MCSymbolELF>(S);
Zoran Jovanovic28221d82014-03-20 09:44:49 +0000558 // If on rhs is micromips symbol then mark Symbol as microMips.
559 if (Value->getKind() != MCExpr::SymbolRef)
560 return;
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000561 const auto &RhsSym = cast<MCSymbolELF>(
562 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol());
Toma Tabacu2cc44f52015-04-16 13:37:32 +0000563
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000564 if (!(RhsSym.getOther() & ELF::STO_MIPS_MICROMIPS))
Zoran Jovanovic28221d82014-03-20 09:44:49 +0000565 return;
566
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000567 Symbol->setOther(ELF::STO_MIPS_MICROMIPS);
Zoran Jovanovic28221d82014-03-20 09:44:49 +0000568}
569
Jack Carter86ac5c12013-11-18 23:55:27 +0000570MCELFStreamer &MipsTargetELFStreamer::getStreamer() {
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000571 return static_cast<MCELFStreamer &>(Streamer);
Jack Carter86ac5c12013-11-18 23:55:27 +0000572}
573
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000574void MipsTargetELFStreamer::emitDirectiveSetMicroMips() {
575 MicroMipsEnabled = true;
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000576
577 MCAssembler &MCA = getStreamer().getAssembler();
578 unsigned Flags = MCA.getELFHeaderEFlags();
579 Flags |= ELF::EF_MIPS_MICROMIPS;
580 MCA.setELFHeaderEFlags(Flags);
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000581 forbidModuleDirective();
Jack Carter86ac5c12013-11-18 23:55:27 +0000582}
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000583
584void MipsTargetELFStreamer::emitDirectiveSetNoMicroMips() {
585 MicroMipsEnabled = false;
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000586 forbidModuleDirective();
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000587}
588
Rafael Espindola6633d572014-01-14 18:57:12 +0000589void MipsTargetELFStreamer::emitDirectiveSetMips16() {
Rafael Espindolae7583752014-01-24 16:13:20 +0000590 MCAssembler &MCA = getStreamer().getAssembler();
591 unsigned Flags = MCA.getELFHeaderEFlags();
592 Flags |= ELF::EF_MIPS_ARCH_ASE_M16;
593 MCA.setELFHeaderEFlags(Flags);
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000594 forbidModuleDirective();
Rafael Espindola6633d572014-01-14 18:57:12 +0000595}
596
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000597void MipsTargetELFStreamer::emitDirectiveSetNoReorder() {
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000598 MCAssembler &MCA = getStreamer().getAssembler();
599 unsigned Flags = MCA.getELFHeaderEFlags();
600 Flags |= ELF::EF_MIPS_NOREORDER;
601 MCA.setELFHeaderEFlags(Flags);
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000602 forbidModuleDirective();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000603}
604
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000605void MipsTargetELFStreamer::emitDirectiveEnd(StringRef Name) {
Daniel Sandersd97a6342014-08-13 10:07:34 +0000606 MCAssembler &MCA = getStreamer().getAssembler();
607 MCContext &Context = MCA.getContext();
608 MCStreamer &OS = getStreamer();
609
Scott Egerton219fae92016-02-17 11:15:16 +0000610 MCSectionELF *Sec = Context.getELFSection(".pdr", ELF::SHT_PROGBITS, 0);
Daniel Sandersd97a6342014-08-13 10:07:34 +0000611
Daniel Sanders2b561332015-11-23 16:08:03 +0000612 MCSymbol *Sym = Context.getOrCreateSymbol(Name);
Daniel Sandersd97a6342014-08-13 10:07:34 +0000613 const MCSymbolRefExpr *ExprRef =
Daniel Sanders2b561332015-11-23 16:08:03 +0000614 MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, Context);
Daniel Sandersd97a6342014-08-13 10:07:34 +0000615
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000616 MCA.registerSection(*Sec);
Rafael Espindola967d6a62015-05-21 21:02:35 +0000617 Sec->setAlignment(4);
Daniel Sandersd97a6342014-08-13 10:07:34 +0000618
619 OS.PushSection();
620
621 OS.SwitchSection(Sec);
622
623 OS.EmitValueImpl(ExprRef, 4);
624
625 OS.EmitIntValue(GPRInfoSet ? GPRBitMask : 0, 4); // reg_mask
626 OS.EmitIntValue(GPRInfoSet ? GPROffset : 0, 4); // reg_offset
627
628 OS.EmitIntValue(FPRInfoSet ? FPRBitMask : 0, 4); // fpreg_mask
629 OS.EmitIntValue(FPRInfoSet ? FPROffset : 0, 4); // fpreg_offset
630
631 OS.EmitIntValue(FrameInfoSet ? FrameOffset : 0, 4); // frame_offset
632 OS.EmitIntValue(FrameInfoSet ? FrameReg : 0, 4); // frame_reg
633 OS.EmitIntValue(FrameInfoSet ? ReturnReg : 0, 4); // return_reg
634
635 // The .end directive marks the end of a procedure. Invalidate
636 // the information gathered up until this point.
637 GPRInfoSet = FPRInfoSet = FrameInfoSet = false;
638
639 OS.PopSection();
Daniel Sanders2b561332015-11-23 16:08:03 +0000640
641 // .end also implicitly sets the size.
642 MCSymbol *CurPCSym = Context.createTempSymbol();
643 OS.EmitLabel(CurPCSym);
644 const MCExpr *Size = MCBinaryExpr::createSub(
645 MCSymbolRefExpr::create(CurPCSym, MCSymbolRefExpr::VK_None, Context),
646 ExprRef, Context);
647 int64_t AbsSize;
648 if (!Size->evaluateAsAbsolute(AbsSize, MCA))
649 llvm_unreachable("Function size must be evaluatable as absolute");
650 Size = MCConstantExpr::create(AbsSize, Context);
651 static_cast<MCSymbolELF *>(Sym)->setSize(Size);
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000652}
653
Rafael Espindola6633d572014-01-14 18:57:12 +0000654void MipsTargetELFStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {
Daniel Sandersd97a6342014-08-13 10:07:34 +0000655 GPRInfoSet = FPRInfoSet = FrameInfoSet = false;
Daniel Sanders2b561332015-11-23 16:08:03 +0000656
657 // .ent also acts like an implicit '.type symbol, STT_FUNC'
658 static_cast<const MCSymbolELF &>(Symbol).setType(ELF::STT_FUNC);
Rafael Espindola6633d572014-01-14 18:57:12 +0000659}
660
Jack Carter0cd3c192014-01-06 23:27:31 +0000661void MipsTargetELFStreamer::emitDirectiveAbiCalls() {
662 MCAssembler &MCA = getStreamer().getAssembler();
663 unsigned Flags = MCA.getELFHeaderEFlags();
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000664 Flags |= ELF::EF_MIPS_CPIC | ELF::EF_MIPS_PIC;
Jack Carter0cd3c192014-01-06 23:27:31 +0000665 MCA.setELFHeaderEFlags(Flags);
666}
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000667
668void MipsTargetELFStreamer::emitDirectiveNaN2008() {
669 MCAssembler &MCA = getStreamer().getAssembler();
670 unsigned Flags = MCA.getELFHeaderEFlags();
671 Flags |= ELF::EF_MIPS_NAN2008;
672 MCA.setELFHeaderEFlags(Flags);
673}
674
675void MipsTargetELFStreamer::emitDirectiveNaNLegacy() {
676 MCAssembler &MCA = getStreamer().getAssembler();
677 unsigned Flags = MCA.getELFHeaderEFlags();
678 Flags &= ~ELF::EF_MIPS_NAN2008;
679 MCA.setELFHeaderEFlags(Flags);
680}
681
Jack Carter0cd3c192014-01-06 23:27:31 +0000682void MipsTargetELFStreamer::emitDirectiveOptionPic0() {
683 MCAssembler &MCA = getStreamer().getAssembler();
684 unsigned Flags = MCA.getELFHeaderEFlags();
Matheus Almeidaf79b2812014-03-26 13:40:29 +0000685 // This option overrides other PIC options like -KPIC.
686 Pic = false;
Jack Carter0cd3c192014-01-06 23:27:31 +0000687 Flags &= ~ELF::EF_MIPS_PIC;
688 MCA.setELFHeaderEFlags(Flags);
689}
Rafael Espindola054234f2014-01-27 03:53:56 +0000690
Matheus Almeidaf79b2812014-03-26 13:40:29 +0000691void MipsTargetELFStreamer::emitDirectiveOptionPic2() {
692 MCAssembler &MCA = getStreamer().getAssembler();
693 unsigned Flags = MCA.getELFHeaderEFlags();
694 Pic = true;
695 // NOTE: We are following the GAS behaviour here which means the directive
696 // 'pic2' also sets the CPIC bit in the ELF header. This is different from
697 // what is stated in the SYSV ABI which consider the bits EF_MIPS_PIC and
698 // EF_MIPS_CPIC to be mutually exclusive.
699 Flags |= ELF::EF_MIPS_PIC | ELF::EF_MIPS_CPIC;
700 MCA.setELFHeaderEFlags(Flags);
701}
702
Toma Tabacu9ca50962015-04-16 09:53:47 +0000703void MipsTargetELFStreamer::emitDirectiveInsn() {
704 MipsTargetStreamer::emitDirectiveInsn();
705 MipsELFStreamer &MEF = static_cast<MipsELFStreamer &>(Streamer);
706 MEF.createPendingLabelRelocs();
707}
708
Rafael Espindola054234f2014-01-27 03:53:56 +0000709void MipsTargetELFStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
Daniel Sandersd97a6342014-08-13 10:07:34 +0000710 unsigned ReturnReg_) {
711 MCContext &Context = getStreamer().getAssembler().getContext();
712 const MCRegisterInfo *RegInfo = Context.getRegisterInfo();
713
714 FrameInfoSet = true;
715 FrameReg = RegInfo->getEncodingValue(StackReg);
716 FrameOffset = StackSize;
717 ReturnReg = RegInfo->getEncodingValue(ReturnReg_);
Rafael Espindola054234f2014-01-27 03:53:56 +0000718}
Rafael Espindola25fa2912014-01-27 04:33:11 +0000719
720void MipsTargetELFStreamer::emitMask(unsigned CPUBitmask,
721 int CPUTopSavedRegOff) {
Daniel Sandersd97a6342014-08-13 10:07:34 +0000722 GPRInfoSet = true;
723 GPRBitMask = CPUBitmask;
724 GPROffset = CPUTopSavedRegOff;
Rafael Espindola25fa2912014-01-27 04:33:11 +0000725}
726
727void MipsTargetELFStreamer::emitFMask(unsigned FPUBitmask,
728 int FPUTopSavedRegOff) {
Daniel Sandersd97a6342014-08-13 10:07:34 +0000729 FPRInfoSet = true;
730 FPRBitMask = FPUBitmask;
731 FPROffset = FPUTopSavedRegOff;
Rafael Espindola25fa2912014-01-27 04:33:11 +0000732}
Vladimir Medic615b26e2014-03-04 09:54:09 +0000733
Toma Tabacuc4c202a2014-10-01 14:53:19 +0000734void MipsTargetELFStreamer::emitDirectiveCpLoad(unsigned RegNo) {
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000735 // .cpload $reg
736 // This directive expands to:
737 // lui $gp, %hi(_gp_disp)
738 // addui $gp, $gp, %lo(_gp_disp)
739 // addu $gp, $gp, $reg
740 // when support for position independent code is enabled.
Eric Christophera5762812015-01-26 17:33:46 +0000741 if (!Pic || (getABI().IsN32() || getABI().IsN64()))
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000742 return;
743
744 // There's a GNU extension controlled by -mno-shared that allows
745 // locally-binding symbols to be accessed using absolute addresses.
746 // This is currently not supported. When supported -mno-shared makes
747 // .cpload expand to:
748 // lui $gp, %hi(__gnu_local_gp)
749 // addiu $gp, $gp, %lo(__gnu_local_gp)
750
751 StringRef SymName("_gp_disp");
752 MCAssembler &MCA = getStreamer().getAssembler();
Jim Grosbach6f482002015-05-18 18:43:14 +0000753 MCSymbol *GP_Disp = MCA.getContext().getOrCreateSymbol(SymName);
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000754 MCA.registerSymbol(*GP_Disp);
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000755
756 MCInst TmpInst;
757 TmpInst.setOpcode(Mips::LUi);
Jim Grosbache9119e42015-05-13 18:37:00 +0000758 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
Jim Grosbach13760bd2015-05-30 01:25:56 +0000759 const MCSymbolRefExpr *HiSym = MCSymbolRefExpr::create(
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000760 "_gp_disp", MCSymbolRefExpr::VK_Mips_ABS_HI, MCA.getContext());
Jim Grosbache9119e42015-05-13 18:37:00 +0000761 TmpInst.addOperand(MCOperand::createExpr(HiSym));
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000762 getStreamer().EmitInstruction(TmpInst, STI);
763
764 TmpInst.clear();
765
766 TmpInst.setOpcode(Mips::ADDiu);
Jim Grosbache9119e42015-05-13 18:37:00 +0000767 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
768 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
Jim Grosbach13760bd2015-05-30 01:25:56 +0000769 const MCSymbolRefExpr *LoSym = MCSymbolRefExpr::create(
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000770 "_gp_disp", MCSymbolRefExpr::VK_Mips_ABS_LO, MCA.getContext());
Jim Grosbache9119e42015-05-13 18:37:00 +0000771 TmpInst.addOperand(MCOperand::createExpr(LoSym));
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000772 getStreamer().EmitInstruction(TmpInst, STI);
773
774 TmpInst.clear();
775
776 TmpInst.setOpcode(Mips::ADDu);
Jim Grosbache9119e42015-05-13 18:37:00 +0000777 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
778 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
779 TmpInst.addOperand(MCOperand::createReg(RegNo));
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000780 getStreamer().EmitInstruction(TmpInst, STI);
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000781
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000782 forbidModuleDirective();
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000783}
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000784
Daniel Sandersc6924fa2016-04-18 12:06:15 +0000785void MipsTargetELFStreamer::emitDirectiveCpRestore(int Offset) {
786 MipsTargetStreamer::emitDirectiveCpRestore(Offset);
Daniel Sanderse2982ad2015-09-17 16:08:39 +0000787 // .cprestore offset
788 // When PIC mode is enabled and the O32 ABI is used, this directive expands
789 // to:
790 // sw $gp, offset($sp)
791 // and adds a corresponding LW after every JAL.
792
793 // Note that .cprestore is ignored if used with the N32 and N64 ABIs or if it
794 // is used in non-PIC mode.
795 if (!Pic || (getABI().IsN32() || getABI().IsN64()))
796 return;
797
Daniel Sandersc6924fa2016-04-18 12:06:15 +0000798 // FIXME: MipsAsmParser currently emits the instructions that should be
799 // emitted here.
Daniel Sanderse2982ad2015-09-17 16:08:39 +0000800}
801
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000802void MipsTargetELFStreamer::emitDirectiveCpsetup(unsigned RegNo,
803 int RegOrOffset,
804 const MCSymbol &Sym,
805 bool IsReg) {
806 // Only N32 and N64 emit anything for .cpsetup iff PIC is set.
Eric Christophera5762812015-01-26 17:33:46 +0000807 if (!Pic || !(getABI().IsN32() || getABI().IsN64()))
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000808 return;
809
810 MCAssembler &MCA = getStreamer().getAssembler();
811 MCInst Inst;
812
813 // Either store the old $gp in a register or on the stack
814 if (IsReg) {
815 // move $save, $gpreg
Vasileios Kalintiris1c78ca62015-08-11 08:56:25 +0000816 Inst.setOpcode(Mips::OR64);
Jim Grosbache9119e42015-05-13 18:37:00 +0000817 Inst.addOperand(MCOperand::createReg(RegOrOffset));
818 Inst.addOperand(MCOperand::createReg(Mips::GP));
819 Inst.addOperand(MCOperand::createReg(Mips::ZERO));
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000820 } else {
821 // sd $gpreg, offset($sp)
822 Inst.setOpcode(Mips::SD);
Jim Grosbache9119e42015-05-13 18:37:00 +0000823 Inst.addOperand(MCOperand::createReg(Mips::GP));
824 Inst.addOperand(MCOperand::createReg(Mips::SP));
825 Inst.addOperand(MCOperand::createImm(RegOrOffset));
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000826 }
827 getStreamer().EmitInstruction(Inst, STI);
828 Inst.clear();
829
Jim Grosbach13760bd2015-05-30 01:25:56 +0000830 const MCSymbolRefExpr *HiExpr = MCSymbolRefExpr::create(
Toma Tabacu8874eac2015-02-18 13:46:53 +0000831 &Sym, MCSymbolRefExpr::VK_Mips_GPOFF_HI, MCA.getContext());
Jim Grosbach13760bd2015-05-30 01:25:56 +0000832 const MCSymbolRefExpr *LoExpr = MCSymbolRefExpr::create(
Toma Tabacu8874eac2015-02-18 13:46:53 +0000833 &Sym, MCSymbolRefExpr::VK_Mips_GPOFF_LO, MCA.getContext());
834
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000835 // lui $gp, %hi(%neg(%gp_rel(funcSym)))
836 Inst.setOpcode(Mips::LUi);
Jim Grosbache9119e42015-05-13 18:37:00 +0000837 Inst.addOperand(MCOperand::createReg(Mips::GP));
838 Inst.addOperand(MCOperand::createExpr(HiExpr));
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000839 getStreamer().EmitInstruction(Inst, STI);
840 Inst.clear();
841
842 // addiu $gp, $gp, %lo(%neg(%gp_rel(funcSym)))
843 Inst.setOpcode(Mips::ADDiu);
Jim Grosbache9119e42015-05-13 18:37:00 +0000844 Inst.addOperand(MCOperand::createReg(Mips::GP));
845 Inst.addOperand(MCOperand::createReg(Mips::GP));
846 Inst.addOperand(MCOperand::createExpr(LoExpr));
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000847 getStreamer().EmitInstruction(Inst, STI);
848 Inst.clear();
849
850 // daddu $gp, $gp, $funcreg
851 Inst.setOpcode(Mips::DADDu);
Jim Grosbache9119e42015-05-13 18:37:00 +0000852 Inst.addOperand(MCOperand::createReg(Mips::GP));
853 Inst.addOperand(MCOperand::createReg(Mips::GP));
854 Inst.addOperand(MCOperand::createReg(RegNo));
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000855 getStreamer().EmitInstruction(Inst, STI);
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000856
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000857 forbidModuleDirective();
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000858}
859
Daniel Sandersf173dda2015-09-22 10:50:09 +0000860void MipsTargetELFStreamer::emitDirectiveCpreturn(unsigned SaveLocation,
861 bool SaveLocationIsRegister) {
862 // Only N32 and N64 emit anything for .cpreturn iff PIC is set.
863 if (!Pic || !(getABI().IsN32() || getABI().IsN64()))
864 return;
865
866 MCInst Inst;
867 // Either restore the old $gp from a register or on the stack
868 if (SaveLocationIsRegister) {
869 Inst.setOpcode(Mips::OR);
870 Inst.addOperand(MCOperand::createReg(Mips::GP));
871 Inst.addOperand(MCOperand::createReg(SaveLocation));
872 Inst.addOperand(MCOperand::createReg(Mips::ZERO));
873 } else {
874 Inst.setOpcode(Mips::LD);
875 Inst.addOperand(MCOperand::createReg(Mips::GP));
876 Inst.addOperand(MCOperand::createReg(Mips::SP));
877 Inst.addOperand(MCOperand::createImm(SaveLocation));
878 }
879 getStreamer().EmitInstruction(Inst, STI);
880
881 forbidModuleDirective();
882}
883
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000884void MipsTargetELFStreamer::emitMipsAbiFlags() {
885 MCAssembler &MCA = getStreamer().getAssembler();
886 MCContext &Context = MCA.getContext();
887 MCStreamer &OS = getStreamer();
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000888 MCSectionELF *Sec = Context.getELFSection(
Rafael Espindolaba31e272015-01-29 17:33:21 +0000889 ".MIPS.abiflags", ELF::SHT_MIPS_ABIFLAGS, ELF::SHF_ALLOC, 24, "");
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000890 MCA.registerSection(*Sec);
Rafael Espindola967d6a62015-05-21 21:02:35 +0000891 Sec->setAlignment(8);
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000892 OS.SwitchSection(Sec);
893
Daniel Sandersc7dbc632014-07-08 10:11:38 +0000894 OS << ABIFlagsSection;
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000895}