blob: 097c24554a272a172b98bb2f98af6f80e22d0d94 [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
Daniel Sandersa736b372016-04-29 13:33:12 +0000117void MipsTargetStreamer::emitR(unsigned Opcode, unsigned Reg0, SMLoc IDLoc,
118 const MCSubtargetInfo *STI) {
119 MCInst TmpInst;
120 TmpInst.setOpcode(Opcode);
121 TmpInst.addOperand(MCOperand::createReg(Reg0));
122 TmpInst.setLoc(IDLoc);
123 getStreamer().EmitInstruction(TmpInst, *STI);
124}
125
126void MipsTargetStreamer::emitRX(unsigned Opcode, unsigned Reg0, MCOperand Op1,
127 SMLoc IDLoc, const MCSubtargetInfo *STI) {
128 MCInst TmpInst;
129 TmpInst.setOpcode(Opcode);
130 TmpInst.addOperand(MCOperand::createReg(Reg0));
131 TmpInst.addOperand(Op1);
132 TmpInst.setLoc(IDLoc);
133 getStreamer().EmitInstruction(TmpInst, *STI);
134}
135
136void MipsTargetStreamer::emitRI(unsigned Opcode, unsigned Reg0, int32_t Imm,
137 SMLoc IDLoc, const MCSubtargetInfo *STI) {
138 emitRX(Opcode, Reg0, MCOperand::createImm(Imm), IDLoc, STI);
139}
140
141void MipsTargetStreamer::emitRR(unsigned Opcode, unsigned Reg0, unsigned Reg1,
142 SMLoc IDLoc, const MCSubtargetInfo *STI) {
143 emitRX(Opcode, Reg0, MCOperand::createReg(Reg1), IDLoc, STI);
144}
145
146void MipsTargetStreamer::emitII(unsigned Opcode, int16_t Imm1, int16_t Imm2,
147 SMLoc IDLoc, const MCSubtargetInfo *STI) {
148 MCInst TmpInst;
149 TmpInst.setOpcode(Opcode);
150 TmpInst.addOperand(MCOperand::createImm(Imm1));
151 TmpInst.addOperand(MCOperand::createImm(Imm2));
152 TmpInst.setLoc(IDLoc);
153 getStreamer().EmitInstruction(TmpInst, *STI);
154}
155
156void MipsTargetStreamer::emitRRX(unsigned Opcode, unsigned Reg0, unsigned Reg1,
157 MCOperand Op2, SMLoc IDLoc,
158 const MCSubtargetInfo *STI) {
159 MCInst TmpInst;
160 TmpInst.setOpcode(Opcode);
161 TmpInst.addOperand(MCOperand::createReg(Reg0));
162 TmpInst.addOperand(MCOperand::createReg(Reg1));
163 TmpInst.addOperand(Op2);
164 TmpInst.setLoc(IDLoc);
165 getStreamer().EmitInstruction(TmpInst, *STI);
166}
167
168void MipsTargetStreamer::emitRRR(unsigned Opcode, unsigned Reg0, unsigned Reg1,
169 unsigned Reg2, SMLoc IDLoc,
170 const MCSubtargetInfo *STI) {
171 emitRRX(Opcode, Reg0, Reg1, MCOperand::createReg(Reg2), IDLoc, STI);
172}
173
174void MipsTargetStreamer::emitRRI(unsigned Opcode, unsigned Reg0, unsigned Reg1,
175 int16_t Imm, SMLoc IDLoc,
176 const MCSubtargetInfo *STI) {
177 emitRRX(Opcode, Reg0, Reg1, MCOperand::createImm(Imm), IDLoc, STI);
178}
179
180void MipsTargetStreamer::emitAddu(unsigned DstReg, unsigned SrcReg,
181 unsigned TrgReg, bool Is64Bit,
182 const MCSubtargetInfo *STI) {
183 emitRRR(Is64Bit ? Mips::DADDu : Mips::ADDu, DstReg, SrcReg, TrgReg, SMLoc(),
184 STI);
185}
186
187void MipsTargetStreamer::emitDSLL(unsigned DstReg, unsigned SrcReg,
188 int16_t ShiftAmount, SMLoc IDLoc,
189 const MCSubtargetInfo *STI) {
190 if (ShiftAmount >= 32) {
191 emitRRI(Mips::DSLL32, DstReg, SrcReg, ShiftAmount - 32, IDLoc, STI);
192 return;
193 }
194
195 emitRRI(Mips::DSLL, DstReg, SrcReg, ShiftAmount, IDLoc, STI);
196}
197
198void MipsTargetStreamer::emitEmptyDelaySlot(bool hasShortDelaySlot, SMLoc IDLoc,
199 const MCSubtargetInfo *STI) {
200 if (hasShortDelaySlot)
201 emitRR(Mips::MOVE16_MM, Mips::ZERO, Mips::ZERO, IDLoc, STI);
202 else
203 emitRRI(Mips::SLL, Mips::ZERO, Mips::ZERO, 0, IDLoc, STI);
204}
205
206void MipsTargetStreamer::emitNop(SMLoc IDLoc, const MCSubtargetInfo *STI) {
207 emitRRI(Mips::SLL, Mips::ZERO, Mips::ZERO, 0, IDLoc, STI);
208}
209
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000210MipsTargetAsmStreamer::MipsTargetAsmStreamer(MCStreamer &S,
211 formatted_raw_ostream &OS)
212 : MipsTargetStreamer(S), OS(OS) {}
Jack Carter6ef6cc52013-11-19 20:53:28 +0000213
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000214void MipsTargetAsmStreamer::emitDirectiveSetMicroMips() {
215 OS << "\t.set\tmicromips\n";
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000216 forbidModuleDirective();
Jack Carter6ef6cc52013-11-19 20:53:28 +0000217}
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000218
219void MipsTargetAsmStreamer::emitDirectiveSetNoMicroMips() {
220 OS << "\t.set\tnomicromips\n";
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000221 forbidModuleDirective();
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000222}
223
Rafael Espindola6633d572014-01-14 18:57:12 +0000224void MipsTargetAsmStreamer::emitDirectiveSetMips16() {
225 OS << "\t.set\tmips16\n";
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000226 forbidModuleDirective();
Rafael Espindola6633d572014-01-14 18:57:12 +0000227}
228
229void MipsTargetAsmStreamer::emitDirectiveSetNoMips16() {
230 OS << "\t.set\tnomips16\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000231 MipsTargetStreamer::emitDirectiveSetNoMips16();
Rafael Espindola6633d572014-01-14 18:57:12 +0000232}
233
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000234void MipsTargetAsmStreamer::emitDirectiveSetReorder() {
235 OS << "\t.set\treorder\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000236 MipsTargetStreamer::emitDirectiveSetReorder();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000237}
238
239void MipsTargetAsmStreamer::emitDirectiveSetNoReorder() {
240 OS << "\t.set\tnoreorder\n";
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000241 forbidModuleDirective();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000242}
243
244void MipsTargetAsmStreamer::emitDirectiveSetMacro() {
245 OS << "\t.set\tmacro\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000246 MipsTargetStreamer::emitDirectiveSetMacro();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000247}
248
249void MipsTargetAsmStreamer::emitDirectiveSetNoMacro() {
250 OS << "\t.set\tnomacro\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000251 MipsTargetStreamer::emitDirectiveSetNoMacro();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000252}
253
Daniel Sanders44934432014-08-07 12:03:36 +0000254void MipsTargetAsmStreamer::emitDirectiveSetMsa() {
255 OS << "\t.set\tmsa\n";
256 MipsTargetStreamer::emitDirectiveSetMsa();
257}
258
259void MipsTargetAsmStreamer::emitDirectiveSetNoMsa() {
260 OS << "\t.set\tnomsa\n";
261 MipsTargetStreamer::emitDirectiveSetNoMsa();
262}
263
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000264void MipsTargetAsmStreamer::emitDirectiveSetAt() {
265 OS << "\t.set\tat\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000266 MipsTargetStreamer::emitDirectiveSetAt();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000267}
268
Toma Tabacu16a74492015-02-13 10:30:57 +0000269void MipsTargetAsmStreamer::emitDirectiveSetAtWithArg(unsigned RegNo) {
270 OS << "\t.set\tat=$" << Twine(RegNo) << "\n";
271 MipsTargetStreamer::emitDirectiveSetAtWithArg(RegNo);
272}
273
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000274void MipsTargetAsmStreamer::emitDirectiveSetNoAt() {
275 OS << "\t.set\tnoat\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000276 MipsTargetStreamer::emitDirectiveSetNoAt();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000277}
278
279void MipsTargetAsmStreamer::emitDirectiveEnd(StringRef Name) {
280 OS << "\t.end\t" << Name << '\n';
281}
282
Rafael Espindola6633d572014-01-14 18:57:12 +0000283void MipsTargetAsmStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {
284 OS << "\t.ent\t" << Symbol.getName() << '\n';
285}
286
Jack Carter0cd3c192014-01-06 23:27:31 +0000287void MipsTargetAsmStreamer::emitDirectiveAbiCalls() { OS << "\t.abicalls\n"; }
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000288
289void MipsTargetAsmStreamer::emitDirectiveNaN2008() { OS << "\t.nan\t2008\n"; }
290
291void MipsTargetAsmStreamer::emitDirectiveNaNLegacy() {
292 OS << "\t.nan\tlegacy\n";
293}
294
Jack Carter0cd3c192014-01-06 23:27:31 +0000295void MipsTargetAsmStreamer::emitDirectiveOptionPic0() {
296 OS << "\t.option\tpic0\n";
297}
298
Matheus Almeidaf79b2812014-03-26 13:40:29 +0000299void MipsTargetAsmStreamer::emitDirectiveOptionPic2() {
300 OS << "\t.option\tpic2\n";
301}
302
Toma Tabacu9ca50962015-04-16 09:53:47 +0000303void MipsTargetAsmStreamer::emitDirectiveInsn() {
304 MipsTargetStreamer::emitDirectiveInsn();
305 OS << "\t.insn\n";
306}
307
Rafael Espindola054234f2014-01-27 03:53:56 +0000308void MipsTargetAsmStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
309 unsigned ReturnReg) {
310 OS << "\t.frame\t$"
311 << StringRef(MipsInstPrinter::getRegisterName(StackReg)).lower() << ","
312 << StackSize << ",$"
Rafael Espindola25fa2912014-01-27 04:33:11 +0000313 << StringRef(MipsInstPrinter::getRegisterName(ReturnReg)).lower() << '\n';
314}
315
Toma Tabacu85618b32014-08-19 14:22:52 +0000316void MipsTargetAsmStreamer::emitDirectiveSetArch(StringRef Arch) {
317 OS << "\t.set arch=" << Arch << "\n";
318 MipsTargetStreamer::emitDirectiveSetArch(Arch);
319}
320
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +0000321void MipsTargetAsmStreamer::emitDirectiveSetMips0() {
322 OS << "\t.set\tmips0\n";
323 MipsTargetStreamer::emitDirectiveSetMips0();
324}
Toma Tabacu26647792014-09-09 12:52:14 +0000325
Daniel Sandersf0df2212014-08-04 12:20:00 +0000326void MipsTargetAsmStreamer::emitDirectiveSetMips1() {
327 OS << "\t.set\tmips1\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000328 MipsTargetStreamer::emitDirectiveSetMips1();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000329}
330
331void MipsTargetAsmStreamer::emitDirectiveSetMips2() {
332 OS << "\t.set\tmips2\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000333 MipsTargetStreamer::emitDirectiveSetMips2();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000334}
335
336void MipsTargetAsmStreamer::emitDirectiveSetMips3() {
337 OS << "\t.set\tmips3\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000338 MipsTargetStreamer::emitDirectiveSetMips3();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000339}
340
341void MipsTargetAsmStreamer::emitDirectiveSetMips4() {
342 OS << "\t.set\tmips4\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000343 MipsTargetStreamer::emitDirectiveSetMips4();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000344}
345
346void MipsTargetAsmStreamer::emitDirectiveSetMips5() {
347 OS << "\t.set\tmips5\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000348 MipsTargetStreamer::emitDirectiveSetMips5();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000349}
350
351void MipsTargetAsmStreamer::emitDirectiveSetMips32() {
352 OS << "\t.set\tmips32\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000353 MipsTargetStreamer::emitDirectiveSetMips32();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000354}
355
Vladimir Medic615b26e2014-03-04 09:54:09 +0000356void MipsTargetAsmStreamer::emitDirectiveSetMips32R2() {
357 OS << "\t.set\tmips32r2\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000358 MipsTargetStreamer::emitDirectiveSetMips32R2();
Vladimir Medic615b26e2014-03-04 09:54:09 +0000359}
360
Daniel Sanders17793142015-02-18 16:24:50 +0000361void MipsTargetAsmStreamer::emitDirectiveSetMips32R3() {
362 OS << "\t.set\tmips32r3\n";
363 MipsTargetStreamer::emitDirectiveSetMips32R3();
364}
365
366void MipsTargetAsmStreamer::emitDirectiveSetMips32R5() {
367 OS << "\t.set\tmips32r5\n";
368 MipsTargetStreamer::emitDirectiveSetMips32R5();
369}
370
Daniel Sandersf0df2212014-08-04 12:20:00 +0000371void MipsTargetAsmStreamer::emitDirectiveSetMips32R6() {
372 OS << "\t.set\tmips32r6\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000373 MipsTargetStreamer::emitDirectiveSetMips32R6();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000374}
375
Matheus Almeida3b9c63d2014-03-26 15:14:32 +0000376void MipsTargetAsmStreamer::emitDirectiveSetMips64() {
377 OS << "\t.set\tmips64\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000378 MipsTargetStreamer::emitDirectiveSetMips64();
Matheus Almeida3b9c63d2014-03-26 15:14:32 +0000379}
380
Matheus Almeidaa2cd0092014-03-26 14:52:22 +0000381void MipsTargetAsmStreamer::emitDirectiveSetMips64R2() {
382 OS << "\t.set\tmips64r2\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000383 MipsTargetStreamer::emitDirectiveSetMips64R2();
Matheus Almeidaa2cd0092014-03-26 14:52:22 +0000384}
385
Daniel Sanders17793142015-02-18 16:24:50 +0000386void MipsTargetAsmStreamer::emitDirectiveSetMips64R3() {
387 OS << "\t.set\tmips64r3\n";
388 MipsTargetStreamer::emitDirectiveSetMips64R3();
389}
390
391void MipsTargetAsmStreamer::emitDirectiveSetMips64R5() {
392 OS << "\t.set\tmips64r5\n";
393 MipsTargetStreamer::emitDirectiveSetMips64R5();
394}
395
Daniel Sandersf0df2212014-08-04 12:20:00 +0000396void MipsTargetAsmStreamer::emitDirectiveSetMips64R6() {
397 OS << "\t.set\tmips64r6\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000398 MipsTargetStreamer::emitDirectiveSetMips64R6();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000399}
400
Vladimir Medic27c398e2014-03-05 11:05:09 +0000401void MipsTargetAsmStreamer::emitDirectiveSetDsp() {
402 OS << "\t.set\tdsp\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000403 MipsTargetStreamer::emitDirectiveSetDsp();
Vladimir Medic27c398e2014-03-05 11:05:09 +0000404}
Toma Tabacu9db22db2014-09-09 10:15:38 +0000405
Toma Tabacu351b2fe2014-09-17 09:01:54 +0000406void MipsTargetAsmStreamer::emitDirectiveSetNoDsp() {
407 OS << "\t.set\tnodsp\n";
408 MipsTargetStreamer::emitDirectiveSetNoDsp();
409}
410
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +0000411void MipsTargetAsmStreamer::emitDirectiveSetPop() {
412 OS << "\t.set\tpop\n";
413 MipsTargetStreamer::emitDirectiveSetPop();
414}
Toma Tabacu9db22db2014-09-09 10:15:38 +0000415
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +0000416void MipsTargetAsmStreamer::emitDirectiveSetPush() {
417 OS << "\t.set\tpush\n";
418 MipsTargetStreamer::emitDirectiveSetPush();
419}
Toma Tabacu9db22db2014-09-09 10:15:38 +0000420
Toma Tabacu29696502015-06-02 09:48:04 +0000421void MipsTargetAsmStreamer::emitDirectiveSetSoftFloat() {
422 OS << "\t.set\tsoftfloat\n";
423 MipsTargetStreamer::emitDirectiveSetSoftFloat();
424}
425
426void MipsTargetAsmStreamer::emitDirectiveSetHardFloat() {
427 OS << "\t.set\thardfloat\n";
428 MipsTargetStreamer::emitDirectiveSetHardFloat();
429}
430
Rafael Espindola25fa2912014-01-27 04:33:11 +0000431// Print a 32 bit hex number with all numbers.
432static void printHex32(unsigned Value, raw_ostream &OS) {
433 OS << "0x";
434 for (int i = 7; i >= 0; i--)
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000435 OS.write_hex((Value & (0xF << (i * 4))) >> (i * 4));
Rafael Espindola25fa2912014-01-27 04:33:11 +0000436}
437
438void MipsTargetAsmStreamer::emitMask(unsigned CPUBitmask,
439 int CPUTopSavedRegOff) {
440 OS << "\t.mask \t";
441 printHex32(CPUBitmask, OS);
442 OS << ',' << CPUTopSavedRegOff << '\n';
443}
444
445void MipsTargetAsmStreamer::emitFMask(unsigned FPUBitmask,
446 int FPUTopSavedRegOff) {
447 OS << "\t.fmask\t";
448 printHex32(FPUBitmask, OS);
449 OS << "," << FPUTopSavedRegOff << '\n';
Rafael Espindola054234f2014-01-27 03:53:56 +0000450}
451
Toma Tabacuc4c202a2014-10-01 14:53:19 +0000452void MipsTargetAsmStreamer::emitDirectiveCpLoad(unsigned RegNo) {
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000453 OS << "\t.cpload\t$"
454 << StringRef(MipsInstPrinter::getRegisterName(RegNo)).lower() << "\n";
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000455 forbidModuleDirective();
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000456}
457
Daniel Sandersc6924fa2016-04-18 12:06:15 +0000458void MipsTargetAsmStreamer::emitDirectiveCpRestore(int Offset) {
459 MipsTargetStreamer::emitDirectiveCpRestore(Offset);
Daniel Sanderse2982ad2015-09-17 16:08:39 +0000460 OS << "\t.cprestore\t" << Offset << "\n";
461}
462
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000463void MipsTargetAsmStreamer::emitDirectiveCpsetup(unsigned RegNo,
464 int RegOrOffset,
465 const MCSymbol &Sym,
466 bool IsReg) {
467 OS << "\t.cpsetup\t$"
468 << StringRef(MipsInstPrinter::getRegisterName(RegNo)).lower() << ", ";
469
470 if (IsReg)
471 OS << "$"
472 << StringRef(MipsInstPrinter::getRegisterName(RegOrOffset)).lower();
473 else
474 OS << RegOrOffset;
475
476 OS << ", ";
477
Daniel Sanders5d796282015-09-21 09:26:55 +0000478 OS << Sym.getName();
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000479 forbidModuleDirective();
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000480}
481
Daniel Sandersf173dda2015-09-22 10:50:09 +0000482void MipsTargetAsmStreamer::emitDirectiveCpreturn(unsigned SaveLocation,
483 bool SaveLocationIsRegister) {
484 OS << "\t.cpreturn";
485 forbidModuleDirective();
486}
487
Toma Tabacua64e5402015-06-25 12:44:38 +0000488void MipsTargetAsmStreamer::emitDirectiveModuleFP() {
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000489 OS << "\t.module\tfp=";
Toma Tabacua64e5402015-06-25 12:44:38 +0000490 OS << ABIFlagsSection.getFpABIString(ABIFlagsSection.getFpABI()) << "\n";
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000491}
492
Daniel Sanders7e527422014-07-10 13:38:23 +0000493void MipsTargetAsmStreamer::emitDirectiveSetFp(
494 MipsABIFlagsSection::FpABIKind Value) {
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +0000495 MipsTargetStreamer::emitDirectiveSetFp(Value);
496
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000497 OS << "\t.set\tfp=";
Daniel Sanders7e527422014-07-10 13:38:23 +0000498 OS << ABIFlagsSection.getFpABIString(Value) << "\n";
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000499}
500
Toma Tabacu3c499582015-06-25 10:56:57 +0000501void MipsTargetAsmStreamer::emitDirectiveModuleOddSPReg() {
502 MipsTargetStreamer::emitDirectiveModuleOddSPReg();
Daniel Sanders7e527422014-07-10 13:38:23 +0000503
Toma Tabacu3c499582015-06-25 10:56:57 +0000504 OS << "\t.module\t" << (ABIFlagsSection.OddSPReg ? "" : "no") << "oddspreg\n";
Daniel Sanders7e527422014-07-10 13:38:23 +0000505}
506
Toma Tabacu32c72aa2015-06-30 09:36:50 +0000507void MipsTargetAsmStreamer::emitDirectiveSetOddSPReg() {
508 MipsTargetStreamer::emitDirectiveSetOddSPReg();
509 OS << "\t.set\toddspreg\n";
510}
511
512void MipsTargetAsmStreamer::emitDirectiveSetNoOddSPReg() {
513 MipsTargetStreamer::emitDirectiveSetNoOddSPReg();
514 OS << "\t.set\tnooddspreg\n";
515}
516
Toma Tabacu0f093132015-06-30 13:46:03 +0000517void MipsTargetAsmStreamer::emitDirectiveModuleSoftFloat() {
518 OS << "\t.module\tsoftfloat\n";
519}
520
521void MipsTargetAsmStreamer::emitDirectiveModuleHardFloat() {
522 OS << "\t.module\thardfloat\n";
523}
524
Jack Carter0cd3c192014-01-06 23:27:31 +0000525// This part is for ELF object output.
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000526MipsTargetELFStreamer::MipsTargetELFStreamer(MCStreamer &S,
527 const MCSubtargetInfo &STI)
Rafael Espindola972e71a2014-01-31 23:10:26 +0000528 : MipsTargetStreamer(S), MicroMipsEnabled(false), STI(STI) {
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000529 MCAssembler &MCA = getStreamer().getAssembler();
Simon Atanasyanc99ce682015-03-24 12:24:56 +0000530 Pic = MCA.getContext().getObjectFileInfo()->getRelocM() == Reloc::PIC_;
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000531
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000532 const FeatureBitset &Features = STI.getFeatureBits();
Eric Christophera5762812015-01-26 17:33:46 +0000533
534 // Set the header flags that we can in the constructor.
535 // FIXME: This is a fairly terrible hack. We set the rest
536 // of these in the destructor. The problem here is two-fold:
537 //
538 // a: Some of the eflags can be set/reset by directives.
539 // b: There aren't any usage paths that initialize the ABI
540 // pointer until after we initialize either an assembler
541 // or the target machine.
542 // We can fix this by making the target streamer construct
543 // the ABI, but this is fraught with wide ranging dependency
544 // issues as well.
545 unsigned EFlags = MCA.getELFHeaderEFlags();
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000546
547 // Architecture
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000548 if (Features[Mips::FeatureMips64r6])
Daniel Sanders950f48d2014-07-04 15:21:53 +0000549 EFlags |= ELF::EF_MIPS_ARCH_64R6;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000550 else if (Features[Mips::FeatureMips64r2] ||
551 Features[Mips::FeatureMips64r3] ||
552 Features[Mips::FeatureMips64r5])
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000553 EFlags |= ELF::EF_MIPS_ARCH_64R2;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000554 else if (Features[Mips::FeatureMips64])
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000555 EFlags |= ELF::EF_MIPS_ARCH_64;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000556 else if (Features[Mips::FeatureMips5])
Daniel Sanders950f48d2014-07-04 15:21:53 +0000557 EFlags |= ELF::EF_MIPS_ARCH_5;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000558 else if (Features[Mips::FeatureMips4])
Daniel Sandersf7b32292014-04-03 12:13:36 +0000559 EFlags |= ELF::EF_MIPS_ARCH_4;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000560 else if (Features[Mips::FeatureMips3])
Daniel Sanders950f48d2014-07-04 15:21:53 +0000561 EFlags |= ELF::EF_MIPS_ARCH_3;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000562 else if (Features[Mips::FeatureMips32r6])
Daniel Sanders950f48d2014-07-04 15:21:53 +0000563 EFlags |= ELF::EF_MIPS_ARCH_32R6;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000564 else if (Features[Mips::FeatureMips32r2] ||
565 Features[Mips::FeatureMips32r3] ||
566 Features[Mips::FeatureMips32r5])
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000567 EFlags |= ELF::EF_MIPS_ARCH_32R2;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000568 else if (Features[Mips::FeatureMips32])
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000569 EFlags |= ELF::EF_MIPS_ARCH_32;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000570 else if (Features[Mips::FeatureMips2])
Daniel Sanders950f48d2014-07-04 15:21:53 +0000571 EFlags |= ELF::EF_MIPS_ARCH_2;
572 else
573 EFlags |= ELF::EF_MIPS_ARCH_1;
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000574
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000575 // Other options.
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000576 if (Features[Mips::FeatureNaN2008])
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000577 EFlags |= ELF::EF_MIPS_NAN2008;
578
Daniel Sanders16ec6c12014-07-17 09:52:56 +0000579 // -mabicalls and -mplt are not implemented but we should act as if they were
580 // given.
581 EFlags |= ELF::EF_MIPS_CPIC;
Daniel Sanders16ec6c12014-07-17 09:52:56 +0000582
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000583 MCA.setELFHeaderEFlags(EFlags);
584}
Jack Carter86ac5c12013-11-18 23:55:27 +0000585
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000586void MipsTargetELFStreamer::emitLabel(MCSymbol *S) {
587 auto *Symbol = cast<MCSymbolELF>(S);
Rafael Espindola26e917c2014-01-15 03:07:12 +0000588 if (!isMicroMipsEnabled())
589 return;
Rafael Espindolac73aed12015-06-03 19:03:11 +0000590 getStreamer().getAssembler().registerSymbol(*Symbol);
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000591 uint8_t Type = Symbol->getType();
Rafael Espindola26e917c2014-01-15 03:07:12 +0000592 if (Type != ELF::STT_FUNC)
593 return;
594
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000595 Symbol->setOther(ELF::STO_MIPS_MICROMIPS);
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000596}
597
Rafael Espindola972e71a2014-01-31 23:10:26 +0000598void MipsTargetELFStreamer::finish() {
599 MCAssembler &MCA = getStreamer().getAssembler();
Daniel Sanders68c37472014-07-21 13:30:55 +0000600 const MCObjectFileInfo &OFI = *MCA.getContext().getObjectFileInfo();
Rafael Espindola972e71a2014-01-31 23:10:26 +0000601
Daniel Sanders41ffa5d2014-07-14 15:05:51 +0000602 // .bss, .text and .data are always at least 16-byte aligned.
Rafael Espindola967d6a62015-05-21 21:02:35 +0000603 MCSection &TextSection = *OFI.getTextSection();
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000604 MCA.registerSection(TextSection);
Rafael Espindola967d6a62015-05-21 21:02:35 +0000605 MCSection &DataSection = *OFI.getDataSection();
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000606 MCA.registerSection(DataSection);
Rafael Espindola967d6a62015-05-21 21:02:35 +0000607 MCSection &BSSSection = *OFI.getBSSSection();
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000608 MCA.registerSection(BSSSection);
Daniel Sanders41ffa5d2014-07-14 15:05:51 +0000609
Rafael Espindola967d6a62015-05-21 21:02:35 +0000610 TextSection.setAlignment(std::max(16u, TextSection.getAlignment()));
611 DataSection.setAlignment(std::max(16u, DataSection.getAlignment()));
612 BSSSection.setAlignment(std::max(16u, BSSSection.getAlignment()));
Daniel Sanders41ffa5d2014-07-14 15:05:51 +0000613
Daniel Sanders9db710a2016-04-29 12:44:07 +0000614 // Make sections sizes a multiple of the alignment.
615 MCStreamer &OS = getStreamer();
616 for (MCSection &S : MCA) {
617 MCSectionELF &Section = static_cast<MCSectionELF &>(S);
618
619 unsigned Alignment = Section.getAlignment();
620 if (Alignment) {
621 OS.SwitchSection(&Section);
622 if (Section.UseCodeAlign())
623 OS.EmitCodeAlignment(Alignment, Alignment);
624 else
625 OS.EmitValueToAlignment(Alignment, 0, 1, Alignment);
626 }
627 }
628
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000629 const FeatureBitset &Features = STI.getFeatureBits();
Eric Christophera5762812015-01-26 17:33:46 +0000630
631 // Update e_header flags. See the FIXME and comment above in
632 // the constructor for a full rundown on this.
633 unsigned EFlags = MCA.getELFHeaderEFlags();
634
635 // ABI
636 // N64 does not require any ABI bits.
637 if (getABI().IsO32())
638 EFlags |= ELF::EF_MIPS_ABI_O32;
639 else if (getABI().IsN32())
640 EFlags |= ELF::EF_MIPS_ABI2;
641
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000642 if (Features[Mips::FeatureGP64Bit]) {
Eric Christophera5762812015-01-26 17:33:46 +0000643 if (getABI().IsO32())
644 EFlags |= ELF::EF_MIPS_32BITMODE; /* Compatibility Mode */
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000645 } else if (Features[Mips::FeatureMips64r2] || Features[Mips::FeatureMips64])
Eric Christophera5762812015-01-26 17:33:46 +0000646 EFlags |= ELF::EF_MIPS_32BITMODE;
647
648 // If we've set the cpic eflag and we're n64, go ahead and set the pic
649 // one as well.
650 if (EFlags & ELF::EF_MIPS_CPIC && getABI().IsN64())
651 EFlags |= ELF::EF_MIPS_PIC;
652
653 MCA.setELFHeaderEFlags(EFlags);
654
Daniel Sanders68c37472014-07-21 13:30:55 +0000655 // Emit all the option records.
656 // At the moment we are only emitting .Mips.options (ODK_REGINFO) and
657 // .reginfo.
658 MipsELFStreamer &MEF = static_cast<MipsELFStreamer &>(Streamer);
659 MEF.EmitMipsOptionRecords();
Rafael Espindola972e71a2014-01-31 23:10:26 +0000660
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000661 emitMipsAbiFlags();
Rafael Espindola972e71a2014-01-31 23:10:26 +0000662}
663
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000664void MipsTargetELFStreamer::emitAssignment(MCSymbol *S, const MCExpr *Value) {
665 auto *Symbol = cast<MCSymbolELF>(S);
Zoran Jovanovic28221d82014-03-20 09:44:49 +0000666 // If on rhs is micromips symbol then mark Symbol as microMips.
667 if (Value->getKind() != MCExpr::SymbolRef)
668 return;
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000669 const auto &RhsSym = cast<MCSymbolELF>(
670 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol());
Toma Tabacu2cc44f52015-04-16 13:37:32 +0000671
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000672 if (!(RhsSym.getOther() & ELF::STO_MIPS_MICROMIPS))
Zoran Jovanovic28221d82014-03-20 09:44:49 +0000673 return;
674
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000675 Symbol->setOther(ELF::STO_MIPS_MICROMIPS);
Zoran Jovanovic28221d82014-03-20 09:44:49 +0000676}
677
Jack Carter86ac5c12013-11-18 23:55:27 +0000678MCELFStreamer &MipsTargetELFStreamer::getStreamer() {
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000679 return static_cast<MCELFStreamer &>(Streamer);
Jack Carter86ac5c12013-11-18 23:55:27 +0000680}
681
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000682void MipsTargetELFStreamer::emitDirectiveSetMicroMips() {
683 MicroMipsEnabled = true;
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000684
685 MCAssembler &MCA = getStreamer().getAssembler();
686 unsigned Flags = MCA.getELFHeaderEFlags();
687 Flags |= ELF::EF_MIPS_MICROMIPS;
688 MCA.setELFHeaderEFlags(Flags);
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000689 forbidModuleDirective();
Jack Carter86ac5c12013-11-18 23:55:27 +0000690}
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000691
692void MipsTargetELFStreamer::emitDirectiveSetNoMicroMips() {
693 MicroMipsEnabled = false;
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000694 forbidModuleDirective();
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000695}
696
Rafael Espindola6633d572014-01-14 18:57:12 +0000697void MipsTargetELFStreamer::emitDirectiveSetMips16() {
Rafael Espindolae7583752014-01-24 16:13:20 +0000698 MCAssembler &MCA = getStreamer().getAssembler();
699 unsigned Flags = MCA.getELFHeaderEFlags();
700 Flags |= ELF::EF_MIPS_ARCH_ASE_M16;
701 MCA.setELFHeaderEFlags(Flags);
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000702 forbidModuleDirective();
Rafael Espindola6633d572014-01-14 18:57:12 +0000703}
704
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000705void MipsTargetELFStreamer::emitDirectiveSetNoReorder() {
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000706 MCAssembler &MCA = getStreamer().getAssembler();
707 unsigned Flags = MCA.getELFHeaderEFlags();
708 Flags |= ELF::EF_MIPS_NOREORDER;
709 MCA.setELFHeaderEFlags(Flags);
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000710 forbidModuleDirective();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000711}
712
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000713void MipsTargetELFStreamer::emitDirectiveEnd(StringRef Name) {
Daniel Sandersd97a6342014-08-13 10:07:34 +0000714 MCAssembler &MCA = getStreamer().getAssembler();
715 MCContext &Context = MCA.getContext();
716 MCStreamer &OS = getStreamer();
717
Scott Egerton219fae92016-02-17 11:15:16 +0000718 MCSectionELF *Sec = Context.getELFSection(".pdr", ELF::SHT_PROGBITS, 0);
Daniel Sandersd97a6342014-08-13 10:07:34 +0000719
Daniel Sanders2b561332015-11-23 16:08:03 +0000720 MCSymbol *Sym = Context.getOrCreateSymbol(Name);
Daniel Sandersd97a6342014-08-13 10:07:34 +0000721 const MCSymbolRefExpr *ExprRef =
Daniel Sanders2b561332015-11-23 16:08:03 +0000722 MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, Context);
Daniel Sandersd97a6342014-08-13 10:07:34 +0000723
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000724 MCA.registerSection(*Sec);
Rafael Espindola967d6a62015-05-21 21:02:35 +0000725 Sec->setAlignment(4);
Daniel Sandersd97a6342014-08-13 10:07:34 +0000726
727 OS.PushSection();
728
729 OS.SwitchSection(Sec);
730
731 OS.EmitValueImpl(ExprRef, 4);
732
733 OS.EmitIntValue(GPRInfoSet ? GPRBitMask : 0, 4); // reg_mask
734 OS.EmitIntValue(GPRInfoSet ? GPROffset : 0, 4); // reg_offset
735
736 OS.EmitIntValue(FPRInfoSet ? FPRBitMask : 0, 4); // fpreg_mask
737 OS.EmitIntValue(FPRInfoSet ? FPROffset : 0, 4); // fpreg_offset
738
739 OS.EmitIntValue(FrameInfoSet ? FrameOffset : 0, 4); // frame_offset
740 OS.EmitIntValue(FrameInfoSet ? FrameReg : 0, 4); // frame_reg
741 OS.EmitIntValue(FrameInfoSet ? ReturnReg : 0, 4); // return_reg
742
743 // The .end directive marks the end of a procedure. Invalidate
744 // the information gathered up until this point.
745 GPRInfoSet = FPRInfoSet = FrameInfoSet = false;
746
747 OS.PopSection();
Daniel Sanders2b561332015-11-23 16:08:03 +0000748
749 // .end also implicitly sets the size.
750 MCSymbol *CurPCSym = Context.createTempSymbol();
751 OS.EmitLabel(CurPCSym);
752 const MCExpr *Size = MCBinaryExpr::createSub(
753 MCSymbolRefExpr::create(CurPCSym, MCSymbolRefExpr::VK_None, Context),
754 ExprRef, Context);
755 int64_t AbsSize;
756 if (!Size->evaluateAsAbsolute(AbsSize, MCA))
757 llvm_unreachable("Function size must be evaluatable as absolute");
758 Size = MCConstantExpr::create(AbsSize, Context);
759 static_cast<MCSymbolELF *>(Sym)->setSize(Size);
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000760}
761
Rafael Espindola6633d572014-01-14 18:57:12 +0000762void MipsTargetELFStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {
Daniel Sandersd97a6342014-08-13 10:07:34 +0000763 GPRInfoSet = FPRInfoSet = FrameInfoSet = false;
Daniel Sanders2b561332015-11-23 16:08:03 +0000764
765 // .ent also acts like an implicit '.type symbol, STT_FUNC'
766 static_cast<const MCSymbolELF &>(Symbol).setType(ELF::STT_FUNC);
Rafael Espindola6633d572014-01-14 18:57:12 +0000767}
768
Jack Carter0cd3c192014-01-06 23:27:31 +0000769void MipsTargetELFStreamer::emitDirectiveAbiCalls() {
770 MCAssembler &MCA = getStreamer().getAssembler();
771 unsigned Flags = MCA.getELFHeaderEFlags();
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000772 Flags |= ELF::EF_MIPS_CPIC | ELF::EF_MIPS_PIC;
Jack Carter0cd3c192014-01-06 23:27:31 +0000773 MCA.setELFHeaderEFlags(Flags);
774}
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000775
776void MipsTargetELFStreamer::emitDirectiveNaN2008() {
777 MCAssembler &MCA = getStreamer().getAssembler();
778 unsigned Flags = MCA.getELFHeaderEFlags();
779 Flags |= ELF::EF_MIPS_NAN2008;
780 MCA.setELFHeaderEFlags(Flags);
781}
782
783void MipsTargetELFStreamer::emitDirectiveNaNLegacy() {
784 MCAssembler &MCA = getStreamer().getAssembler();
785 unsigned Flags = MCA.getELFHeaderEFlags();
786 Flags &= ~ELF::EF_MIPS_NAN2008;
787 MCA.setELFHeaderEFlags(Flags);
788}
789
Jack Carter0cd3c192014-01-06 23:27:31 +0000790void MipsTargetELFStreamer::emitDirectiveOptionPic0() {
791 MCAssembler &MCA = getStreamer().getAssembler();
792 unsigned Flags = MCA.getELFHeaderEFlags();
Matheus Almeidaf79b2812014-03-26 13:40:29 +0000793 // This option overrides other PIC options like -KPIC.
794 Pic = false;
Jack Carter0cd3c192014-01-06 23:27:31 +0000795 Flags &= ~ELF::EF_MIPS_PIC;
796 MCA.setELFHeaderEFlags(Flags);
797}
Rafael Espindola054234f2014-01-27 03:53:56 +0000798
Matheus Almeidaf79b2812014-03-26 13:40:29 +0000799void MipsTargetELFStreamer::emitDirectiveOptionPic2() {
800 MCAssembler &MCA = getStreamer().getAssembler();
801 unsigned Flags = MCA.getELFHeaderEFlags();
802 Pic = true;
803 // NOTE: We are following the GAS behaviour here which means the directive
804 // 'pic2' also sets the CPIC bit in the ELF header. This is different from
805 // what is stated in the SYSV ABI which consider the bits EF_MIPS_PIC and
806 // EF_MIPS_CPIC to be mutually exclusive.
807 Flags |= ELF::EF_MIPS_PIC | ELF::EF_MIPS_CPIC;
808 MCA.setELFHeaderEFlags(Flags);
809}
810
Toma Tabacu9ca50962015-04-16 09:53:47 +0000811void MipsTargetELFStreamer::emitDirectiveInsn() {
812 MipsTargetStreamer::emitDirectiveInsn();
813 MipsELFStreamer &MEF = static_cast<MipsELFStreamer &>(Streamer);
814 MEF.createPendingLabelRelocs();
815}
816
Rafael Espindola054234f2014-01-27 03:53:56 +0000817void MipsTargetELFStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
Daniel Sandersd97a6342014-08-13 10:07:34 +0000818 unsigned ReturnReg_) {
819 MCContext &Context = getStreamer().getAssembler().getContext();
820 const MCRegisterInfo *RegInfo = Context.getRegisterInfo();
821
822 FrameInfoSet = true;
823 FrameReg = RegInfo->getEncodingValue(StackReg);
824 FrameOffset = StackSize;
825 ReturnReg = RegInfo->getEncodingValue(ReturnReg_);
Rafael Espindola054234f2014-01-27 03:53:56 +0000826}
Rafael Espindola25fa2912014-01-27 04:33:11 +0000827
828void MipsTargetELFStreamer::emitMask(unsigned CPUBitmask,
829 int CPUTopSavedRegOff) {
Daniel Sandersd97a6342014-08-13 10:07:34 +0000830 GPRInfoSet = true;
831 GPRBitMask = CPUBitmask;
832 GPROffset = CPUTopSavedRegOff;
Rafael Espindola25fa2912014-01-27 04:33:11 +0000833}
834
835void MipsTargetELFStreamer::emitFMask(unsigned FPUBitmask,
836 int FPUTopSavedRegOff) {
Daniel Sandersd97a6342014-08-13 10:07:34 +0000837 FPRInfoSet = true;
838 FPRBitMask = FPUBitmask;
839 FPROffset = FPUTopSavedRegOff;
Rafael Espindola25fa2912014-01-27 04:33:11 +0000840}
Vladimir Medic615b26e2014-03-04 09:54:09 +0000841
Toma Tabacuc4c202a2014-10-01 14:53:19 +0000842void MipsTargetELFStreamer::emitDirectiveCpLoad(unsigned RegNo) {
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000843 // .cpload $reg
844 // This directive expands to:
845 // lui $gp, %hi(_gp_disp)
846 // addui $gp, $gp, %lo(_gp_disp)
847 // addu $gp, $gp, $reg
848 // when support for position independent code is enabled.
Eric Christophera5762812015-01-26 17:33:46 +0000849 if (!Pic || (getABI().IsN32() || getABI().IsN64()))
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000850 return;
851
852 // There's a GNU extension controlled by -mno-shared that allows
853 // locally-binding symbols to be accessed using absolute addresses.
854 // This is currently not supported. When supported -mno-shared makes
855 // .cpload expand to:
856 // lui $gp, %hi(__gnu_local_gp)
857 // addiu $gp, $gp, %lo(__gnu_local_gp)
858
859 StringRef SymName("_gp_disp");
860 MCAssembler &MCA = getStreamer().getAssembler();
Jim Grosbach6f482002015-05-18 18:43:14 +0000861 MCSymbol *GP_Disp = MCA.getContext().getOrCreateSymbol(SymName);
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000862 MCA.registerSymbol(*GP_Disp);
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000863
864 MCInst TmpInst;
865 TmpInst.setOpcode(Mips::LUi);
Jim Grosbache9119e42015-05-13 18:37:00 +0000866 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
Jim Grosbach13760bd2015-05-30 01:25:56 +0000867 const MCSymbolRefExpr *HiSym = MCSymbolRefExpr::create(
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000868 "_gp_disp", MCSymbolRefExpr::VK_Mips_ABS_HI, MCA.getContext());
Jim Grosbache9119e42015-05-13 18:37:00 +0000869 TmpInst.addOperand(MCOperand::createExpr(HiSym));
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000870 getStreamer().EmitInstruction(TmpInst, STI);
871
872 TmpInst.clear();
873
874 TmpInst.setOpcode(Mips::ADDiu);
Jim Grosbache9119e42015-05-13 18:37:00 +0000875 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
876 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
Jim Grosbach13760bd2015-05-30 01:25:56 +0000877 const MCSymbolRefExpr *LoSym = MCSymbolRefExpr::create(
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000878 "_gp_disp", MCSymbolRefExpr::VK_Mips_ABS_LO, MCA.getContext());
Jim Grosbache9119e42015-05-13 18:37:00 +0000879 TmpInst.addOperand(MCOperand::createExpr(LoSym));
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000880 getStreamer().EmitInstruction(TmpInst, STI);
881
882 TmpInst.clear();
883
884 TmpInst.setOpcode(Mips::ADDu);
Jim Grosbache9119e42015-05-13 18:37:00 +0000885 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
886 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
887 TmpInst.addOperand(MCOperand::createReg(RegNo));
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000888 getStreamer().EmitInstruction(TmpInst, STI);
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000889
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000890 forbidModuleDirective();
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000891}
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000892
Daniel Sandersc6924fa2016-04-18 12:06:15 +0000893void MipsTargetELFStreamer::emitDirectiveCpRestore(int Offset) {
894 MipsTargetStreamer::emitDirectiveCpRestore(Offset);
Daniel Sanderse2982ad2015-09-17 16:08:39 +0000895 // .cprestore offset
896 // When PIC mode is enabled and the O32 ABI is used, this directive expands
897 // to:
898 // sw $gp, offset($sp)
899 // and adds a corresponding LW after every JAL.
900
901 // Note that .cprestore is ignored if used with the N32 and N64 ABIs or if it
902 // is used in non-PIC mode.
903 if (!Pic || (getABI().IsN32() || getABI().IsN64()))
904 return;
905
Daniel Sandersc6924fa2016-04-18 12:06:15 +0000906 // FIXME: MipsAsmParser currently emits the instructions that should be
907 // emitted here.
Daniel Sanderse2982ad2015-09-17 16:08:39 +0000908}
909
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000910void MipsTargetELFStreamer::emitDirectiveCpsetup(unsigned RegNo,
911 int RegOrOffset,
912 const MCSymbol &Sym,
913 bool IsReg) {
914 // Only N32 and N64 emit anything for .cpsetup iff PIC is set.
Eric Christophera5762812015-01-26 17:33:46 +0000915 if (!Pic || !(getABI().IsN32() || getABI().IsN64()))
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000916 return;
917
918 MCAssembler &MCA = getStreamer().getAssembler();
919 MCInst Inst;
920
921 // Either store the old $gp in a register or on the stack
922 if (IsReg) {
923 // move $save, $gpreg
Vasileios Kalintiris1c78ca62015-08-11 08:56:25 +0000924 Inst.setOpcode(Mips::OR64);
Jim Grosbache9119e42015-05-13 18:37:00 +0000925 Inst.addOperand(MCOperand::createReg(RegOrOffset));
926 Inst.addOperand(MCOperand::createReg(Mips::GP));
927 Inst.addOperand(MCOperand::createReg(Mips::ZERO));
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000928 } else {
929 // sd $gpreg, offset($sp)
930 Inst.setOpcode(Mips::SD);
Jim Grosbache9119e42015-05-13 18:37:00 +0000931 Inst.addOperand(MCOperand::createReg(Mips::GP));
932 Inst.addOperand(MCOperand::createReg(Mips::SP));
933 Inst.addOperand(MCOperand::createImm(RegOrOffset));
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000934 }
935 getStreamer().EmitInstruction(Inst, STI);
936 Inst.clear();
937
Jim Grosbach13760bd2015-05-30 01:25:56 +0000938 const MCSymbolRefExpr *HiExpr = MCSymbolRefExpr::create(
Toma Tabacu8874eac2015-02-18 13:46:53 +0000939 &Sym, MCSymbolRefExpr::VK_Mips_GPOFF_HI, MCA.getContext());
Jim Grosbach13760bd2015-05-30 01:25:56 +0000940 const MCSymbolRefExpr *LoExpr = MCSymbolRefExpr::create(
Toma Tabacu8874eac2015-02-18 13:46:53 +0000941 &Sym, MCSymbolRefExpr::VK_Mips_GPOFF_LO, MCA.getContext());
942
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000943 // lui $gp, %hi(%neg(%gp_rel(funcSym)))
944 Inst.setOpcode(Mips::LUi);
Jim Grosbache9119e42015-05-13 18:37:00 +0000945 Inst.addOperand(MCOperand::createReg(Mips::GP));
946 Inst.addOperand(MCOperand::createExpr(HiExpr));
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000947 getStreamer().EmitInstruction(Inst, STI);
948 Inst.clear();
949
950 // addiu $gp, $gp, %lo(%neg(%gp_rel(funcSym)))
951 Inst.setOpcode(Mips::ADDiu);
Jim Grosbache9119e42015-05-13 18:37:00 +0000952 Inst.addOperand(MCOperand::createReg(Mips::GP));
953 Inst.addOperand(MCOperand::createReg(Mips::GP));
954 Inst.addOperand(MCOperand::createExpr(LoExpr));
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000955 getStreamer().EmitInstruction(Inst, STI);
956 Inst.clear();
957
958 // daddu $gp, $gp, $funcreg
959 Inst.setOpcode(Mips::DADDu);
Jim Grosbache9119e42015-05-13 18:37:00 +0000960 Inst.addOperand(MCOperand::createReg(Mips::GP));
961 Inst.addOperand(MCOperand::createReg(Mips::GP));
962 Inst.addOperand(MCOperand::createReg(RegNo));
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000963 getStreamer().EmitInstruction(Inst, STI);
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000964
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000965 forbidModuleDirective();
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000966}
967
Daniel Sandersf173dda2015-09-22 10:50:09 +0000968void MipsTargetELFStreamer::emitDirectiveCpreturn(unsigned SaveLocation,
969 bool SaveLocationIsRegister) {
970 // Only N32 and N64 emit anything for .cpreturn iff PIC is set.
971 if (!Pic || !(getABI().IsN32() || getABI().IsN64()))
972 return;
973
974 MCInst Inst;
975 // Either restore the old $gp from a register or on the stack
976 if (SaveLocationIsRegister) {
977 Inst.setOpcode(Mips::OR);
978 Inst.addOperand(MCOperand::createReg(Mips::GP));
979 Inst.addOperand(MCOperand::createReg(SaveLocation));
980 Inst.addOperand(MCOperand::createReg(Mips::ZERO));
981 } else {
982 Inst.setOpcode(Mips::LD);
983 Inst.addOperand(MCOperand::createReg(Mips::GP));
984 Inst.addOperand(MCOperand::createReg(Mips::SP));
985 Inst.addOperand(MCOperand::createImm(SaveLocation));
986 }
987 getStreamer().EmitInstruction(Inst, STI);
988
989 forbidModuleDirective();
990}
991
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000992void MipsTargetELFStreamer::emitMipsAbiFlags() {
993 MCAssembler &MCA = getStreamer().getAssembler();
994 MCContext &Context = MCA.getContext();
995 MCStreamer &OS = getStreamer();
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000996 MCSectionELF *Sec = Context.getELFSection(
Rafael Espindolaba31e272015-01-29 17:33:21 +0000997 ".MIPS.abiflags", ELF::SHT_MIPS_ABIFLAGS, ELF::SHF_ALLOC, 24, "");
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000998 MCA.registerSection(*Sec);
Rafael Espindola967d6a62015-05-21 21:02:35 +0000999 Sec->setAlignment(8);
Vladimir Medicfb8a2a92014-07-08 08:59:22 +00001000 OS.SwitchSection(Sec);
1001
Daniel Sandersc7dbc632014-07-08 10:11:38 +00001002 OS << ABIFlagsSection;
Matheus Almeidad92a3fa2014-05-01 10:24:46 +00001003}