blob: fa89f46cbdbfb2089ec617e1c62f92f2a73722f8 [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 Sanderse2982ad2015-09-17 16:08:39 +000091void MipsTargetStreamer::emitDirectiveCpRestore(
92 SmallVector<MCInst, 3> &StoreInsts, int Offset) {
93 forbidModuleDirective();
94}
Rafael Espindola60890b82014-06-23 19:43:40 +000095void MipsTargetStreamer::emitDirectiveCpsetup(unsigned RegNo, int RegOrOffset,
96 const MCSymbol &Sym, bool IsReg) {
97}
Daniel Sandersf173dda2015-09-22 10:50:09 +000098void MipsTargetStreamer::emitDirectiveCpreturn(unsigned SaveLocation,
99 bool SaveLocationIsRegister) {}
Toma Tabacubfcbfd52015-06-23 12:34:19 +0000100
Toma Tabacua64e5402015-06-25 12:44:38 +0000101void MipsTargetStreamer::emitDirectiveModuleFP() {}
Toma Tabacubfcbfd52015-06-23 12:34:19 +0000102
Toma Tabacu3c499582015-06-25 10:56:57 +0000103void MipsTargetStreamer::emitDirectiveModuleOddSPReg() {
104 if (!ABIFlagsSection.OddSPReg && !ABIFlagsSection.Is32BitABI)
Daniel Sanders7e527422014-07-10 13:38:23 +0000105 report_fatal_error("+nooddspreg is only valid for O32");
106}
Toma Tabacu0f093132015-06-30 13:46:03 +0000107void MipsTargetStreamer::emitDirectiveModuleSoftFloat() {}
108void MipsTargetStreamer::emitDirectiveModuleHardFloat() {}
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +0000109void MipsTargetStreamer::emitDirectiveSetFp(
110 MipsABIFlagsSection::FpABIKind Value) {
111 forbidModuleDirective();
112}
Toma Tabacu32c72aa2015-06-30 09:36:50 +0000113void MipsTargetStreamer::emitDirectiveSetOddSPReg() { forbidModuleDirective(); }
114void MipsTargetStreamer::emitDirectiveSetNoOddSPReg() {
115 forbidModuleDirective();
116}
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000117
118MipsTargetAsmStreamer::MipsTargetAsmStreamer(MCStreamer &S,
119 formatted_raw_ostream &OS)
120 : MipsTargetStreamer(S), OS(OS) {}
Jack Carter6ef6cc52013-11-19 20:53:28 +0000121
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000122void MipsTargetAsmStreamer::emitDirectiveSetMicroMips() {
123 OS << "\t.set\tmicromips\n";
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000124 forbidModuleDirective();
Jack Carter6ef6cc52013-11-19 20:53:28 +0000125}
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000126
127void MipsTargetAsmStreamer::emitDirectiveSetNoMicroMips() {
128 OS << "\t.set\tnomicromips\n";
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000129 forbidModuleDirective();
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000130}
131
Rafael Espindola6633d572014-01-14 18:57:12 +0000132void MipsTargetAsmStreamer::emitDirectiveSetMips16() {
133 OS << "\t.set\tmips16\n";
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000134 forbidModuleDirective();
Rafael Espindola6633d572014-01-14 18:57:12 +0000135}
136
137void MipsTargetAsmStreamer::emitDirectiveSetNoMips16() {
138 OS << "\t.set\tnomips16\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000139 MipsTargetStreamer::emitDirectiveSetNoMips16();
Rafael Espindola6633d572014-01-14 18:57:12 +0000140}
141
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000142void MipsTargetAsmStreamer::emitDirectiveSetReorder() {
143 OS << "\t.set\treorder\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000144 MipsTargetStreamer::emitDirectiveSetReorder();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000145}
146
147void MipsTargetAsmStreamer::emitDirectiveSetNoReorder() {
148 OS << "\t.set\tnoreorder\n";
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000149 forbidModuleDirective();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000150}
151
152void MipsTargetAsmStreamer::emitDirectiveSetMacro() {
153 OS << "\t.set\tmacro\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000154 MipsTargetStreamer::emitDirectiveSetMacro();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000155}
156
157void MipsTargetAsmStreamer::emitDirectiveSetNoMacro() {
158 OS << "\t.set\tnomacro\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000159 MipsTargetStreamer::emitDirectiveSetNoMacro();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000160}
161
Daniel Sanders44934432014-08-07 12:03:36 +0000162void MipsTargetAsmStreamer::emitDirectiveSetMsa() {
163 OS << "\t.set\tmsa\n";
164 MipsTargetStreamer::emitDirectiveSetMsa();
165}
166
167void MipsTargetAsmStreamer::emitDirectiveSetNoMsa() {
168 OS << "\t.set\tnomsa\n";
169 MipsTargetStreamer::emitDirectiveSetNoMsa();
170}
171
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000172void MipsTargetAsmStreamer::emitDirectiveSetAt() {
173 OS << "\t.set\tat\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000174 MipsTargetStreamer::emitDirectiveSetAt();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000175}
176
Toma Tabacu16a74492015-02-13 10:30:57 +0000177void MipsTargetAsmStreamer::emitDirectiveSetAtWithArg(unsigned RegNo) {
178 OS << "\t.set\tat=$" << Twine(RegNo) << "\n";
179 MipsTargetStreamer::emitDirectiveSetAtWithArg(RegNo);
180}
181
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000182void MipsTargetAsmStreamer::emitDirectiveSetNoAt() {
183 OS << "\t.set\tnoat\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000184 MipsTargetStreamer::emitDirectiveSetNoAt();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000185}
186
187void MipsTargetAsmStreamer::emitDirectiveEnd(StringRef Name) {
188 OS << "\t.end\t" << Name << '\n';
189}
190
Rafael Espindola6633d572014-01-14 18:57:12 +0000191void MipsTargetAsmStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {
192 OS << "\t.ent\t" << Symbol.getName() << '\n';
193}
194
Jack Carter0cd3c192014-01-06 23:27:31 +0000195void MipsTargetAsmStreamer::emitDirectiveAbiCalls() { OS << "\t.abicalls\n"; }
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000196
197void MipsTargetAsmStreamer::emitDirectiveNaN2008() { OS << "\t.nan\t2008\n"; }
198
199void MipsTargetAsmStreamer::emitDirectiveNaNLegacy() {
200 OS << "\t.nan\tlegacy\n";
201}
202
Jack Carter0cd3c192014-01-06 23:27:31 +0000203void MipsTargetAsmStreamer::emitDirectiveOptionPic0() {
204 OS << "\t.option\tpic0\n";
205}
206
Matheus Almeidaf79b2812014-03-26 13:40:29 +0000207void MipsTargetAsmStreamer::emitDirectiveOptionPic2() {
208 OS << "\t.option\tpic2\n";
209}
210
Toma Tabacu9ca50962015-04-16 09:53:47 +0000211void MipsTargetAsmStreamer::emitDirectiveInsn() {
212 MipsTargetStreamer::emitDirectiveInsn();
213 OS << "\t.insn\n";
214}
215
Rafael Espindola054234f2014-01-27 03:53:56 +0000216void MipsTargetAsmStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
217 unsigned ReturnReg) {
218 OS << "\t.frame\t$"
219 << StringRef(MipsInstPrinter::getRegisterName(StackReg)).lower() << ","
220 << StackSize << ",$"
Rafael Espindola25fa2912014-01-27 04:33:11 +0000221 << StringRef(MipsInstPrinter::getRegisterName(ReturnReg)).lower() << '\n';
222}
223
Toma Tabacu85618b32014-08-19 14:22:52 +0000224void MipsTargetAsmStreamer::emitDirectiveSetArch(StringRef Arch) {
225 OS << "\t.set arch=" << Arch << "\n";
226 MipsTargetStreamer::emitDirectiveSetArch(Arch);
227}
228
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +0000229void MipsTargetAsmStreamer::emitDirectiveSetMips0() {
230 OS << "\t.set\tmips0\n";
231 MipsTargetStreamer::emitDirectiveSetMips0();
232}
Toma Tabacu26647792014-09-09 12:52:14 +0000233
Daniel Sandersf0df2212014-08-04 12:20:00 +0000234void MipsTargetAsmStreamer::emitDirectiveSetMips1() {
235 OS << "\t.set\tmips1\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000236 MipsTargetStreamer::emitDirectiveSetMips1();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000237}
238
239void MipsTargetAsmStreamer::emitDirectiveSetMips2() {
240 OS << "\t.set\tmips2\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000241 MipsTargetStreamer::emitDirectiveSetMips2();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000242}
243
244void MipsTargetAsmStreamer::emitDirectiveSetMips3() {
245 OS << "\t.set\tmips3\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000246 MipsTargetStreamer::emitDirectiveSetMips3();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000247}
248
249void MipsTargetAsmStreamer::emitDirectiveSetMips4() {
250 OS << "\t.set\tmips4\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000251 MipsTargetStreamer::emitDirectiveSetMips4();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000252}
253
254void MipsTargetAsmStreamer::emitDirectiveSetMips5() {
255 OS << "\t.set\tmips5\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000256 MipsTargetStreamer::emitDirectiveSetMips5();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000257}
258
259void MipsTargetAsmStreamer::emitDirectiveSetMips32() {
260 OS << "\t.set\tmips32\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000261 MipsTargetStreamer::emitDirectiveSetMips32();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000262}
263
Vladimir Medic615b26e2014-03-04 09:54:09 +0000264void MipsTargetAsmStreamer::emitDirectiveSetMips32R2() {
265 OS << "\t.set\tmips32r2\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000266 MipsTargetStreamer::emitDirectiveSetMips32R2();
Vladimir Medic615b26e2014-03-04 09:54:09 +0000267}
268
Daniel Sanders17793142015-02-18 16:24:50 +0000269void MipsTargetAsmStreamer::emitDirectiveSetMips32R3() {
270 OS << "\t.set\tmips32r3\n";
271 MipsTargetStreamer::emitDirectiveSetMips32R3();
272}
273
274void MipsTargetAsmStreamer::emitDirectiveSetMips32R5() {
275 OS << "\t.set\tmips32r5\n";
276 MipsTargetStreamer::emitDirectiveSetMips32R5();
277}
278
Daniel Sandersf0df2212014-08-04 12:20:00 +0000279void MipsTargetAsmStreamer::emitDirectiveSetMips32R6() {
280 OS << "\t.set\tmips32r6\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000281 MipsTargetStreamer::emitDirectiveSetMips32R6();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000282}
283
Matheus Almeida3b9c63d2014-03-26 15:14:32 +0000284void MipsTargetAsmStreamer::emitDirectiveSetMips64() {
285 OS << "\t.set\tmips64\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000286 MipsTargetStreamer::emitDirectiveSetMips64();
Matheus Almeida3b9c63d2014-03-26 15:14:32 +0000287}
288
Matheus Almeidaa2cd0092014-03-26 14:52:22 +0000289void MipsTargetAsmStreamer::emitDirectiveSetMips64R2() {
290 OS << "\t.set\tmips64r2\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000291 MipsTargetStreamer::emitDirectiveSetMips64R2();
Matheus Almeidaa2cd0092014-03-26 14:52:22 +0000292}
293
Daniel Sanders17793142015-02-18 16:24:50 +0000294void MipsTargetAsmStreamer::emitDirectiveSetMips64R3() {
295 OS << "\t.set\tmips64r3\n";
296 MipsTargetStreamer::emitDirectiveSetMips64R3();
297}
298
299void MipsTargetAsmStreamer::emitDirectiveSetMips64R5() {
300 OS << "\t.set\tmips64r5\n";
301 MipsTargetStreamer::emitDirectiveSetMips64R5();
302}
303
Daniel Sandersf0df2212014-08-04 12:20:00 +0000304void MipsTargetAsmStreamer::emitDirectiveSetMips64R6() {
305 OS << "\t.set\tmips64r6\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000306 MipsTargetStreamer::emitDirectiveSetMips64R6();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000307}
308
Vladimir Medic27c398e2014-03-05 11:05:09 +0000309void MipsTargetAsmStreamer::emitDirectiveSetDsp() {
310 OS << "\t.set\tdsp\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000311 MipsTargetStreamer::emitDirectiveSetDsp();
Vladimir Medic27c398e2014-03-05 11:05:09 +0000312}
Toma Tabacu9db22db2014-09-09 10:15:38 +0000313
Toma Tabacu351b2fe2014-09-17 09:01:54 +0000314void MipsTargetAsmStreamer::emitDirectiveSetNoDsp() {
315 OS << "\t.set\tnodsp\n";
316 MipsTargetStreamer::emitDirectiveSetNoDsp();
317}
318
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +0000319void MipsTargetAsmStreamer::emitDirectiveSetPop() {
320 OS << "\t.set\tpop\n";
321 MipsTargetStreamer::emitDirectiveSetPop();
322}
Toma Tabacu9db22db2014-09-09 10:15:38 +0000323
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +0000324void MipsTargetAsmStreamer::emitDirectiveSetPush() {
325 OS << "\t.set\tpush\n";
326 MipsTargetStreamer::emitDirectiveSetPush();
327}
Toma Tabacu9db22db2014-09-09 10:15:38 +0000328
Toma Tabacu29696502015-06-02 09:48:04 +0000329void MipsTargetAsmStreamer::emitDirectiveSetSoftFloat() {
330 OS << "\t.set\tsoftfloat\n";
331 MipsTargetStreamer::emitDirectiveSetSoftFloat();
332}
333
334void MipsTargetAsmStreamer::emitDirectiveSetHardFloat() {
335 OS << "\t.set\thardfloat\n";
336 MipsTargetStreamer::emitDirectiveSetHardFloat();
337}
338
Rafael Espindola25fa2912014-01-27 04:33:11 +0000339// Print a 32 bit hex number with all numbers.
340static void printHex32(unsigned Value, raw_ostream &OS) {
341 OS << "0x";
342 for (int i = 7; i >= 0; i--)
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000343 OS.write_hex((Value & (0xF << (i * 4))) >> (i * 4));
Rafael Espindola25fa2912014-01-27 04:33:11 +0000344}
345
346void MipsTargetAsmStreamer::emitMask(unsigned CPUBitmask,
347 int CPUTopSavedRegOff) {
348 OS << "\t.mask \t";
349 printHex32(CPUBitmask, OS);
350 OS << ',' << CPUTopSavedRegOff << '\n';
351}
352
353void MipsTargetAsmStreamer::emitFMask(unsigned FPUBitmask,
354 int FPUTopSavedRegOff) {
355 OS << "\t.fmask\t";
356 printHex32(FPUBitmask, OS);
357 OS << "," << FPUTopSavedRegOff << '\n';
Rafael Espindola054234f2014-01-27 03:53:56 +0000358}
359
Toma Tabacuc4c202a2014-10-01 14:53:19 +0000360void MipsTargetAsmStreamer::emitDirectiveCpLoad(unsigned RegNo) {
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000361 OS << "\t.cpload\t$"
362 << StringRef(MipsInstPrinter::getRegisterName(RegNo)).lower() << "\n";
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000363 forbidModuleDirective();
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000364}
365
Daniel Sanderse2982ad2015-09-17 16:08:39 +0000366void MipsTargetAsmStreamer::emitDirectiveCpRestore(
367 SmallVector<MCInst, 3> &StoreInsts, int Offset) {
368 MipsTargetStreamer::emitDirectiveCpRestore(StoreInsts, Offset);
369 OS << "\t.cprestore\t" << Offset << "\n";
370}
371
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000372void MipsTargetAsmStreamer::emitDirectiveCpsetup(unsigned RegNo,
373 int RegOrOffset,
374 const MCSymbol &Sym,
375 bool IsReg) {
376 OS << "\t.cpsetup\t$"
377 << StringRef(MipsInstPrinter::getRegisterName(RegNo)).lower() << ", ";
378
379 if (IsReg)
380 OS << "$"
381 << StringRef(MipsInstPrinter::getRegisterName(RegOrOffset)).lower();
382 else
383 OS << RegOrOffset;
384
385 OS << ", ";
386
Daniel Sanders5d796282015-09-21 09:26:55 +0000387 OS << Sym.getName();
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000388 forbidModuleDirective();
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000389}
390
Daniel Sandersf173dda2015-09-22 10:50:09 +0000391void MipsTargetAsmStreamer::emitDirectiveCpreturn(unsigned SaveLocation,
392 bool SaveLocationIsRegister) {
393 OS << "\t.cpreturn";
394 forbidModuleDirective();
395}
396
Toma Tabacua64e5402015-06-25 12:44:38 +0000397void MipsTargetAsmStreamer::emitDirectiveModuleFP() {
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000398 OS << "\t.module\tfp=";
Toma Tabacua64e5402015-06-25 12:44:38 +0000399 OS << ABIFlagsSection.getFpABIString(ABIFlagsSection.getFpABI()) << "\n";
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000400}
401
Daniel Sanders7e527422014-07-10 13:38:23 +0000402void MipsTargetAsmStreamer::emitDirectiveSetFp(
403 MipsABIFlagsSection::FpABIKind Value) {
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +0000404 MipsTargetStreamer::emitDirectiveSetFp(Value);
405
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000406 OS << "\t.set\tfp=";
Daniel Sanders7e527422014-07-10 13:38:23 +0000407 OS << ABIFlagsSection.getFpABIString(Value) << "\n";
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000408}
409
Toma Tabacu3c499582015-06-25 10:56:57 +0000410void MipsTargetAsmStreamer::emitDirectiveModuleOddSPReg() {
411 MipsTargetStreamer::emitDirectiveModuleOddSPReg();
Daniel Sanders7e527422014-07-10 13:38:23 +0000412
Toma Tabacu3c499582015-06-25 10:56:57 +0000413 OS << "\t.module\t" << (ABIFlagsSection.OddSPReg ? "" : "no") << "oddspreg\n";
Daniel Sanders7e527422014-07-10 13:38:23 +0000414}
415
Toma Tabacu32c72aa2015-06-30 09:36:50 +0000416void MipsTargetAsmStreamer::emitDirectiveSetOddSPReg() {
417 MipsTargetStreamer::emitDirectiveSetOddSPReg();
418 OS << "\t.set\toddspreg\n";
419}
420
421void MipsTargetAsmStreamer::emitDirectiveSetNoOddSPReg() {
422 MipsTargetStreamer::emitDirectiveSetNoOddSPReg();
423 OS << "\t.set\tnooddspreg\n";
424}
425
Toma Tabacu0f093132015-06-30 13:46:03 +0000426void MipsTargetAsmStreamer::emitDirectiveModuleSoftFloat() {
427 OS << "\t.module\tsoftfloat\n";
428}
429
430void MipsTargetAsmStreamer::emitDirectiveModuleHardFloat() {
431 OS << "\t.module\thardfloat\n";
432}
433
Jack Carter0cd3c192014-01-06 23:27:31 +0000434// This part is for ELF object output.
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000435MipsTargetELFStreamer::MipsTargetELFStreamer(MCStreamer &S,
436 const MCSubtargetInfo &STI)
Rafael Espindola972e71a2014-01-31 23:10:26 +0000437 : MipsTargetStreamer(S), MicroMipsEnabled(false), STI(STI) {
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000438 MCAssembler &MCA = getStreamer().getAssembler();
Simon Atanasyanc99ce682015-03-24 12:24:56 +0000439 Pic = MCA.getContext().getObjectFileInfo()->getRelocM() == Reloc::PIC_;
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000440
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000441 const FeatureBitset &Features = STI.getFeatureBits();
Eric Christophera5762812015-01-26 17:33:46 +0000442
443 // Set the header flags that we can in the constructor.
444 // FIXME: This is a fairly terrible hack. We set the rest
445 // of these in the destructor. The problem here is two-fold:
446 //
447 // a: Some of the eflags can be set/reset by directives.
448 // b: There aren't any usage paths that initialize the ABI
449 // pointer until after we initialize either an assembler
450 // or the target machine.
451 // We can fix this by making the target streamer construct
452 // the ABI, but this is fraught with wide ranging dependency
453 // issues as well.
454 unsigned EFlags = MCA.getELFHeaderEFlags();
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000455
456 // Architecture
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000457 if (Features[Mips::FeatureMips64r6])
Daniel Sanders950f48d2014-07-04 15:21:53 +0000458 EFlags |= ELF::EF_MIPS_ARCH_64R6;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000459 else if (Features[Mips::FeatureMips64r2] ||
460 Features[Mips::FeatureMips64r3] ||
461 Features[Mips::FeatureMips64r5])
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000462 EFlags |= ELF::EF_MIPS_ARCH_64R2;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000463 else if (Features[Mips::FeatureMips64])
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000464 EFlags |= ELF::EF_MIPS_ARCH_64;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000465 else if (Features[Mips::FeatureMips5])
Daniel Sanders950f48d2014-07-04 15:21:53 +0000466 EFlags |= ELF::EF_MIPS_ARCH_5;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000467 else if (Features[Mips::FeatureMips4])
Daniel Sandersf7b32292014-04-03 12:13:36 +0000468 EFlags |= ELF::EF_MIPS_ARCH_4;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000469 else if (Features[Mips::FeatureMips3])
Daniel Sanders950f48d2014-07-04 15:21:53 +0000470 EFlags |= ELF::EF_MIPS_ARCH_3;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000471 else if (Features[Mips::FeatureMips32r6])
Daniel Sanders950f48d2014-07-04 15:21:53 +0000472 EFlags |= ELF::EF_MIPS_ARCH_32R6;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000473 else if (Features[Mips::FeatureMips32r2] ||
474 Features[Mips::FeatureMips32r3] ||
475 Features[Mips::FeatureMips32r5])
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000476 EFlags |= ELF::EF_MIPS_ARCH_32R2;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000477 else if (Features[Mips::FeatureMips32])
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000478 EFlags |= ELF::EF_MIPS_ARCH_32;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000479 else if (Features[Mips::FeatureMips2])
Daniel Sanders950f48d2014-07-04 15:21:53 +0000480 EFlags |= ELF::EF_MIPS_ARCH_2;
481 else
482 EFlags |= ELF::EF_MIPS_ARCH_1;
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000483
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000484 // Other options.
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000485 if (Features[Mips::FeatureNaN2008])
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000486 EFlags |= ELF::EF_MIPS_NAN2008;
487
Daniel Sanders16ec6c12014-07-17 09:52:56 +0000488 // -mabicalls and -mplt are not implemented but we should act as if they were
489 // given.
490 EFlags |= ELF::EF_MIPS_CPIC;
Daniel Sanders16ec6c12014-07-17 09:52:56 +0000491
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000492 MCA.setELFHeaderEFlags(EFlags);
493}
Jack Carter86ac5c12013-11-18 23:55:27 +0000494
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000495void MipsTargetELFStreamer::emitLabel(MCSymbol *S) {
496 auto *Symbol = cast<MCSymbolELF>(S);
Rafael Espindola26e917c2014-01-15 03:07:12 +0000497 if (!isMicroMipsEnabled())
498 return;
Rafael Espindolac73aed12015-06-03 19:03:11 +0000499 getStreamer().getAssembler().registerSymbol(*Symbol);
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000500 uint8_t Type = Symbol->getType();
Rafael Espindola26e917c2014-01-15 03:07:12 +0000501 if (Type != ELF::STT_FUNC)
502 return;
503
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000504 Symbol->setOther(ELF::STO_MIPS_MICROMIPS);
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000505}
506
Rafael Espindola972e71a2014-01-31 23:10:26 +0000507void MipsTargetELFStreamer::finish() {
508 MCAssembler &MCA = getStreamer().getAssembler();
Daniel Sanders68c37472014-07-21 13:30:55 +0000509 const MCObjectFileInfo &OFI = *MCA.getContext().getObjectFileInfo();
Rafael Espindola972e71a2014-01-31 23:10:26 +0000510
Daniel Sanders41ffa5d2014-07-14 15:05:51 +0000511 // .bss, .text and .data are always at least 16-byte aligned.
Rafael Espindola967d6a62015-05-21 21:02:35 +0000512 MCSection &TextSection = *OFI.getTextSection();
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000513 MCA.registerSection(TextSection);
Rafael Espindola967d6a62015-05-21 21:02:35 +0000514 MCSection &DataSection = *OFI.getDataSection();
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000515 MCA.registerSection(DataSection);
Rafael Espindola967d6a62015-05-21 21:02:35 +0000516 MCSection &BSSSection = *OFI.getBSSSection();
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000517 MCA.registerSection(BSSSection);
Daniel Sanders41ffa5d2014-07-14 15:05:51 +0000518
Rafael Espindola967d6a62015-05-21 21:02:35 +0000519 TextSection.setAlignment(std::max(16u, TextSection.getAlignment()));
520 DataSection.setAlignment(std::max(16u, DataSection.getAlignment()));
521 BSSSection.setAlignment(std::max(16u, BSSSection.getAlignment()));
Daniel Sanders41ffa5d2014-07-14 15:05:51 +0000522
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000523 const FeatureBitset &Features = STI.getFeatureBits();
Eric Christophera5762812015-01-26 17:33:46 +0000524
525 // Update e_header flags. See the FIXME and comment above in
526 // the constructor for a full rundown on this.
527 unsigned EFlags = MCA.getELFHeaderEFlags();
528
529 // ABI
530 // N64 does not require any ABI bits.
531 if (getABI().IsO32())
532 EFlags |= ELF::EF_MIPS_ABI_O32;
533 else if (getABI().IsN32())
534 EFlags |= ELF::EF_MIPS_ABI2;
535
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000536 if (Features[Mips::FeatureGP64Bit]) {
Eric Christophera5762812015-01-26 17:33:46 +0000537 if (getABI().IsO32())
538 EFlags |= ELF::EF_MIPS_32BITMODE; /* Compatibility Mode */
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000539 } else if (Features[Mips::FeatureMips64r2] || Features[Mips::FeatureMips64])
Eric Christophera5762812015-01-26 17:33:46 +0000540 EFlags |= ELF::EF_MIPS_32BITMODE;
541
542 // If we've set the cpic eflag and we're n64, go ahead and set the pic
543 // one as well.
544 if (EFlags & ELF::EF_MIPS_CPIC && getABI().IsN64())
545 EFlags |= ELF::EF_MIPS_PIC;
546
547 MCA.setELFHeaderEFlags(EFlags);
548
Daniel Sanders68c37472014-07-21 13:30:55 +0000549 // Emit all the option records.
550 // At the moment we are only emitting .Mips.options (ODK_REGINFO) and
551 // .reginfo.
552 MipsELFStreamer &MEF = static_cast<MipsELFStreamer &>(Streamer);
553 MEF.EmitMipsOptionRecords();
Rafael Espindola972e71a2014-01-31 23:10:26 +0000554
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000555 emitMipsAbiFlags();
Rafael Espindola972e71a2014-01-31 23:10:26 +0000556}
557
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000558void MipsTargetELFStreamer::emitAssignment(MCSymbol *S, const MCExpr *Value) {
559 auto *Symbol = cast<MCSymbolELF>(S);
Zoran Jovanovic28221d82014-03-20 09:44:49 +0000560 // If on rhs is micromips symbol then mark Symbol as microMips.
561 if (Value->getKind() != MCExpr::SymbolRef)
562 return;
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000563 const auto &RhsSym = cast<MCSymbolELF>(
564 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol());
Toma Tabacu2cc44f52015-04-16 13:37:32 +0000565
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000566 if (!(RhsSym.getOther() & ELF::STO_MIPS_MICROMIPS))
Zoran Jovanovic28221d82014-03-20 09:44:49 +0000567 return;
568
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000569 Symbol->setOther(ELF::STO_MIPS_MICROMIPS);
Zoran Jovanovic28221d82014-03-20 09:44:49 +0000570}
571
Jack Carter86ac5c12013-11-18 23:55:27 +0000572MCELFStreamer &MipsTargetELFStreamer::getStreamer() {
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000573 return static_cast<MCELFStreamer &>(Streamer);
Jack Carter86ac5c12013-11-18 23:55:27 +0000574}
575
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000576void MipsTargetELFStreamer::emitDirectiveSetMicroMips() {
577 MicroMipsEnabled = true;
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000578
579 MCAssembler &MCA = getStreamer().getAssembler();
580 unsigned Flags = MCA.getELFHeaderEFlags();
581 Flags |= ELF::EF_MIPS_MICROMIPS;
582 MCA.setELFHeaderEFlags(Flags);
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000583 forbidModuleDirective();
Jack Carter86ac5c12013-11-18 23:55:27 +0000584}
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000585
586void MipsTargetELFStreamer::emitDirectiveSetNoMicroMips() {
587 MicroMipsEnabled = false;
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000588 forbidModuleDirective();
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000589}
590
Rafael Espindola6633d572014-01-14 18:57:12 +0000591void MipsTargetELFStreamer::emitDirectiveSetMips16() {
Rafael Espindolae7583752014-01-24 16:13:20 +0000592 MCAssembler &MCA = getStreamer().getAssembler();
593 unsigned Flags = MCA.getELFHeaderEFlags();
594 Flags |= ELF::EF_MIPS_ARCH_ASE_M16;
595 MCA.setELFHeaderEFlags(Flags);
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000596 forbidModuleDirective();
Rafael Espindola6633d572014-01-14 18:57:12 +0000597}
598
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000599void MipsTargetELFStreamer::emitDirectiveSetNoReorder() {
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000600 MCAssembler &MCA = getStreamer().getAssembler();
601 unsigned Flags = MCA.getELFHeaderEFlags();
602 Flags |= ELF::EF_MIPS_NOREORDER;
603 MCA.setELFHeaderEFlags(Flags);
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000604 forbidModuleDirective();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000605}
606
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000607void MipsTargetELFStreamer::emitDirectiveEnd(StringRef Name) {
Daniel Sandersd97a6342014-08-13 10:07:34 +0000608 MCAssembler &MCA = getStreamer().getAssembler();
609 MCContext &Context = MCA.getContext();
610 MCStreamer &OS = getStreamer();
611
Scott Egerton219fae92016-02-17 11:15:16 +0000612 MCSectionELF *Sec = Context.getELFSection(".pdr", ELF::SHT_PROGBITS, 0);
Daniel Sandersd97a6342014-08-13 10:07:34 +0000613
Daniel Sanders2b561332015-11-23 16:08:03 +0000614 MCSymbol *Sym = Context.getOrCreateSymbol(Name);
Daniel Sandersd97a6342014-08-13 10:07:34 +0000615 const MCSymbolRefExpr *ExprRef =
Daniel Sanders2b561332015-11-23 16:08:03 +0000616 MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, Context);
Daniel Sandersd97a6342014-08-13 10:07:34 +0000617
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000618 MCA.registerSection(*Sec);
Rafael Espindola967d6a62015-05-21 21:02:35 +0000619 Sec->setAlignment(4);
Daniel Sandersd97a6342014-08-13 10:07:34 +0000620
621 OS.PushSection();
622
623 OS.SwitchSection(Sec);
624
625 OS.EmitValueImpl(ExprRef, 4);
626
627 OS.EmitIntValue(GPRInfoSet ? GPRBitMask : 0, 4); // reg_mask
628 OS.EmitIntValue(GPRInfoSet ? GPROffset : 0, 4); // reg_offset
629
630 OS.EmitIntValue(FPRInfoSet ? FPRBitMask : 0, 4); // fpreg_mask
631 OS.EmitIntValue(FPRInfoSet ? FPROffset : 0, 4); // fpreg_offset
632
633 OS.EmitIntValue(FrameInfoSet ? FrameOffset : 0, 4); // frame_offset
634 OS.EmitIntValue(FrameInfoSet ? FrameReg : 0, 4); // frame_reg
635 OS.EmitIntValue(FrameInfoSet ? ReturnReg : 0, 4); // return_reg
636
637 // The .end directive marks the end of a procedure. Invalidate
638 // the information gathered up until this point.
639 GPRInfoSet = FPRInfoSet = FrameInfoSet = false;
640
641 OS.PopSection();
Daniel Sanders2b561332015-11-23 16:08:03 +0000642
643 // .end also implicitly sets the size.
644 MCSymbol *CurPCSym = Context.createTempSymbol();
645 OS.EmitLabel(CurPCSym);
646 const MCExpr *Size = MCBinaryExpr::createSub(
647 MCSymbolRefExpr::create(CurPCSym, MCSymbolRefExpr::VK_None, Context),
648 ExprRef, Context);
649 int64_t AbsSize;
650 if (!Size->evaluateAsAbsolute(AbsSize, MCA))
651 llvm_unreachable("Function size must be evaluatable as absolute");
652 Size = MCConstantExpr::create(AbsSize, Context);
653 static_cast<MCSymbolELF *>(Sym)->setSize(Size);
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000654}
655
Rafael Espindola6633d572014-01-14 18:57:12 +0000656void MipsTargetELFStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {
Daniel Sandersd97a6342014-08-13 10:07:34 +0000657 GPRInfoSet = FPRInfoSet = FrameInfoSet = false;
Daniel Sanders2b561332015-11-23 16:08:03 +0000658
659 // .ent also acts like an implicit '.type symbol, STT_FUNC'
660 static_cast<const MCSymbolELF &>(Symbol).setType(ELF::STT_FUNC);
Rafael Espindola6633d572014-01-14 18:57:12 +0000661}
662
Jack Carter0cd3c192014-01-06 23:27:31 +0000663void MipsTargetELFStreamer::emitDirectiveAbiCalls() {
664 MCAssembler &MCA = getStreamer().getAssembler();
665 unsigned Flags = MCA.getELFHeaderEFlags();
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000666 Flags |= ELF::EF_MIPS_CPIC | ELF::EF_MIPS_PIC;
Jack Carter0cd3c192014-01-06 23:27:31 +0000667 MCA.setELFHeaderEFlags(Flags);
668}
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000669
670void MipsTargetELFStreamer::emitDirectiveNaN2008() {
671 MCAssembler &MCA = getStreamer().getAssembler();
672 unsigned Flags = MCA.getELFHeaderEFlags();
673 Flags |= ELF::EF_MIPS_NAN2008;
674 MCA.setELFHeaderEFlags(Flags);
675}
676
677void MipsTargetELFStreamer::emitDirectiveNaNLegacy() {
678 MCAssembler &MCA = getStreamer().getAssembler();
679 unsigned Flags = MCA.getELFHeaderEFlags();
680 Flags &= ~ELF::EF_MIPS_NAN2008;
681 MCA.setELFHeaderEFlags(Flags);
682}
683
Jack Carter0cd3c192014-01-06 23:27:31 +0000684void MipsTargetELFStreamer::emitDirectiveOptionPic0() {
685 MCAssembler &MCA = getStreamer().getAssembler();
686 unsigned Flags = MCA.getELFHeaderEFlags();
Matheus Almeidaf79b2812014-03-26 13:40:29 +0000687 // This option overrides other PIC options like -KPIC.
688 Pic = false;
Jack Carter0cd3c192014-01-06 23:27:31 +0000689 Flags &= ~ELF::EF_MIPS_PIC;
690 MCA.setELFHeaderEFlags(Flags);
691}
Rafael Espindola054234f2014-01-27 03:53:56 +0000692
Matheus Almeidaf79b2812014-03-26 13:40:29 +0000693void MipsTargetELFStreamer::emitDirectiveOptionPic2() {
694 MCAssembler &MCA = getStreamer().getAssembler();
695 unsigned Flags = MCA.getELFHeaderEFlags();
696 Pic = true;
697 // NOTE: We are following the GAS behaviour here which means the directive
698 // 'pic2' also sets the CPIC bit in the ELF header. This is different from
699 // what is stated in the SYSV ABI which consider the bits EF_MIPS_PIC and
700 // EF_MIPS_CPIC to be mutually exclusive.
701 Flags |= ELF::EF_MIPS_PIC | ELF::EF_MIPS_CPIC;
702 MCA.setELFHeaderEFlags(Flags);
703}
704
Toma Tabacu9ca50962015-04-16 09:53:47 +0000705void MipsTargetELFStreamer::emitDirectiveInsn() {
706 MipsTargetStreamer::emitDirectiveInsn();
707 MipsELFStreamer &MEF = static_cast<MipsELFStreamer &>(Streamer);
708 MEF.createPendingLabelRelocs();
709}
710
Rafael Espindola054234f2014-01-27 03:53:56 +0000711void MipsTargetELFStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
Daniel Sandersd97a6342014-08-13 10:07:34 +0000712 unsigned ReturnReg_) {
713 MCContext &Context = getStreamer().getAssembler().getContext();
714 const MCRegisterInfo *RegInfo = Context.getRegisterInfo();
715
716 FrameInfoSet = true;
717 FrameReg = RegInfo->getEncodingValue(StackReg);
718 FrameOffset = StackSize;
719 ReturnReg = RegInfo->getEncodingValue(ReturnReg_);
Rafael Espindola054234f2014-01-27 03:53:56 +0000720}
Rafael Espindola25fa2912014-01-27 04:33:11 +0000721
722void MipsTargetELFStreamer::emitMask(unsigned CPUBitmask,
723 int CPUTopSavedRegOff) {
Daniel Sandersd97a6342014-08-13 10:07:34 +0000724 GPRInfoSet = true;
725 GPRBitMask = CPUBitmask;
726 GPROffset = CPUTopSavedRegOff;
Rafael Espindola25fa2912014-01-27 04:33:11 +0000727}
728
729void MipsTargetELFStreamer::emitFMask(unsigned FPUBitmask,
730 int FPUTopSavedRegOff) {
Daniel Sandersd97a6342014-08-13 10:07:34 +0000731 FPRInfoSet = true;
732 FPRBitMask = FPUBitmask;
733 FPROffset = FPUTopSavedRegOff;
Rafael Espindola25fa2912014-01-27 04:33:11 +0000734}
Vladimir Medic615b26e2014-03-04 09:54:09 +0000735
Toma Tabacuc4c202a2014-10-01 14:53:19 +0000736void MipsTargetELFStreamer::emitDirectiveCpLoad(unsigned RegNo) {
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000737 // .cpload $reg
738 // This directive expands to:
739 // lui $gp, %hi(_gp_disp)
740 // addui $gp, $gp, %lo(_gp_disp)
741 // addu $gp, $gp, $reg
742 // when support for position independent code is enabled.
Eric Christophera5762812015-01-26 17:33:46 +0000743 if (!Pic || (getABI().IsN32() || getABI().IsN64()))
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000744 return;
745
746 // There's a GNU extension controlled by -mno-shared that allows
747 // locally-binding symbols to be accessed using absolute addresses.
748 // This is currently not supported. When supported -mno-shared makes
749 // .cpload expand to:
750 // lui $gp, %hi(__gnu_local_gp)
751 // addiu $gp, $gp, %lo(__gnu_local_gp)
752
753 StringRef SymName("_gp_disp");
754 MCAssembler &MCA = getStreamer().getAssembler();
Jim Grosbach6f482002015-05-18 18:43:14 +0000755 MCSymbol *GP_Disp = MCA.getContext().getOrCreateSymbol(SymName);
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000756 MCA.registerSymbol(*GP_Disp);
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000757
758 MCInst TmpInst;
759 TmpInst.setOpcode(Mips::LUi);
Jim Grosbache9119e42015-05-13 18:37:00 +0000760 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
Jim Grosbach13760bd2015-05-30 01:25:56 +0000761 const MCSymbolRefExpr *HiSym = MCSymbolRefExpr::create(
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000762 "_gp_disp", MCSymbolRefExpr::VK_Mips_ABS_HI, MCA.getContext());
Jim Grosbache9119e42015-05-13 18:37:00 +0000763 TmpInst.addOperand(MCOperand::createExpr(HiSym));
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000764 getStreamer().EmitInstruction(TmpInst, STI);
765
766 TmpInst.clear();
767
768 TmpInst.setOpcode(Mips::ADDiu);
Jim Grosbache9119e42015-05-13 18:37:00 +0000769 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
770 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
Jim Grosbach13760bd2015-05-30 01:25:56 +0000771 const MCSymbolRefExpr *LoSym = MCSymbolRefExpr::create(
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000772 "_gp_disp", MCSymbolRefExpr::VK_Mips_ABS_LO, MCA.getContext());
Jim Grosbache9119e42015-05-13 18:37:00 +0000773 TmpInst.addOperand(MCOperand::createExpr(LoSym));
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000774 getStreamer().EmitInstruction(TmpInst, STI);
775
776 TmpInst.clear();
777
778 TmpInst.setOpcode(Mips::ADDu);
Jim Grosbache9119e42015-05-13 18:37:00 +0000779 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
780 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
781 TmpInst.addOperand(MCOperand::createReg(RegNo));
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000782 getStreamer().EmitInstruction(TmpInst, STI);
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000783
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000784 forbidModuleDirective();
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000785}
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000786
Daniel Sanderse2982ad2015-09-17 16:08:39 +0000787void MipsTargetELFStreamer::emitDirectiveCpRestore(
788 SmallVector<MCInst, 3> &StoreInsts, int Offset) {
789 MipsTargetStreamer::emitDirectiveCpRestore(StoreInsts, Offset);
790 // .cprestore offset
791 // When PIC mode is enabled and the O32 ABI is used, this directive expands
792 // to:
793 // sw $gp, offset($sp)
794 // and adds a corresponding LW after every JAL.
795
796 // Note that .cprestore is ignored if used with the N32 and N64 ABIs or if it
797 // is used in non-PIC mode.
798 if (!Pic || (getABI().IsN32() || getABI().IsN64()))
799 return;
800
801 for (const MCInst &Inst : StoreInsts)
802 getStreamer().EmitInstruction(Inst, STI);
803}
804
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000805void MipsTargetELFStreamer::emitDirectiveCpsetup(unsigned RegNo,
806 int RegOrOffset,
807 const MCSymbol &Sym,
808 bool IsReg) {
809 // Only N32 and N64 emit anything for .cpsetup iff PIC is set.
Eric Christophera5762812015-01-26 17:33:46 +0000810 if (!Pic || !(getABI().IsN32() || getABI().IsN64()))
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000811 return;
812
813 MCAssembler &MCA = getStreamer().getAssembler();
814 MCInst Inst;
815
816 // Either store the old $gp in a register or on the stack
817 if (IsReg) {
818 // move $save, $gpreg
Vasileios Kalintiris1c78ca62015-08-11 08:56:25 +0000819 Inst.setOpcode(Mips::OR64);
Jim Grosbache9119e42015-05-13 18:37:00 +0000820 Inst.addOperand(MCOperand::createReg(RegOrOffset));
821 Inst.addOperand(MCOperand::createReg(Mips::GP));
822 Inst.addOperand(MCOperand::createReg(Mips::ZERO));
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000823 } else {
824 // sd $gpreg, offset($sp)
825 Inst.setOpcode(Mips::SD);
Jim Grosbache9119e42015-05-13 18:37:00 +0000826 Inst.addOperand(MCOperand::createReg(Mips::GP));
827 Inst.addOperand(MCOperand::createReg(Mips::SP));
828 Inst.addOperand(MCOperand::createImm(RegOrOffset));
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000829 }
830 getStreamer().EmitInstruction(Inst, STI);
831 Inst.clear();
832
Jim Grosbach13760bd2015-05-30 01:25:56 +0000833 const MCSymbolRefExpr *HiExpr = MCSymbolRefExpr::create(
Toma Tabacu8874eac2015-02-18 13:46:53 +0000834 &Sym, MCSymbolRefExpr::VK_Mips_GPOFF_HI, MCA.getContext());
Jim Grosbach13760bd2015-05-30 01:25:56 +0000835 const MCSymbolRefExpr *LoExpr = MCSymbolRefExpr::create(
Toma Tabacu8874eac2015-02-18 13:46:53 +0000836 &Sym, MCSymbolRefExpr::VK_Mips_GPOFF_LO, MCA.getContext());
837
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000838 // lui $gp, %hi(%neg(%gp_rel(funcSym)))
839 Inst.setOpcode(Mips::LUi);
Jim Grosbache9119e42015-05-13 18:37:00 +0000840 Inst.addOperand(MCOperand::createReg(Mips::GP));
841 Inst.addOperand(MCOperand::createExpr(HiExpr));
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000842 getStreamer().EmitInstruction(Inst, STI);
843 Inst.clear();
844
845 // addiu $gp, $gp, %lo(%neg(%gp_rel(funcSym)))
846 Inst.setOpcode(Mips::ADDiu);
Jim Grosbache9119e42015-05-13 18:37:00 +0000847 Inst.addOperand(MCOperand::createReg(Mips::GP));
848 Inst.addOperand(MCOperand::createReg(Mips::GP));
849 Inst.addOperand(MCOperand::createExpr(LoExpr));
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000850 getStreamer().EmitInstruction(Inst, STI);
851 Inst.clear();
852
853 // daddu $gp, $gp, $funcreg
854 Inst.setOpcode(Mips::DADDu);
Jim Grosbache9119e42015-05-13 18:37:00 +0000855 Inst.addOperand(MCOperand::createReg(Mips::GP));
856 Inst.addOperand(MCOperand::createReg(Mips::GP));
857 Inst.addOperand(MCOperand::createReg(RegNo));
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000858 getStreamer().EmitInstruction(Inst, STI);
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000859
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000860 forbidModuleDirective();
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000861}
862
Daniel Sandersf173dda2015-09-22 10:50:09 +0000863void MipsTargetELFStreamer::emitDirectiveCpreturn(unsigned SaveLocation,
864 bool SaveLocationIsRegister) {
865 // Only N32 and N64 emit anything for .cpreturn iff PIC is set.
866 if (!Pic || !(getABI().IsN32() || getABI().IsN64()))
867 return;
868
869 MCInst Inst;
870 // Either restore the old $gp from a register or on the stack
871 if (SaveLocationIsRegister) {
872 Inst.setOpcode(Mips::OR);
873 Inst.addOperand(MCOperand::createReg(Mips::GP));
874 Inst.addOperand(MCOperand::createReg(SaveLocation));
875 Inst.addOperand(MCOperand::createReg(Mips::ZERO));
876 } else {
877 Inst.setOpcode(Mips::LD);
878 Inst.addOperand(MCOperand::createReg(Mips::GP));
879 Inst.addOperand(MCOperand::createReg(Mips::SP));
880 Inst.addOperand(MCOperand::createImm(SaveLocation));
881 }
882 getStreamer().EmitInstruction(Inst, STI);
883
884 forbidModuleDirective();
885}
886
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000887void MipsTargetELFStreamer::emitMipsAbiFlags() {
888 MCAssembler &MCA = getStreamer().getAssembler();
889 MCContext &Context = MCA.getContext();
890 MCStreamer &OS = getStreamer();
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000891 MCSectionELF *Sec = Context.getELFSection(
Rafael Espindolaba31e272015-01-29 17:33:21 +0000892 ".MIPS.abiflags", ELF::SHT_MIPS_ABIFLAGS, ELF::SHF_ALLOC, 24, "");
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000893 MCA.registerSection(*Sec);
Rafael Espindola967d6a62015-05-21 21:02:35 +0000894 Sec->setAlignment(8);
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000895 OS.SwitchSection(Sec);
896
Daniel Sandersc7dbc632014-07-08 10:11:38 +0000897 OS << ABIFlagsSection;
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000898}