blob: 9ae244bd17bcd58ae606ad3c243100312d90e613 [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
Daniel Sanders9db710a2016-04-29 12:44:07 +0000521 // Make sections sizes a multiple of the alignment.
522 MCStreamer &OS = getStreamer();
523 for (MCSection &S : MCA) {
524 MCSectionELF &Section = static_cast<MCSectionELF &>(S);
525
526 unsigned Alignment = Section.getAlignment();
527 if (Alignment) {
528 OS.SwitchSection(&Section);
529 if (Section.UseCodeAlign())
530 OS.EmitCodeAlignment(Alignment, Alignment);
531 else
532 OS.EmitValueToAlignment(Alignment, 0, 1, Alignment);
533 }
534 }
535
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000536 const FeatureBitset &Features = STI.getFeatureBits();
Eric Christophera5762812015-01-26 17:33:46 +0000537
538 // Update e_header flags. See the FIXME and comment above in
539 // the constructor for a full rundown on this.
540 unsigned EFlags = MCA.getELFHeaderEFlags();
541
542 // ABI
543 // N64 does not require any ABI bits.
544 if (getABI().IsO32())
545 EFlags |= ELF::EF_MIPS_ABI_O32;
546 else if (getABI().IsN32())
547 EFlags |= ELF::EF_MIPS_ABI2;
548
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000549 if (Features[Mips::FeatureGP64Bit]) {
Eric Christophera5762812015-01-26 17:33:46 +0000550 if (getABI().IsO32())
551 EFlags |= ELF::EF_MIPS_32BITMODE; /* Compatibility Mode */
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000552 } else if (Features[Mips::FeatureMips64r2] || Features[Mips::FeatureMips64])
Eric Christophera5762812015-01-26 17:33:46 +0000553 EFlags |= ELF::EF_MIPS_32BITMODE;
554
555 // If we've set the cpic eflag and we're n64, go ahead and set the pic
556 // one as well.
557 if (EFlags & ELF::EF_MIPS_CPIC && getABI().IsN64())
558 EFlags |= ELF::EF_MIPS_PIC;
559
560 MCA.setELFHeaderEFlags(EFlags);
561
Daniel Sanders68c37472014-07-21 13:30:55 +0000562 // Emit all the option records.
563 // At the moment we are only emitting .Mips.options (ODK_REGINFO) and
564 // .reginfo.
565 MipsELFStreamer &MEF = static_cast<MipsELFStreamer &>(Streamer);
566 MEF.EmitMipsOptionRecords();
Rafael Espindola972e71a2014-01-31 23:10:26 +0000567
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000568 emitMipsAbiFlags();
Rafael Espindola972e71a2014-01-31 23:10:26 +0000569}
570
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000571void MipsTargetELFStreamer::emitAssignment(MCSymbol *S, const MCExpr *Value) {
572 auto *Symbol = cast<MCSymbolELF>(S);
Zoran Jovanovic28221d82014-03-20 09:44:49 +0000573 // If on rhs is micromips symbol then mark Symbol as microMips.
574 if (Value->getKind() != MCExpr::SymbolRef)
575 return;
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000576 const auto &RhsSym = cast<MCSymbolELF>(
577 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol());
Toma Tabacu2cc44f52015-04-16 13:37:32 +0000578
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000579 if (!(RhsSym.getOther() & ELF::STO_MIPS_MICROMIPS))
Zoran Jovanovic28221d82014-03-20 09:44:49 +0000580 return;
581
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000582 Symbol->setOther(ELF::STO_MIPS_MICROMIPS);
Zoran Jovanovic28221d82014-03-20 09:44:49 +0000583}
584
Jack Carter86ac5c12013-11-18 23:55:27 +0000585MCELFStreamer &MipsTargetELFStreamer::getStreamer() {
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000586 return static_cast<MCELFStreamer &>(Streamer);
Jack Carter86ac5c12013-11-18 23:55:27 +0000587}
588
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000589void MipsTargetELFStreamer::emitDirectiveSetMicroMips() {
590 MicroMipsEnabled = true;
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000591
592 MCAssembler &MCA = getStreamer().getAssembler();
593 unsigned Flags = MCA.getELFHeaderEFlags();
594 Flags |= ELF::EF_MIPS_MICROMIPS;
595 MCA.setELFHeaderEFlags(Flags);
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000596 forbidModuleDirective();
Jack Carter86ac5c12013-11-18 23:55:27 +0000597}
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000598
599void MipsTargetELFStreamer::emitDirectiveSetNoMicroMips() {
600 MicroMipsEnabled = false;
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000601 forbidModuleDirective();
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000602}
603
Rafael Espindola6633d572014-01-14 18:57:12 +0000604void MipsTargetELFStreamer::emitDirectiveSetMips16() {
Rafael Espindolae7583752014-01-24 16:13:20 +0000605 MCAssembler &MCA = getStreamer().getAssembler();
606 unsigned Flags = MCA.getELFHeaderEFlags();
607 Flags |= ELF::EF_MIPS_ARCH_ASE_M16;
608 MCA.setELFHeaderEFlags(Flags);
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000609 forbidModuleDirective();
Rafael Espindola6633d572014-01-14 18:57:12 +0000610}
611
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000612void MipsTargetELFStreamer::emitDirectiveSetNoReorder() {
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000613 MCAssembler &MCA = getStreamer().getAssembler();
614 unsigned Flags = MCA.getELFHeaderEFlags();
615 Flags |= ELF::EF_MIPS_NOREORDER;
616 MCA.setELFHeaderEFlags(Flags);
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000617 forbidModuleDirective();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000618}
619
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000620void MipsTargetELFStreamer::emitDirectiveEnd(StringRef Name) {
Daniel Sandersd97a6342014-08-13 10:07:34 +0000621 MCAssembler &MCA = getStreamer().getAssembler();
622 MCContext &Context = MCA.getContext();
623 MCStreamer &OS = getStreamer();
624
Scott Egerton219fae92016-02-17 11:15:16 +0000625 MCSectionELF *Sec = Context.getELFSection(".pdr", ELF::SHT_PROGBITS, 0);
Daniel Sandersd97a6342014-08-13 10:07:34 +0000626
Daniel Sanders2b561332015-11-23 16:08:03 +0000627 MCSymbol *Sym = Context.getOrCreateSymbol(Name);
Daniel Sandersd97a6342014-08-13 10:07:34 +0000628 const MCSymbolRefExpr *ExprRef =
Daniel Sanders2b561332015-11-23 16:08:03 +0000629 MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, Context);
Daniel Sandersd97a6342014-08-13 10:07:34 +0000630
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000631 MCA.registerSection(*Sec);
Rafael Espindola967d6a62015-05-21 21:02:35 +0000632 Sec->setAlignment(4);
Daniel Sandersd97a6342014-08-13 10:07:34 +0000633
634 OS.PushSection();
635
636 OS.SwitchSection(Sec);
637
638 OS.EmitValueImpl(ExprRef, 4);
639
640 OS.EmitIntValue(GPRInfoSet ? GPRBitMask : 0, 4); // reg_mask
641 OS.EmitIntValue(GPRInfoSet ? GPROffset : 0, 4); // reg_offset
642
643 OS.EmitIntValue(FPRInfoSet ? FPRBitMask : 0, 4); // fpreg_mask
644 OS.EmitIntValue(FPRInfoSet ? FPROffset : 0, 4); // fpreg_offset
645
646 OS.EmitIntValue(FrameInfoSet ? FrameOffset : 0, 4); // frame_offset
647 OS.EmitIntValue(FrameInfoSet ? FrameReg : 0, 4); // frame_reg
648 OS.EmitIntValue(FrameInfoSet ? ReturnReg : 0, 4); // return_reg
649
650 // The .end directive marks the end of a procedure. Invalidate
651 // the information gathered up until this point.
652 GPRInfoSet = FPRInfoSet = FrameInfoSet = false;
653
654 OS.PopSection();
Daniel Sanders2b561332015-11-23 16:08:03 +0000655
656 // .end also implicitly sets the size.
657 MCSymbol *CurPCSym = Context.createTempSymbol();
658 OS.EmitLabel(CurPCSym);
659 const MCExpr *Size = MCBinaryExpr::createSub(
660 MCSymbolRefExpr::create(CurPCSym, MCSymbolRefExpr::VK_None, Context),
661 ExprRef, Context);
662 int64_t AbsSize;
663 if (!Size->evaluateAsAbsolute(AbsSize, MCA))
664 llvm_unreachable("Function size must be evaluatable as absolute");
665 Size = MCConstantExpr::create(AbsSize, Context);
666 static_cast<MCSymbolELF *>(Sym)->setSize(Size);
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000667}
668
Rafael Espindola6633d572014-01-14 18:57:12 +0000669void MipsTargetELFStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {
Daniel Sandersd97a6342014-08-13 10:07:34 +0000670 GPRInfoSet = FPRInfoSet = FrameInfoSet = false;
Daniel Sanders2b561332015-11-23 16:08:03 +0000671
672 // .ent also acts like an implicit '.type symbol, STT_FUNC'
673 static_cast<const MCSymbolELF &>(Symbol).setType(ELF::STT_FUNC);
Rafael Espindola6633d572014-01-14 18:57:12 +0000674}
675
Jack Carter0cd3c192014-01-06 23:27:31 +0000676void MipsTargetELFStreamer::emitDirectiveAbiCalls() {
677 MCAssembler &MCA = getStreamer().getAssembler();
678 unsigned Flags = MCA.getELFHeaderEFlags();
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000679 Flags |= ELF::EF_MIPS_CPIC | ELF::EF_MIPS_PIC;
Jack Carter0cd3c192014-01-06 23:27:31 +0000680 MCA.setELFHeaderEFlags(Flags);
681}
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000682
683void MipsTargetELFStreamer::emitDirectiveNaN2008() {
684 MCAssembler &MCA = getStreamer().getAssembler();
685 unsigned Flags = MCA.getELFHeaderEFlags();
686 Flags |= ELF::EF_MIPS_NAN2008;
687 MCA.setELFHeaderEFlags(Flags);
688}
689
690void MipsTargetELFStreamer::emitDirectiveNaNLegacy() {
691 MCAssembler &MCA = getStreamer().getAssembler();
692 unsigned Flags = MCA.getELFHeaderEFlags();
693 Flags &= ~ELF::EF_MIPS_NAN2008;
694 MCA.setELFHeaderEFlags(Flags);
695}
696
Jack Carter0cd3c192014-01-06 23:27:31 +0000697void MipsTargetELFStreamer::emitDirectiveOptionPic0() {
698 MCAssembler &MCA = getStreamer().getAssembler();
699 unsigned Flags = MCA.getELFHeaderEFlags();
Matheus Almeidaf79b2812014-03-26 13:40:29 +0000700 // This option overrides other PIC options like -KPIC.
701 Pic = false;
Jack Carter0cd3c192014-01-06 23:27:31 +0000702 Flags &= ~ELF::EF_MIPS_PIC;
703 MCA.setELFHeaderEFlags(Flags);
704}
Rafael Espindola054234f2014-01-27 03:53:56 +0000705
Matheus Almeidaf79b2812014-03-26 13:40:29 +0000706void MipsTargetELFStreamer::emitDirectiveOptionPic2() {
707 MCAssembler &MCA = getStreamer().getAssembler();
708 unsigned Flags = MCA.getELFHeaderEFlags();
709 Pic = true;
710 // NOTE: We are following the GAS behaviour here which means the directive
711 // 'pic2' also sets the CPIC bit in the ELF header. This is different from
712 // what is stated in the SYSV ABI which consider the bits EF_MIPS_PIC and
713 // EF_MIPS_CPIC to be mutually exclusive.
714 Flags |= ELF::EF_MIPS_PIC | ELF::EF_MIPS_CPIC;
715 MCA.setELFHeaderEFlags(Flags);
716}
717
Toma Tabacu9ca50962015-04-16 09:53:47 +0000718void MipsTargetELFStreamer::emitDirectiveInsn() {
719 MipsTargetStreamer::emitDirectiveInsn();
720 MipsELFStreamer &MEF = static_cast<MipsELFStreamer &>(Streamer);
721 MEF.createPendingLabelRelocs();
722}
723
Rafael Espindola054234f2014-01-27 03:53:56 +0000724void MipsTargetELFStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
Daniel Sandersd97a6342014-08-13 10:07:34 +0000725 unsigned ReturnReg_) {
726 MCContext &Context = getStreamer().getAssembler().getContext();
727 const MCRegisterInfo *RegInfo = Context.getRegisterInfo();
728
729 FrameInfoSet = true;
730 FrameReg = RegInfo->getEncodingValue(StackReg);
731 FrameOffset = StackSize;
732 ReturnReg = RegInfo->getEncodingValue(ReturnReg_);
Rafael Espindola054234f2014-01-27 03:53:56 +0000733}
Rafael Espindola25fa2912014-01-27 04:33:11 +0000734
735void MipsTargetELFStreamer::emitMask(unsigned CPUBitmask,
736 int CPUTopSavedRegOff) {
Daniel Sandersd97a6342014-08-13 10:07:34 +0000737 GPRInfoSet = true;
738 GPRBitMask = CPUBitmask;
739 GPROffset = CPUTopSavedRegOff;
Rafael Espindola25fa2912014-01-27 04:33:11 +0000740}
741
742void MipsTargetELFStreamer::emitFMask(unsigned FPUBitmask,
743 int FPUTopSavedRegOff) {
Daniel Sandersd97a6342014-08-13 10:07:34 +0000744 FPRInfoSet = true;
745 FPRBitMask = FPUBitmask;
746 FPROffset = FPUTopSavedRegOff;
Rafael Espindola25fa2912014-01-27 04:33:11 +0000747}
Vladimir Medic615b26e2014-03-04 09:54:09 +0000748
Toma Tabacuc4c202a2014-10-01 14:53:19 +0000749void MipsTargetELFStreamer::emitDirectiveCpLoad(unsigned RegNo) {
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000750 // .cpload $reg
751 // This directive expands to:
752 // lui $gp, %hi(_gp_disp)
753 // addui $gp, $gp, %lo(_gp_disp)
754 // addu $gp, $gp, $reg
755 // when support for position independent code is enabled.
Eric Christophera5762812015-01-26 17:33:46 +0000756 if (!Pic || (getABI().IsN32() || getABI().IsN64()))
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000757 return;
758
759 // There's a GNU extension controlled by -mno-shared that allows
760 // locally-binding symbols to be accessed using absolute addresses.
761 // This is currently not supported. When supported -mno-shared makes
762 // .cpload expand to:
763 // lui $gp, %hi(__gnu_local_gp)
764 // addiu $gp, $gp, %lo(__gnu_local_gp)
765
766 StringRef SymName("_gp_disp");
767 MCAssembler &MCA = getStreamer().getAssembler();
Jim Grosbach6f482002015-05-18 18:43:14 +0000768 MCSymbol *GP_Disp = MCA.getContext().getOrCreateSymbol(SymName);
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000769 MCA.registerSymbol(*GP_Disp);
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000770
771 MCInst TmpInst;
772 TmpInst.setOpcode(Mips::LUi);
Jim Grosbache9119e42015-05-13 18:37:00 +0000773 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
Jim Grosbach13760bd2015-05-30 01:25:56 +0000774 const MCSymbolRefExpr *HiSym = MCSymbolRefExpr::create(
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000775 "_gp_disp", MCSymbolRefExpr::VK_Mips_ABS_HI, MCA.getContext());
Jim Grosbache9119e42015-05-13 18:37:00 +0000776 TmpInst.addOperand(MCOperand::createExpr(HiSym));
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000777 getStreamer().EmitInstruction(TmpInst, STI);
778
779 TmpInst.clear();
780
781 TmpInst.setOpcode(Mips::ADDiu);
Jim Grosbache9119e42015-05-13 18:37:00 +0000782 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
783 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
Jim Grosbach13760bd2015-05-30 01:25:56 +0000784 const MCSymbolRefExpr *LoSym = MCSymbolRefExpr::create(
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000785 "_gp_disp", MCSymbolRefExpr::VK_Mips_ABS_LO, MCA.getContext());
Jim Grosbache9119e42015-05-13 18:37:00 +0000786 TmpInst.addOperand(MCOperand::createExpr(LoSym));
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000787 getStreamer().EmitInstruction(TmpInst, STI);
788
789 TmpInst.clear();
790
791 TmpInst.setOpcode(Mips::ADDu);
Jim Grosbache9119e42015-05-13 18:37:00 +0000792 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
793 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
794 TmpInst.addOperand(MCOperand::createReg(RegNo));
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000795 getStreamer().EmitInstruction(TmpInst, STI);
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000796
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000797 forbidModuleDirective();
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000798}
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000799
Daniel Sandersc6924fa2016-04-18 12:06:15 +0000800void MipsTargetELFStreamer::emitDirectiveCpRestore(int Offset) {
801 MipsTargetStreamer::emitDirectiveCpRestore(Offset);
Daniel Sanderse2982ad2015-09-17 16:08:39 +0000802 // .cprestore offset
803 // When PIC mode is enabled and the O32 ABI is used, this directive expands
804 // to:
805 // sw $gp, offset($sp)
806 // and adds a corresponding LW after every JAL.
807
808 // Note that .cprestore is ignored if used with the N32 and N64 ABIs or if it
809 // is used in non-PIC mode.
810 if (!Pic || (getABI().IsN32() || getABI().IsN64()))
811 return;
812
Daniel Sandersc6924fa2016-04-18 12:06:15 +0000813 // FIXME: MipsAsmParser currently emits the instructions that should be
814 // emitted here.
Daniel Sanderse2982ad2015-09-17 16:08:39 +0000815}
816
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000817void MipsTargetELFStreamer::emitDirectiveCpsetup(unsigned RegNo,
818 int RegOrOffset,
819 const MCSymbol &Sym,
820 bool IsReg) {
821 // Only N32 and N64 emit anything for .cpsetup iff PIC is set.
Eric Christophera5762812015-01-26 17:33:46 +0000822 if (!Pic || !(getABI().IsN32() || getABI().IsN64()))
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000823 return;
824
825 MCAssembler &MCA = getStreamer().getAssembler();
826 MCInst Inst;
827
828 // Either store the old $gp in a register or on the stack
829 if (IsReg) {
830 // move $save, $gpreg
Vasileios Kalintiris1c78ca62015-08-11 08:56:25 +0000831 Inst.setOpcode(Mips::OR64);
Jim Grosbache9119e42015-05-13 18:37:00 +0000832 Inst.addOperand(MCOperand::createReg(RegOrOffset));
833 Inst.addOperand(MCOperand::createReg(Mips::GP));
834 Inst.addOperand(MCOperand::createReg(Mips::ZERO));
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000835 } else {
836 // sd $gpreg, offset($sp)
837 Inst.setOpcode(Mips::SD);
Jim Grosbache9119e42015-05-13 18:37:00 +0000838 Inst.addOperand(MCOperand::createReg(Mips::GP));
839 Inst.addOperand(MCOperand::createReg(Mips::SP));
840 Inst.addOperand(MCOperand::createImm(RegOrOffset));
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000841 }
842 getStreamer().EmitInstruction(Inst, STI);
843 Inst.clear();
844
Jim Grosbach13760bd2015-05-30 01:25:56 +0000845 const MCSymbolRefExpr *HiExpr = MCSymbolRefExpr::create(
Toma Tabacu8874eac2015-02-18 13:46:53 +0000846 &Sym, MCSymbolRefExpr::VK_Mips_GPOFF_HI, MCA.getContext());
Jim Grosbach13760bd2015-05-30 01:25:56 +0000847 const MCSymbolRefExpr *LoExpr = MCSymbolRefExpr::create(
Toma Tabacu8874eac2015-02-18 13:46:53 +0000848 &Sym, MCSymbolRefExpr::VK_Mips_GPOFF_LO, MCA.getContext());
849
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000850 // lui $gp, %hi(%neg(%gp_rel(funcSym)))
851 Inst.setOpcode(Mips::LUi);
Jim Grosbache9119e42015-05-13 18:37:00 +0000852 Inst.addOperand(MCOperand::createReg(Mips::GP));
853 Inst.addOperand(MCOperand::createExpr(HiExpr));
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000854 getStreamer().EmitInstruction(Inst, STI);
855 Inst.clear();
856
857 // addiu $gp, $gp, %lo(%neg(%gp_rel(funcSym)))
858 Inst.setOpcode(Mips::ADDiu);
Jim Grosbache9119e42015-05-13 18:37:00 +0000859 Inst.addOperand(MCOperand::createReg(Mips::GP));
860 Inst.addOperand(MCOperand::createReg(Mips::GP));
861 Inst.addOperand(MCOperand::createExpr(LoExpr));
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000862 getStreamer().EmitInstruction(Inst, STI);
863 Inst.clear();
864
865 // daddu $gp, $gp, $funcreg
866 Inst.setOpcode(Mips::DADDu);
Jim Grosbache9119e42015-05-13 18:37:00 +0000867 Inst.addOperand(MCOperand::createReg(Mips::GP));
868 Inst.addOperand(MCOperand::createReg(Mips::GP));
869 Inst.addOperand(MCOperand::createReg(RegNo));
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000870 getStreamer().EmitInstruction(Inst, STI);
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000871
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000872 forbidModuleDirective();
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000873}
874
Daniel Sandersf173dda2015-09-22 10:50:09 +0000875void MipsTargetELFStreamer::emitDirectiveCpreturn(unsigned SaveLocation,
876 bool SaveLocationIsRegister) {
877 // Only N32 and N64 emit anything for .cpreturn iff PIC is set.
878 if (!Pic || !(getABI().IsN32() || getABI().IsN64()))
879 return;
880
881 MCInst Inst;
882 // Either restore the old $gp from a register or on the stack
883 if (SaveLocationIsRegister) {
884 Inst.setOpcode(Mips::OR);
885 Inst.addOperand(MCOperand::createReg(Mips::GP));
886 Inst.addOperand(MCOperand::createReg(SaveLocation));
887 Inst.addOperand(MCOperand::createReg(Mips::ZERO));
888 } else {
889 Inst.setOpcode(Mips::LD);
890 Inst.addOperand(MCOperand::createReg(Mips::GP));
891 Inst.addOperand(MCOperand::createReg(Mips::SP));
892 Inst.addOperand(MCOperand::createImm(SaveLocation));
893 }
894 getStreamer().EmitInstruction(Inst, STI);
895
896 forbidModuleDirective();
897}
898
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000899void MipsTargetELFStreamer::emitMipsAbiFlags() {
900 MCAssembler &MCA = getStreamer().getAssembler();
901 MCContext &Context = MCA.getContext();
902 MCStreamer &OS = getStreamer();
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000903 MCSectionELF *Sec = Context.getELFSection(
Rafael Espindolaba31e272015-01-29 17:33:21 +0000904 ".MIPS.abiflags", ELF::SHT_MIPS_ABIFLAGS, ELF::SHF_ALLOC, 24, "");
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000905 MCA.registerSection(*Sec);
Rafael Espindola967d6a62015-05-21 21:02:35 +0000906 Sec->setAlignment(8);
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000907 OS.SwitchSection(Sec);
908
Daniel Sandersc7dbc632014-07-08 10:11:38 +0000909 OS << ABIFlagsSection;
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000910}