blob: 7f79eb400f5970c4aeba87e43bb5319c8f360a28 [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"
Daniel Sandersfe98b2f2016-05-03 13:35:44 +000017#include "MipsMCExpr.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000018#include "MipsMCTargetDesc.h"
Rafael Espindola972e71a2014-01-31 23:10:26 +000019#include "MipsTargetObjectFile.h"
Rafael Espindola972e71a2014-01-31 23:10:26 +000020#include "llvm/MC/MCContext.h"
Rafael Espindola972e71a2014-01-31 23:10:26 +000021#include "llvm/MC/MCSectionELF.h"
Rafael Espindolacb1953f2014-01-26 06:57:13 +000022#include "llvm/MC/MCSubtargetInfo.h"
Rafael Espindola95fb9b92015-06-02 20:38:46 +000023#include "llvm/MC/MCSymbolELF.h"
Daniel Sandersc07f06a2016-05-04 13:21:06 +000024#include "llvm/Support/CommandLine.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000025#include "llvm/Support/ELF.h"
Jack Carter86ac5c12013-11-18 23:55:27 +000026#include "llvm/Support/ErrorHandling.h"
27#include "llvm/Support/FormattedStream.h"
28
29using namespace llvm;
30
Daniel Sandersc07f06a2016-05-04 13:21:06 +000031namespace {
32static cl::opt<bool> RoundSectionSizes(
33 "mips-round-section-sizes", cl::init(false),
34 cl::desc("Round section sizes up to the section alignment"), cl::Hidden);
35} // end anonymous namespace
36
Vladimir Medicfb8a2a92014-07-08 08:59:22 +000037MipsTargetStreamer::MipsTargetStreamer(MCStreamer &S)
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000038 : MCTargetStreamer(S), ModuleDirectiveAllowed(true) {
Daniel Sandersd97a6342014-08-13 10:07:34 +000039 GPRInfoSet = FPRInfoSet = FrameInfoSet = false;
40}
Rafael Espindola60890b82014-06-23 19:43:40 +000041void MipsTargetStreamer::emitDirectiveSetMicroMips() {}
42void MipsTargetStreamer::emitDirectiveSetNoMicroMips() {}
Daniel Sanderscda908a2016-05-16 09:10:13 +000043void MipsTargetStreamer::setUsesMicroMips() {}
Rafael Espindola60890b82014-06-23 19:43:40 +000044void MipsTargetStreamer::emitDirectiveSetMips16() {}
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000045void MipsTargetStreamer::emitDirectiveSetNoMips16() { forbidModuleDirective(); }
46void MipsTargetStreamer::emitDirectiveSetReorder() { forbidModuleDirective(); }
Rafael Espindola60890b82014-06-23 19:43:40 +000047void MipsTargetStreamer::emitDirectiveSetNoReorder() {}
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000048void MipsTargetStreamer::emitDirectiveSetMacro() { forbidModuleDirective(); }
49void MipsTargetStreamer::emitDirectiveSetNoMacro() { forbidModuleDirective(); }
50void MipsTargetStreamer::emitDirectiveSetMsa() { forbidModuleDirective(); }
51void MipsTargetStreamer::emitDirectiveSetNoMsa() { forbidModuleDirective(); }
52void MipsTargetStreamer::emitDirectiveSetAt() { forbidModuleDirective(); }
Toma Tabacu16a74492015-02-13 10:30:57 +000053void MipsTargetStreamer::emitDirectiveSetAtWithArg(unsigned RegNo) {
54 forbidModuleDirective();
55}
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000056void MipsTargetStreamer::emitDirectiveSetNoAt() { forbidModuleDirective(); }
Rafael Espindola60890b82014-06-23 19:43:40 +000057void MipsTargetStreamer::emitDirectiveEnd(StringRef Name) {}
58void MipsTargetStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {}
59void MipsTargetStreamer::emitDirectiveAbiCalls() {}
60void MipsTargetStreamer::emitDirectiveNaN2008() {}
61void MipsTargetStreamer::emitDirectiveNaNLegacy() {}
62void MipsTargetStreamer::emitDirectiveOptionPic0() {}
63void MipsTargetStreamer::emitDirectiveOptionPic2() {}
Toma Tabacu9ca50962015-04-16 09:53:47 +000064void MipsTargetStreamer::emitDirectiveInsn() { forbidModuleDirective(); }
Rafael Espindola60890b82014-06-23 19:43:40 +000065void MipsTargetStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
66 unsigned ReturnReg) {}
67void MipsTargetStreamer::emitMask(unsigned CPUBitmask, int CPUTopSavedRegOff) {}
68void MipsTargetStreamer::emitFMask(unsigned FPUBitmask, int FPUTopSavedRegOff) {
69}
Toma Tabacu85618b32014-08-19 14:22:52 +000070void MipsTargetStreamer::emitDirectiveSetArch(StringRef Arch) {
71 forbidModuleDirective();
72}
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +000073void MipsTargetStreamer::emitDirectiveSetMips0() { forbidModuleDirective(); }
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000074void MipsTargetStreamer::emitDirectiveSetMips1() { forbidModuleDirective(); }
75void MipsTargetStreamer::emitDirectiveSetMips2() { forbidModuleDirective(); }
76void MipsTargetStreamer::emitDirectiveSetMips3() { forbidModuleDirective(); }
77void MipsTargetStreamer::emitDirectiveSetMips4() { forbidModuleDirective(); }
78void MipsTargetStreamer::emitDirectiveSetMips5() { forbidModuleDirective(); }
79void MipsTargetStreamer::emitDirectiveSetMips32() { forbidModuleDirective(); }
80void MipsTargetStreamer::emitDirectiveSetMips32R2() { forbidModuleDirective(); }
Daniel Sanders17793142015-02-18 16:24:50 +000081void MipsTargetStreamer::emitDirectiveSetMips32R3() { forbidModuleDirective(); }
82void MipsTargetStreamer::emitDirectiveSetMips32R5() { forbidModuleDirective(); }
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000083void MipsTargetStreamer::emitDirectiveSetMips32R6() { forbidModuleDirective(); }
84void MipsTargetStreamer::emitDirectiveSetMips64() { forbidModuleDirective(); }
85void MipsTargetStreamer::emitDirectiveSetMips64R2() { forbidModuleDirective(); }
Daniel Sanders17793142015-02-18 16:24:50 +000086void MipsTargetStreamer::emitDirectiveSetMips64R3() { forbidModuleDirective(); }
87void MipsTargetStreamer::emitDirectiveSetMips64R5() { forbidModuleDirective(); }
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000088void MipsTargetStreamer::emitDirectiveSetMips64R6() { forbidModuleDirective(); }
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +000089void MipsTargetStreamer::emitDirectiveSetPop() { forbidModuleDirective(); }
90void MipsTargetStreamer::emitDirectiveSetPush() { forbidModuleDirective(); }
Toma Tabacu29696502015-06-02 09:48:04 +000091void MipsTargetStreamer::emitDirectiveSetSoftFloat() {
92 forbidModuleDirective();
93}
94void MipsTargetStreamer::emitDirectiveSetHardFloat() {
95 forbidModuleDirective();
96}
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000097void MipsTargetStreamer::emitDirectiveSetDsp() { forbidModuleDirective(); }
Toma Tabacu351b2fe2014-09-17 09:01:54 +000098void MipsTargetStreamer::emitDirectiveSetNoDsp() { forbidModuleDirective(); }
Toma Tabacuc4c202a2014-10-01 14:53:19 +000099void MipsTargetStreamer::emitDirectiveCpLoad(unsigned RegNo) {}
Daniel Sandersdf8510d2016-05-11 12:48:19 +0000100bool MipsTargetStreamer::emitDirectiveCpRestore(
Benjamin Kramerd3f4c052016-06-12 16:13:55 +0000101 int Offset, function_ref<unsigned()> GetATReg, SMLoc IDLoc,
Daniel Sandersdf8510d2016-05-11 12:48:19 +0000102 const MCSubtargetInfo *STI) {
Daniel Sanderse2982ad2015-09-17 16:08:39 +0000103 forbidModuleDirective();
Daniel Sandersdf8510d2016-05-11 12:48:19 +0000104 return true;
Daniel Sanderse2982ad2015-09-17 16:08:39 +0000105}
Rafael Espindola60890b82014-06-23 19:43:40 +0000106void MipsTargetStreamer::emitDirectiveCpsetup(unsigned RegNo, int RegOrOffset,
107 const MCSymbol &Sym, bool IsReg) {
108}
Daniel Sandersf173dda2015-09-22 10:50:09 +0000109void MipsTargetStreamer::emitDirectiveCpreturn(unsigned SaveLocation,
110 bool SaveLocationIsRegister) {}
Toma Tabacubfcbfd52015-06-23 12:34:19 +0000111
Toma Tabacua64e5402015-06-25 12:44:38 +0000112void MipsTargetStreamer::emitDirectiveModuleFP() {}
Toma Tabacubfcbfd52015-06-23 12:34:19 +0000113
Toma Tabacu3c499582015-06-25 10:56:57 +0000114void MipsTargetStreamer::emitDirectiveModuleOddSPReg() {
115 if (!ABIFlagsSection.OddSPReg && !ABIFlagsSection.Is32BitABI)
Daniel Sanders7e527422014-07-10 13:38:23 +0000116 report_fatal_error("+nooddspreg is only valid for O32");
117}
Toma Tabacu0f093132015-06-30 13:46:03 +0000118void MipsTargetStreamer::emitDirectiveModuleSoftFloat() {}
119void MipsTargetStreamer::emitDirectiveModuleHardFloat() {}
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +0000120void MipsTargetStreamer::emitDirectiveSetFp(
121 MipsABIFlagsSection::FpABIKind Value) {
122 forbidModuleDirective();
123}
Toma Tabacu32c72aa2015-06-30 09:36:50 +0000124void MipsTargetStreamer::emitDirectiveSetOddSPReg() { forbidModuleDirective(); }
125void MipsTargetStreamer::emitDirectiveSetNoOddSPReg() {
126 forbidModuleDirective();
127}
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000128
Daniel Sandersa736b372016-04-29 13:33:12 +0000129void MipsTargetStreamer::emitR(unsigned Opcode, unsigned Reg0, SMLoc IDLoc,
130 const MCSubtargetInfo *STI) {
131 MCInst TmpInst;
132 TmpInst.setOpcode(Opcode);
133 TmpInst.addOperand(MCOperand::createReg(Reg0));
134 TmpInst.setLoc(IDLoc);
135 getStreamer().EmitInstruction(TmpInst, *STI);
136}
137
138void MipsTargetStreamer::emitRX(unsigned Opcode, unsigned Reg0, MCOperand Op1,
139 SMLoc IDLoc, const MCSubtargetInfo *STI) {
140 MCInst TmpInst;
141 TmpInst.setOpcode(Opcode);
142 TmpInst.addOperand(MCOperand::createReg(Reg0));
143 TmpInst.addOperand(Op1);
144 TmpInst.setLoc(IDLoc);
145 getStreamer().EmitInstruction(TmpInst, *STI);
146}
147
148void MipsTargetStreamer::emitRI(unsigned Opcode, unsigned Reg0, int32_t Imm,
149 SMLoc IDLoc, const MCSubtargetInfo *STI) {
150 emitRX(Opcode, Reg0, MCOperand::createImm(Imm), IDLoc, STI);
151}
152
153void MipsTargetStreamer::emitRR(unsigned Opcode, unsigned Reg0, unsigned Reg1,
154 SMLoc IDLoc, const MCSubtargetInfo *STI) {
155 emitRX(Opcode, Reg0, MCOperand::createReg(Reg1), IDLoc, STI);
156}
157
158void MipsTargetStreamer::emitII(unsigned Opcode, int16_t Imm1, int16_t Imm2,
159 SMLoc IDLoc, const MCSubtargetInfo *STI) {
160 MCInst TmpInst;
161 TmpInst.setOpcode(Opcode);
162 TmpInst.addOperand(MCOperand::createImm(Imm1));
163 TmpInst.addOperand(MCOperand::createImm(Imm2));
164 TmpInst.setLoc(IDLoc);
165 getStreamer().EmitInstruction(TmpInst, *STI);
166}
167
168void MipsTargetStreamer::emitRRX(unsigned Opcode, unsigned Reg0, unsigned Reg1,
169 MCOperand Op2, SMLoc IDLoc,
170 const MCSubtargetInfo *STI) {
171 MCInst TmpInst;
172 TmpInst.setOpcode(Opcode);
173 TmpInst.addOperand(MCOperand::createReg(Reg0));
174 TmpInst.addOperand(MCOperand::createReg(Reg1));
175 TmpInst.addOperand(Op2);
176 TmpInst.setLoc(IDLoc);
177 getStreamer().EmitInstruction(TmpInst, *STI);
178}
179
180void MipsTargetStreamer::emitRRR(unsigned Opcode, unsigned Reg0, unsigned Reg1,
181 unsigned Reg2, SMLoc IDLoc,
182 const MCSubtargetInfo *STI) {
183 emitRRX(Opcode, Reg0, Reg1, MCOperand::createReg(Reg2), IDLoc, STI);
184}
185
186void MipsTargetStreamer::emitRRI(unsigned Opcode, unsigned Reg0, unsigned Reg1,
187 int16_t Imm, SMLoc IDLoc,
188 const MCSubtargetInfo *STI) {
189 emitRRX(Opcode, Reg0, Reg1, MCOperand::createImm(Imm), IDLoc, STI);
190}
191
192void MipsTargetStreamer::emitAddu(unsigned DstReg, unsigned SrcReg,
193 unsigned TrgReg, bool Is64Bit,
194 const MCSubtargetInfo *STI) {
195 emitRRR(Is64Bit ? Mips::DADDu : Mips::ADDu, DstReg, SrcReg, TrgReg, SMLoc(),
196 STI);
197}
198
199void MipsTargetStreamer::emitDSLL(unsigned DstReg, unsigned SrcReg,
200 int16_t ShiftAmount, SMLoc IDLoc,
201 const MCSubtargetInfo *STI) {
202 if (ShiftAmount >= 32) {
203 emitRRI(Mips::DSLL32, DstReg, SrcReg, ShiftAmount - 32, IDLoc, STI);
204 return;
205 }
206
207 emitRRI(Mips::DSLL, DstReg, SrcReg, ShiftAmount, IDLoc, STI);
208}
209
210void MipsTargetStreamer::emitEmptyDelaySlot(bool hasShortDelaySlot, SMLoc IDLoc,
211 const MCSubtargetInfo *STI) {
212 if (hasShortDelaySlot)
213 emitRR(Mips::MOVE16_MM, Mips::ZERO, Mips::ZERO, IDLoc, STI);
214 else
215 emitRRI(Mips::SLL, Mips::ZERO, Mips::ZERO, 0, IDLoc, STI);
216}
217
218void MipsTargetStreamer::emitNop(SMLoc IDLoc, const MCSubtargetInfo *STI) {
219 emitRRI(Mips::SLL, Mips::ZERO, Mips::ZERO, 0, IDLoc, STI);
220}
221
Daniel Sanders7225cd52016-04-29 16:16:49 +0000222/// Emit the $gp restore operation for .cprestore.
223void MipsTargetStreamer::emitGPRestore(int Offset, SMLoc IDLoc,
224 const MCSubtargetInfo *STI) {
225 emitLoadWithImmOffset(Mips::LW, Mips::GP, Mips::SP, Offset, Mips::GP, IDLoc,
226 STI);
227}
228
229/// Emit a store instruction with an immediate offset.
Daniel Sandersfba875f2016-04-29 13:43:45 +0000230void MipsTargetStreamer::emitStoreWithImmOffset(
231 unsigned Opcode, unsigned SrcReg, unsigned BaseReg, int64_t Offset,
Benjamin Kramerd3f4c052016-06-12 16:13:55 +0000232 function_ref<unsigned()> GetATReg, SMLoc IDLoc,
Daniel Sanders241c6792016-05-12 14:01:50 +0000233 const MCSubtargetInfo *STI) {
Daniel Sanders7225cd52016-04-29 16:16:49 +0000234 if (isInt<16>(Offset)) {
235 emitRRI(Opcode, SrcReg, BaseReg, Offset, IDLoc, STI);
236 return;
237 }
238
Daniel Sandersfba875f2016-04-29 13:43:45 +0000239 // sw $8, offset($8) => lui $at, %hi(offset)
240 // add $at, $at, $8
241 // sw $8, %lo(offset)($at)
242
Daniel Sanders241c6792016-05-12 14:01:50 +0000243 unsigned ATReg = GetATReg();
244 if (!ATReg)
245 return;
246
Daniel Sandersfba875f2016-04-29 13:43:45 +0000247 unsigned LoOffset = Offset & 0x0000ffff;
248 unsigned HiOffset = (Offset & 0xffff0000) >> 16;
249
250 // If msb of LoOffset is 1(negative number) we must increment HiOffset
251 // to account for the sign-extension of the low part.
252 if (LoOffset & 0x8000)
253 HiOffset++;
254
255 // Generate the base address in ATReg.
256 emitRI(Mips::LUi, ATReg, HiOffset, IDLoc, STI);
257 if (BaseReg != Mips::ZERO)
258 emitRRR(Mips::ADDu, ATReg, ATReg, BaseReg, IDLoc, STI);
259 // Emit the store with the adjusted base and offset.
260 emitRRI(Opcode, SrcReg, ATReg, LoOffset, IDLoc, STI);
261}
262
263/// Emit a store instruction with an symbol offset. Symbols are assumed to be
264/// out of range for a simm16 will be expanded to appropriate instructions.
265void MipsTargetStreamer::emitStoreWithSymOffset(
266 unsigned Opcode, unsigned SrcReg, unsigned BaseReg, MCOperand &HiOperand,
267 MCOperand &LoOperand, unsigned ATReg, SMLoc IDLoc,
268 const MCSubtargetInfo *STI) {
269 // sw $8, sym => lui $at, %hi(sym)
270 // sw $8, %lo(sym)($at)
271
272 // Generate the base address in ATReg.
273 emitRX(Mips::LUi, ATReg, HiOperand, IDLoc, STI);
274 if (BaseReg != Mips::ZERO)
275 emitRRR(Mips::ADDu, ATReg, ATReg, BaseReg, IDLoc, STI);
276 // Emit the store with the adjusted base and offset.
277 emitRRX(Opcode, SrcReg, ATReg, LoOperand, IDLoc, STI);
278}
279
Daniel Sanders7225cd52016-04-29 16:16:49 +0000280/// Emit a load instruction with an immediate offset. DstReg and TmpReg are
281/// permitted to be the same register iff DstReg is distinct from BaseReg and
282/// DstReg is a GPR. It is the callers responsibility to identify such cases
283/// and pass the appropriate register in TmpReg.
Daniel Sandersfba875f2016-04-29 13:43:45 +0000284void MipsTargetStreamer::emitLoadWithImmOffset(unsigned Opcode, unsigned DstReg,
285 unsigned BaseReg, int64_t Offset,
286 unsigned TmpReg, SMLoc IDLoc,
287 const MCSubtargetInfo *STI) {
Daniel Sanders7225cd52016-04-29 16:16:49 +0000288 if (isInt<16>(Offset)) {
289 emitRRI(Opcode, DstReg, BaseReg, Offset, IDLoc, STI);
290 return;
291 }
292
Daniel Sandersfba875f2016-04-29 13:43:45 +0000293 // 1) lw $8, offset($9) => lui $8, %hi(offset)
294 // add $8, $8, $9
295 // lw $8, %lo(offset)($9)
296 // 2) lw $8, offset($8) => lui $at, %hi(offset)
297 // add $at, $at, $8
298 // lw $8, %lo(offset)($at)
299
300 unsigned LoOffset = Offset & 0x0000ffff;
301 unsigned HiOffset = (Offset & 0xffff0000) >> 16;
302
303 // If msb of LoOffset is 1(negative number) we must increment HiOffset
304 // to account for the sign-extension of the low part.
305 if (LoOffset & 0x8000)
306 HiOffset++;
307
308 // Generate the base address in TmpReg.
309 emitRI(Mips::LUi, TmpReg, HiOffset, IDLoc, STI);
310 if (BaseReg != Mips::ZERO)
311 emitRRR(Mips::ADDu, TmpReg, TmpReg, BaseReg, IDLoc, STI);
312 // Emit the load with the adjusted base and offset.
313 emitRRI(Opcode, DstReg, TmpReg, LoOffset, IDLoc, STI);
314}
315
316/// Emit a load instruction with an symbol offset. Symbols are assumed to be
317/// out of range for a simm16 will be expanded to appropriate instructions.
318/// DstReg and TmpReg are permitted to be the same register iff DstReg is a
319/// GPR. It is the callers responsibility to identify such cases and pass the
320/// appropriate register in TmpReg.
321void MipsTargetStreamer::emitLoadWithSymOffset(unsigned Opcode, unsigned DstReg,
322 unsigned BaseReg,
323 MCOperand &HiOperand,
324 MCOperand &LoOperand,
325 unsigned TmpReg, SMLoc IDLoc,
326 const MCSubtargetInfo *STI) {
327 // 1) lw $8, sym => lui $8, %hi(sym)
328 // lw $8, %lo(sym)($8)
329 // 2) ldc1 $f0, sym => lui $at, %hi(sym)
330 // ldc1 $f0, %lo(sym)($at)
331
332 // Generate the base address in TmpReg.
333 emitRX(Mips::LUi, TmpReg, HiOperand, IDLoc, STI);
334 if (BaseReg != Mips::ZERO)
335 emitRRR(Mips::ADDu, TmpReg, TmpReg, BaseReg, IDLoc, STI);
336 // Emit the load with the adjusted base and offset.
337 emitRRX(Opcode, DstReg, TmpReg, LoOperand, IDLoc, STI);
338}
339
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000340MipsTargetAsmStreamer::MipsTargetAsmStreamer(MCStreamer &S,
341 formatted_raw_ostream &OS)
342 : MipsTargetStreamer(S), OS(OS) {}
Jack Carter6ef6cc52013-11-19 20:53:28 +0000343
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000344void MipsTargetAsmStreamer::emitDirectiveSetMicroMips() {
345 OS << "\t.set\tmicromips\n";
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000346 forbidModuleDirective();
Jack Carter6ef6cc52013-11-19 20:53:28 +0000347}
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000348
349void MipsTargetAsmStreamer::emitDirectiveSetNoMicroMips() {
350 OS << "\t.set\tnomicromips\n";
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000351 forbidModuleDirective();
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000352}
353
Rafael Espindola6633d572014-01-14 18:57:12 +0000354void MipsTargetAsmStreamer::emitDirectiveSetMips16() {
355 OS << "\t.set\tmips16\n";
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000356 forbidModuleDirective();
Rafael Espindola6633d572014-01-14 18:57:12 +0000357}
358
359void MipsTargetAsmStreamer::emitDirectiveSetNoMips16() {
360 OS << "\t.set\tnomips16\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000361 MipsTargetStreamer::emitDirectiveSetNoMips16();
Rafael Espindola6633d572014-01-14 18:57:12 +0000362}
363
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000364void MipsTargetAsmStreamer::emitDirectiveSetReorder() {
365 OS << "\t.set\treorder\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000366 MipsTargetStreamer::emitDirectiveSetReorder();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000367}
368
369void MipsTargetAsmStreamer::emitDirectiveSetNoReorder() {
370 OS << "\t.set\tnoreorder\n";
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000371 forbidModuleDirective();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000372}
373
374void MipsTargetAsmStreamer::emitDirectiveSetMacro() {
375 OS << "\t.set\tmacro\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000376 MipsTargetStreamer::emitDirectiveSetMacro();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000377}
378
379void MipsTargetAsmStreamer::emitDirectiveSetNoMacro() {
380 OS << "\t.set\tnomacro\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000381 MipsTargetStreamer::emitDirectiveSetNoMacro();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000382}
383
Daniel Sanders44934432014-08-07 12:03:36 +0000384void MipsTargetAsmStreamer::emitDirectiveSetMsa() {
385 OS << "\t.set\tmsa\n";
386 MipsTargetStreamer::emitDirectiveSetMsa();
387}
388
389void MipsTargetAsmStreamer::emitDirectiveSetNoMsa() {
390 OS << "\t.set\tnomsa\n";
391 MipsTargetStreamer::emitDirectiveSetNoMsa();
392}
393
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000394void MipsTargetAsmStreamer::emitDirectiveSetAt() {
395 OS << "\t.set\tat\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000396 MipsTargetStreamer::emitDirectiveSetAt();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000397}
398
Toma Tabacu16a74492015-02-13 10:30:57 +0000399void MipsTargetAsmStreamer::emitDirectiveSetAtWithArg(unsigned RegNo) {
400 OS << "\t.set\tat=$" << Twine(RegNo) << "\n";
401 MipsTargetStreamer::emitDirectiveSetAtWithArg(RegNo);
402}
403
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000404void MipsTargetAsmStreamer::emitDirectiveSetNoAt() {
405 OS << "\t.set\tnoat\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000406 MipsTargetStreamer::emitDirectiveSetNoAt();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000407}
408
409void MipsTargetAsmStreamer::emitDirectiveEnd(StringRef Name) {
410 OS << "\t.end\t" << Name << '\n';
411}
412
Rafael Espindola6633d572014-01-14 18:57:12 +0000413void MipsTargetAsmStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {
414 OS << "\t.ent\t" << Symbol.getName() << '\n';
415}
416
Jack Carter0cd3c192014-01-06 23:27:31 +0000417void MipsTargetAsmStreamer::emitDirectiveAbiCalls() { OS << "\t.abicalls\n"; }
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000418
419void MipsTargetAsmStreamer::emitDirectiveNaN2008() { OS << "\t.nan\t2008\n"; }
420
421void MipsTargetAsmStreamer::emitDirectiveNaNLegacy() {
422 OS << "\t.nan\tlegacy\n";
423}
424
Jack Carter0cd3c192014-01-06 23:27:31 +0000425void MipsTargetAsmStreamer::emitDirectiveOptionPic0() {
426 OS << "\t.option\tpic0\n";
427}
428
Matheus Almeidaf79b2812014-03-26 13:40:29 +0000429void MipsTargetAsmStreamer::emitDirectiveOptionPic2() {
430 OS << "\t.option\tpic2\n";
431}
432
Toma Tabacu9ca50962015-04-16 09:53:47 +0000433void MipsTargetAsmStreamer::emitDirectiveInsn() {
434 MipsTargetStreamer::emitDirectiveInsn();
435 OS << "\t.insn\n";
436}
437
Rafael Espindola054234f2014-01-27 03:53:56 +0000438void MipsTargetAsmStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
439 unsigned ReturnReg) {
440 OS << "\t.frame\t$"
441 << StringRef(MipsInstPrinter::getRegisterName(StackReg)).lower() << ","
442 << StackSize << ",$"
Rafael Espindola25fa2912014-01-27 04:33:11 +0000443 << StringRef(MipsInstPrinter::getRegisterName(ReturnReg)).lower() << '\n';
444}
445
Toma Tabacu85618b32014-08-19 14:22:52 +0000446void MipsTargetAsmStreamer::emitDirectiveSetArch(StringRef Arch) {
447 OS << "\t.set arch=" << Arch << "\n";
448 MipsTargetStreamer::emitDirectiveSetArch(Arch);
449}
450
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +0000451void MipsTargetAsmStreamer::emitDirectiveSetMips0() {
452 OS << "\t.set\tmips0\n";
453 MipsTargetStreamer::emitDirectiveSetMips0();
454}
Toma Tabacu26647792014-09-09 12:52:14 +0000455
Daniel Sandersf0df2212014-08-04 12:20:00 +0000456void MipsTargetAsmStreamer::emitDirectiveSetMips1() {
457 OS << "\t.set\tmips1\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000458 MipsTargetStreamer::emitDirectiveSetMips1();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000459}
460
461void MipsTargetAsmStreamer::emitDirectiveSetMips2() {
462 OS << "\t.set\tmips2\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000463 MipsTargetStreamer::emitDirectiveSetMips2();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000464}
465
466void MipsTargetAsmStreamer::emitDirectiveSetMips3() {
467 OS << "\t.set\tmips3\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000468 MipsTargetStreamer::emitDirectiveSetMips3();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000469}
470
471void MipsTargetAsmStreamer::emitDirectiveSetMips4() {
472 OS << "\t.set\tmips4\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000473 MipsTargetStreamer::emitDirectiveSetMips4();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000474}
475
476void MipsTargetAsmStreamer::emitDirectiveSetMips5() {
477 OS << "\t.set\tmips5\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000478 MipsTargetStreamer::emitDirectiveSetMips5();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000479}
480
481void MipsTargetAsmStreamer::emitDirectiveSetMips32() {
482 OS << "\t.set\tmips32\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000483 MipsTargetStreamer::emitDirectiveSetMips32();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000484}
485
Vladimir Medic615b26e2014-03-04 09:54:09 +0000486void MipsTargetAsmStreamer::emitDirectiveSetMips32R2() {
487 OS << "\t.set\tmips32r2\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000488 MipsTargetStreamer::emitDirectiveSetMips32R2();
Vladimir Medic615b26e2014-03-04 09:54:09 +0000489}
490
Daniel Sanders17793142015-02-18 16:24:50 +0000491void MipsTargetAsmStreamer::emitDirectiveSetMips32R3() {
492 OS << "\t.set\tmips32r3\n";
493 MipsTargetStreamer::emitDirectiveSetMips32R3();
494}
495
496void MipsTargetAsmStreamer::emitDirectiveSetMips32R5() {
497 OS << "\t.set\tmips32r5\n";
498 MipsTargetStreamer::emitDirectiveSetMips32R5();
499}
500
Daniel Sandersf0df2212014-08-04 12:20:00 +0000501void MipsTargetAsmStreamer::emitDirectiveSetMips32R6() {
502 OS << "\t.set\tmips32r6\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000503 MipsTargetStreamer::emitDirectiveSetMips32R6();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000504}
505
Matheus Almeida3b9c63d2014-03-26 15:14:32 +0000506void MipsTargetAsmStreamer::emitDirectiveSetMips64() {
507 OS << "\t.set\tmips64\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000508 MipsTargetStreamer::emitDirectiveSetMips64();
Matheus Almeida3b9c63d2014-03-26 15:14:32 +0000509}
510
Matheus Almeidaa2cd0092014-03-26 14:52:22 +0000511void MipsTargetAsmStreamer::emitDirectiveSetMips64R2() {
512 OS << "\t.set\tmips64r2\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000513 MipsTargetStreamer::emitDirectiveSetMips64R2();
Matheus Almeidaa2cd0092014-03-26 14:52:22 +0000514}
515
Daniel Sanders17793142015-02-18 16:24:50 +0000516void MipsTargetAsmStreamer::emitDirectiveSetMips64R3() {
517 OS << "\t.set\tmips64r3\n";
518 MipsTargetStreamer::emitDirectiveSetMips64R3();
519}
520
521void MipsTargetAsmStreamer::emitDirectiveSetMips64R5() {
522 OS << "\t.set\tmips64r5\n";
523 MipsTargetStreamer::emitDirectiveSetMips64R5();
524}
525
Daniel Sandersf0df2212014-08-04 12:20:00 +0000526void MipsTargetAsmStreamer::emitDirectiveSetMips64R6() {
527 OS << "\t.set\tmips64r6\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000528 MipsTargetStreamer::emitDirectiveSetMips64R6();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000529}
530
Vladimir Medic27c398e2014-03-05 11:05:09 +0000531void MipsTargetAsmStreamer::emitDirectiveSetDsp() {
532 OS << "\t.set\tdsp\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000533 MipsTargetStreamer::emitDirectiveSetDsp();
Vladimir Medic27c398e2014-03-05 11:05:09 +0000534}
Toma Tabacu9db22db2014-09-09 10:15:38 +0000535
Toma Tabacu351b2fe2014-09-17 09:01:54 +0000536void MipsTargetAsmStreamer::emitDirectiveSetNoDsp() {
537 OS << "\t.set\tnodsp\n";
538 MipsTargetStreamer::emitDirectiveSetNoDsp();
539}
540
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +0000541void MipsTargetAsmStreamer::emitDirectiveSetPop() {
542 OS << "\t.set\tpop\n";
543 MipsTargetStreamer::emitDirectiveSetPop();
544}
Toma Tabacu9db22db2014-09-09 10:15:38 +0000545
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +0000546void MipsTargetAsmStreamer::emitDirectiveSetPush() {
547 OS << "\t.set\tpush\n";
548 MipsTargetStreamer::emitDirectiveSetPush();
549}
Toma Tabacu9db22db2014-09-09 10:15:38 +0000550
Toma Tabacu29696502015-06-02 09:48:04 +0000551void MipsTargetAsmStreamer::emitDirectiveSetSoftFloat() {
552 OS << "\t.set\tsoftfloat\n";
553 MipsTargetStreamer::emitDirectiveSetSoftFloat();
554}
555
556void MipsTargetAsmStreamer::emitDirectiveSetHardFloat() {
557 OS << "\t.set\thardfloat\n";
558 MipsTargetStreamer::emitDirectiveSetHardFloat();
559}
560
Rafael Espindola25fa2912014-01-27 04:33:11 +0000561// Print a 32 bit hex number with all numbers.
562static void printHex32(unsigned Value, raw_ostream &OS) {
563 OS << "0x";
564 for (int i = 7; i >= 0; i--)
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000565 OS.write_hex((Value & (0xF << (i * 4))) >> (i * 4));
Rafael Espindola25fa2912014-01-27 04:33:11 +0000566}
567
568void MipsTargetAsmStreamer::emitMask(unsigned CPUBitmask,
569 int CPUTopSavedRegOff) {
570 OS << "\t.mask \t";
571 printHex32(CPUBitmask, OS);
572 OS << ',' << CPUTopSavedRegOff << '\n';
573}
574
575void MipsTargetAsmStreamer::emitFMask(unsigned FPUBitmask,
576 int FPUTopSavedRegOff) {
577 OS << "\t.fmask\t";
578 printHex32(FPUBitmask, OS);
579 OS << "," << FPUTopSavedRegOff << '\n';
Rafael Espindola054234f2014-01-27 03:53:56 +0000580}
581
Toma Tabacuc4c202a2014-10-01 14:53:19 +0000582void MipsTargetAsmStreamer::emitDirectiveCpLoad(unsigned RegNo) {
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000583 OS << "\t.cpload\t$"
584 << StringRef(MipsInstPrinter::getRegisterName(RegNo)).lower() << "\n";
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000585 forbidModuleDirective();
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000586}
587
Daniel Sandersdf8510d2016-05-11 12:48:19 +0000588bool MipsTargetAsmStreamer::emitDirectiveCpRestore(
Benjamin Kramerd3f4c052016-06-12 16:13:55 +0000589 int Offset, function_ref<unsigned()> GetATReg, SMLoc IDLoc,
Daniel Sandersdf8510d2016-05-11 12:48:19 +0000590 const MCSubtargetInfo *STI) {
591 MipsTargetStreamer::emitDirectiveCpRestore(Offset, GetATReg, IDLoc, STI);
Daniel Sanderse2982ad2015-09-17 16:08:39 +0000592 OS << "\t.cprestore\t" << Offset << "\n";
Daniel Sandersdf8510d2016-05-11 12:48:19 +0000593 return true;
Daniel Sanderse2982ad2015-09-17 16:08:39 +0000594}
595
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000596void MipsTargetAsmStreamer::emitDirectiveCpsetup(unsigned RegNo,
597 int RegOrOffset,
598 const MCSymbol &Sym,
599 bool IsReg) {
600 OS << "\t.cpsetup\t$"
601 << StringRef(MipsInstPrinter::getRegisterName(RegNo)).lower() << ", ";
602
603 if (IsReg)
604 OS << "$"
605 << StringRef(MipsInstPrinter::getRegisterName(RegOrOffset)).lower();
606 else
607 OS << RegOrOffset;
608
609 OS << ", ";
610
Daniel Sanders5d796282015-09-21 09:26:55 +0000611 OS << Sym.getName();
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000612 forbidModuleDirective();
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000613}
614
Daniel Sandersf173dda2015-09-22 10:50:09 +0000615void MipsTargetAsmStreamer::emitDirectiveCpreturn(unsigned SaveLocation,
616 bool SaveLocationIsRegister) {
617 OS << "\t.cpreturn";
618 forbidModuleDirective();
619}
620
Toma Tabacua64e5402015-06-25 12:44:38 +0000621void MipsTargetAsmStreamer::emitDirectiveModuleFP() {
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000622 OS << "\t.module\tfp=";
Toma Tabacua64e5402015-06-25 12:44:38 +0000623 OS << ABIFlagsSection.getFpABIString(ABIFlagsSection.getFpABI()) << "\n";
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000624}
625
Daniel Sanders7e527422014-07-10 13:38:23 +0000626void MipsTargetAsmStreamer::emitDirectiveSetFp(
627 MipsABIFlagsSection::FpABIKind Value) {
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +0000628 MipsTargetStreamer::emitDirectiveSetFp(Value);
629
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000630 OS << "\t.set\tfp=";
Daniel Sanders7e527422014-07-10 13:38:23 +0000631 OS << ABIFlagsSection.getFpABIString(Value) << "\n";
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000632}
633
Toma Tabacu3c499582015-06-25 10:56:57 +0000634void MipsTargetAsmStreamer::emitDirectiveModuleOddSPReg() {
635 MipsTargetStreamer::emitDirectiveModuleOddSPReg();
Daniel Sanders7e527422014-07-10 13:38:23 +0000636
Toma Tabacu3c499582015-06-25 10:56:57 +0000637 OS << "\t.module\t" << (ABIFlagsSection.OddSPReg ? "" : "no") << "oddspreg\n";
Daniel Sanders7e527422014-07-10 13:38:23 +0000638}
639
Toma Tabacu32c72aa2015-06-30 09:36:50 +0000640void MipsTargetAsmStreamer::emitDirectiveSetOddSPReg() {
641 MipsTargetStreamer::emitDirectiveSetOddSPReg();
642 OS << "\t.set\toddspreg\n";
643}
644
645void MipsTargetAsmStreamer::emitDirectiveSetNoOddSPReg() {
646 MipsTargetStreamer::emitDirectiveSetNoOddSPReg();
647 OS << "\t.set\tnooddspreg\n";
648}
649
Toma Tabacu0f093132015-06-30 13:46:03 +0000650void MipsTargetAsmStreamer::emitDirectiveModuleSoftFloat() {
651 OS << "\t.module\tsoftfloat\n";
652}
653
654void MipsTargetAsmStreamer::emitDirectiveModuleHardFloat() {
655 OS << "\t.module\thardfloat\n";
656}
657
Jack Carter0cd3c192014-01-06 23:27:31 +0000658// This part is for ELF object output.
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000659MipsTargetELFStreamer::MipsTargetELFStreamer(MCStreamer &S,
660 const MCSubtargetInfo &STI)
Rafael Espindola972e71a2014-01-31 23:10:26 +0000661 : MipsTargetStreamer(S), MicroMipsEnabled(false), STI(STI) {
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000662 MCAssembler &MCA = getStreamer().getAssembler();
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000663
664 // It's possible that MCObjectFileInfo isn't fully initialized at this point
665 // due to an initialization order problem where LLVMTargetMachine creates the
666 // target streamer before TargetLoweringObjectFile calls
667 // InitializeMCObjectFileInfo. There doesn't seem to be a single place that
668 // covers all cases so this statement covers most cases and direct object
669 // emission must call setPic() once MCObjectFileInfo has been initialized. The
670 // cases we don't handle here are covered by MipsAsmPrinter.
Rafael Espindola699281c2016-05-18 11:58:50 +0000671 Pic = MCA.getContext().getObjectFileInfo()->isPositionIndependent();
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000672
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000673 const FeatureBitset &Features = STI.getFeatureBits();
Eric Christophera5762812015-01-26 17:33:46 +0000674
675 // Set the header flags that we can in the constructor.
676 // FIXME: This is a fairly terrible hack. We set the rest
677 // of these in the destructor. The problem here is two-fold:
678 //
679 // a: Some of the eflags can be set/reset by directives.
680 // b: There aren't any usage paths that initialize the ABI
681 // pointer until after we initialize either an assembler
682 // or the target machine.
683 // We can fix this by making the target streamer construct
684 // the ABI, but this is fraught with wide ranging dependency
685 // issues as well.
686 unsigned EFlags = MCA.getELFHeaderEFlags();
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000687
688 // Architecture
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000689 if (Features[Mips::FeatureMips64r6])
Daniel Sanders950f48d2014-07-04 15:21:53 +0000690 EFlags |= ELF::EF_MIPS_ARCH_64R6;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000691 else if (Features[Mips::FeatureMips64r2] ||
692 Features[Mips::FeatureMips64r3] ||
693 Features[Mips::FeatureMips64r5])
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000694 EFlags |= ELF::EF_MIPS_ARCH_64R2;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000695 else if (Features[Mips::FeatureMips64])
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000696 EFlags |= ELF::EF_MIPS_ARCH_64;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000697 else if (Features[Mips::FeatureMips5])
Daniel Sanders950f48d2014-07-04 15:21:53 +0000698 EFlags |= ELF::EF_MIPS_ARCH_5;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000699 else if (Features[Mips::FeatureMips4])
Daniel Sandersf7b32292014-04-03 12:13:36 +0000700 EFlags |= ELF::EF_MIPS_ARCH_4;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000701 else if (Features[Mips::FeatureMips3])
Daniel Sanders950f48d2014-07-04 15:21:53 +0000702 EFlags |= ELF::EF_MIPS_ARCH_3;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000703 else if (Features[Mips::FeatureMips32r6])
Daniel Sanders950f48d2014-07-04 15:21:53 +0000704 EFlags |= ELF::EF_MIPS_ARCH_32R6;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000705 else if (Features[Mips::FeatureMips32r2] ||
706 Features[Mips::FeatureMips32r3] ||
707 Features[Mips::FeatureMips32r5])
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000708 EFlags |= ELF::EF_MIPS_ARCH_32R2;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000709 else if (Features[Mips::FeatureMips32])
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000710 EFlags |= ELF::EF_MIPS_ARCH_32;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000711 else if (Features[Mips::FeatureMips2])
Daniel Sanders950f48d2014-07-04 15:21:53 +0000712 EFlags |= ELF::EF_MIPS_ARCH_2;
713 else
714 EFlags |= ELF::EF_MIPS_ARCH_1;
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000715
Daniel Sanders415c1592016-05-12 11:31:19 +0000716 // Machine
717 if (Features[Mips::FeatureCnMips])
718 EFlags |= ELF::EF_MIPS_MACH_OCTEON;
719
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000720 // Other options.
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000721 if (Features[Mips::FeatureNaN2008])
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000722 EFlags |= ELF::EF_MIPS_NAN2008;
723
Daniel Sanders16ec6c12014-07-17 09:52:56 +0000724 // -mabicalls and -mplt are not implemented but we should act as if they were
725 // given.
726 EFlags |= ELF::EF_MIPS_CPIC;
Daniel Sanders16ec6c12014-07-17 09:52:56 +0000727
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000728 MCA.setELFHeaderEFlags(EFlags);
729}
Jack Carter86ac5c12013-11-18 23:55:27 +0000730
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000731void MipsTargetELFStreamer::emitLabel(MCSymbol *S) {
732 auto *Symbol = cast<MCSymbolELF>(S);
Rafael Espindola26e917c2014-01-15 03:07:12 +0000733 if (!isMicroMipsEnabled())
734 return;
Rafael Espindolac73aed12015-06-03 19:03:11 +0000735 getStreamer().getAssembler().registerSymbol(*Symbol);
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000736 uint8_t Type = Symbol->getType();
Rafael Espindola26e917c2014-01-15 03:07:12 +0000737 if (Type != ELF::STT_FUNC)
738 return;
739
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000740 Symbol->setOther(ELF::STO_MIPS_MICROMIPS);
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000741}
742
Rafael Espindola972e71a2014-01-31 23:10:26 +0000743void MipsTargetELFStreamer::finish() {
744 MCAssembler &MCA = getStreamer().getAssembler();
Daniel Sanders68c37472014-07-21 13:30:55 +0000745 const MCObjectFileInfo &OFI = *MCA.getContext().getObjectFileInfo();
Rafael Espindola972e71a2014-01-31 23:10:26 +0000746
Daniel Sanders41ffa5d2014-07-14 15:05:51 +0000747 // .bss, .text and .data are always at least 16-byte aligned.
Rafael Espindola967d6a62015-05-21 21:02:35 +0000748 MCSection &TextSection = *OFI.getTextSection();
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000749 MCA.registerSection(TextSection);
Rafael Espindola967d6a62015-05-21 21:02:35 +0000750 MCSection &DataSection = *OFI.getDataSection();
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000751 MCA.registerSection(DataSection);
Rafael Espindola967d6a62015-05-21 21:02:35 +0000752 MCSection &BSSSection = *OFI.getBSSSection();
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000753 MCA.registerSection(BSSSection);
Daniel Sanders41ffa5d2014-07-14 15:05:51 +0000754
Rafael Espindola967d6a62015-05-21 21:02:35 +0000755 TextSection.setAlignment(std::max(16u, TextSection.getAlignment()));
756 DataSection.setAlignment(std::max(16u, DataSection.getAlignment()));
757 BSSSection.setAlignment(std::max(16u, BSSSection.getAlignment()));
Daniel Sanders41ffa5d2014-07-14 15:05:51 +0000758
Daniel Sandersc07f06a2016-05-04 13:21:06 +0000759 if (RoundSectionSizes) {
760 // Make sections sizes a multiple of the alignment. This is useful for
761 // verifying the output of IAS against the output of other assemblers but
762 // it's not necessary to produce a correct object and increases section
763 // size.
764 MCStreamer &OS = getStreamer();
765 for (MCSection &S : MCA) {
766 MCSectionELF &Section = static_cast<MCSectionELF &>(S);
Daniel Sanders9db710a2016-04-29 12:44:07 +0000767
Daniel Sandersc07f06a2016-05-04 13:21:06 +0000768 unsigned Alignment = Section.getAlignment();
769 if (Alignment) {
770 OS.SwitchSection(&Section);
771 if (Section.UseCodeAlign())
772 OS.EmitCodeAlignment(Alignment, Alignment);
773 else
774 OS.EmitValueToAlignment(Alignment, 0, 1, Alignment);
775 }
Daniel Sanders9db710a2016-04-29 12:44:07 +0000776 }
777 }
778
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000779 const FeatureBitset &Features = STI.getFeatureBits();
Eric Christophera5762812015-01-26 17:33:46 +0000780
781 // Update e_header flags. See the FIXME and comment above in
782 // the constructor for a full rundown on this.
783 unsigned EFlags = MCA.getELFHeaderEFlags();
784
785 // ABI
786 // N64 does not require any ABI bits.
787 if (getABI().IsO32())
788 EFlags |= ELF::EF_MIPS_ABI_O32;
789 else if (getABI().IsN32())
790 EFlags |= ELF::EF_MIPS_ABI2;
791
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000792 if (Features[Mips::FeatureGP64Bit]) {
Eric Christophera5762812015-01-26 17:33:46 +0000793 if (getABI().IsO32())
794 EFlags |= ELF::EF_MIPS_32BITMODE; /* Compatibility Mode */
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000795 } else if (Features[Mips::FeatureMips64r2] || Features[Mips::FeatureMips64])
Eric Christophera5762812015-01-26 17:33:46 +0000796 EFlags |= ELF::EF_MIPS_32BITMODE;
797
798 // If we've set the cpic eflag and we're n64, go ahead and set the pic
799 // one as well.
800 if (EFlags & ELF::EF_MIPS_CPIC && getABI().IsN64())
801 EFlags |= ELF::EF_MIPS_PIC;
802
803 MCA.setELFHeaderEFlags(EFlags);
804
Daniel Sanders68c37472014-07-21 13:30:55 +0000805 // Emit all the option records.
806 // At the moment we are only emitting .Mips.options (ODK_REGINFO) and
807 // .reginfo.
808 MipsELFStreamer &MEF = static_cast<MipsELFStreamer &>(Streamer);
809 MEF.EmitMipsOptionRecords();
Rafael Espindola972e71a2014-01-31 23:10:26 +0000810
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000811 emitMipsAbiFlags();
Rafael Espindola972e71a2014-01-31 23:10:26 +0000812}
813
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000814void MipsTargetELFStreamer::emitAssignment(MCSymbol *S, const MCExpr *Value) {
815 auto *Symbol = cast<MCSymbolELF>(S);
Zoran Jovanovic28221d82014-03-20 09:44:49 +0000816 // If on rhs is micromips symbol then mark Symbol as microMips.
817 if (Value->getKind() != MCExpr::SymbolRef)
818 return;
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000819 const auto &RhsSym = cast<MCSymbolELF>(
820 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol());
Toma Tabacu2cc44f52015-04-16 13:37:32 +0000821
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000822 if (!(RhsSym.getOther() & ELF::STO_MIPS_MICROMIPS))
Zoran Jovanovic28221d82014-03-20 09:44:49 +0000823 return;
824
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000825 Symbol->setOther(ELF::STO_MIPS_MICROMIPS);
Zoran Jovanovic28221d82014-03-20 09:44:49 +0000826}
827
Jack Carter86ac5c12013-11-18 23:55:27 +0000828MCELFStreamer &MipsTargetELFStreamer::getStreamer() {
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000829 return static_cast<MCELFStreamer &>(Streamer);
Jack Carter86ac5c12013-11-18 23:55:27 +0000830}
831
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000832void MipsTargetELFStreamer::emitDirectiveSetMicroMips() {
833 MicroMipsEnabled = true;
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000834 forbidModuleDirective();
Jack Carter86ac5c12013-11-18 23:55:27 +0000835}
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000836
837void MipsTargetELFStreamer::emitDirectiveSetNoMicroMips() {
838 MicroMipsEnabled = false;
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000839 forbidModuleDirective();
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000840}
841
Daniel Sanderscda908a2016-05-16 09:10:13 +0000842void MipsTargetELFStreamer::setUsesMicroMips() {
843 MCAssembler &MCA = getStreamer().getAssembler();
844 unsigned Flags = MCA.getELFHeaderEFlags();
845 Flags |= ELF::EF_MIPS_MICROMIPS;
846 MCA.setELFHeaderEFlags(Flags);
847}
848
Rafael Espindola6633d572014-01-14 18:57:12 +0000849void MipsTargetELFStreamer::emitDirectiveSetMips16() {
Rafael Espindolae7583752014-01-24 16:13:20 +0000850 MCAssembler &MCA = getStreamer().getAssembler();
851 unsigned Flags = MCA.getELFHeaderEFlags();
852 Flags |= ELF::EF_MIPS_ARCH_ASE_M16;
853 MCA.setELFHeaderEFlags(Flags);
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000854 forbidModuleDirective();
Rafael Espindola6633d572014-01-14 18:57:12 +0000855}
856
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000857void MipsTargetELFStreamer::emitDirectiveSetNoReorder() {
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000858 MCAssembler &MCA = getStreamer().getAssembler();
859 unsigned Flags = MCA.getELFHeaderEFlags();
860 Flags |= ELF::EF_MIPS_NOREORDER;
861 MCA.setELFHeaderEFlags(Flags);
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000862 forbidModuleDirective();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000863}
864
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000865void MipsTargetELFStreamer::emitDirectiveEnd(StringRef Name) {
Daniel Sandersd97a6342014-08-13 10:07:34 +0000866 MCAssembler &MCA = getStreamer().getAssembler();
867 MCContext &Context = MCA.getContext();
868 MCStreamer &OS = getStreamer();
869
Scott Egerton219fae92016-02-17 11:15:16 +0000870 MCSectionELF *Sec = Context.getELFSection(".pdr", ELF::SHT_PROGBITS, 0);
Daniel Sandersd97a6342014-08-13 10:07:34 +0000871
Daniel Sanders2b561332015-11-23 16:08:03 +0000872 MCSymbol *Sym = Context.getOrCreateSymbol(Name);
Daniel Sandersd97a6342014-08-13 10:07:34 +0000873 const MCSymbolRefExpr *ExprRef =
Daniel Sanders2b561332015-11-23 16:08:03 +0000874 MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, Context);
Daniel Sandersd97a6342014-08-13 10:07:34 +0000875
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000876 MCA.registerSection(*Sec);
Rafael Espindola967d6a62015-05-21 21:02:35 +0000877 Sec->setAlignment(4);
Daniel Sandersd97a6342014-08-13 10:07:34 +0000878
879 OS.PushSection();
880
881 OS.SwitchSection(Sec);
882
883 OS.EmitValueImpl(ExprRef, 4);
884
885 OS.EmitIntValue(GPRInfoSet ? GPRBitMask : 0, 4); // reg_mask
886 OS.EmitIntValue(GPRInfoSet ? GPROffset : 0, 4); // reg_offset
887
888 OS.EmitIntValue(FPRInfoSet ? FPRBitMask : 0, 4); // fpreg_mask
889 OS.EmitIntValue(FPRInfoSet ? FPROffset : 0, 4); // fpreg_offset
890
891 OS.EmitIntValue(FrameInfoSet ? FrameOffset : 0, 4); // frame_offset
892 OS.EmitIntValue(FrameInfoSet ? FrameReg : 0, 4); // frame_reg
893 OS.EmitIntValue(FrameInfoSet ? ReturnReg : 0, 4); // return_reg
894
895 // The .end directive marks the end of a procedure. Invalidate
896 // the information gathered up until this point.
897 GPRInfoSet = FPRInfoSet = FrameInfoSet = false;
898
899 OS.PopSection();
Daniel Sanders2b561332015-11-23 16:08:03 +0000900
901 // .end also implicitly sets the size.
902 MCSymbol *CurPCSym = Context.createTempSymbol();
903 OS.EmitLabel(CurPCSym);
904 const MCExpr *Size = MCBinaryExpr::createSub(
905 MCSymbolRefExpr::create(CurPCSym, MCSymbolRefExpr::VK_None, Context),
906 ExprRef, Context);
907 int64_t AbsSize;
908 if (!Size->evaluateAsAbsolute(AbsSize, MCA))
909 llvm_unreachable("Function size must be evaluatable as absolute");
910 Size = MCConstantExpr::create(AbsSize, Context);
911 static_cast<MCSymbolELF *>(Sym)->setSize(Size);
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000912}
913
Rafael Espindola6633d572014-01-14 18:57:12 +0000914void MipsTargetELFStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {
Daniel Sandersd97a6342014-08-13 10:07:34 +0000915 GPRInfoSet = FPRInfoSet = FrameInfoSet = false;
Daniel Sanders2b561332015-11-23 16:08:03 +0000916
917 // .ent also acts like an implicit '.type symbol, STT_FUNC'
918 static_cast<const MCSymbolELF &>(Symbol).setType(ELF::STT_FUNC);
Rafael Espindola6633d572014-01-14 18:57:12 +0000919}
920
Jack Carter0cd3c192014-01-06 23:27:31 +0000921void MipsTargetELFStreamer::emitDirectiveAbiCalls() {
922 MCAssembler &MCA = getStreamer().getAssembler();
923 unsigned Flags = MCA.getELFHeaderEFlags();
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000924 Flags |= ELF::EF_MIPS_CPIC | ELF::EF_MIPS_PIC;
Jack Carter0cd3c192014-01-06 23:27:31 +0000925 MCA.setELFHeaderEFlags(Flags);
926}
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000927
928void MipsTargetELFStreamer::emitDirectiveNaN2008() {
929 MCAssembler &MCA = getStreamer().getAssembler();
930 unsigned Flags = MCA.getELFHeaderEFlags();
931 Flags |= ELF::EF_MIPS_NAN2008;
932 MCA.setELFHeaderEFlags(Flags);
933}
934
935void MipsTargetELFStreamer::emitDirectiveNaNLegacy() {
936 MCAssembler &MCA = getStreamer().getAssembler();
937 unsigned Flags = MCA.getELFHeaderEFlags();
938 Flags &= ~ELF::EF_MIPS_NAN2008;
939 MCA.setELFHeaderEFlags(Flags);
940}
941
Jack Carter0cd3c192014-01-06 23:27:31 +0000942void MipsTargetELFStreamer::emitDirectiveOptionPic0() {
943 MCAssembler &MCA = getStreamer().getAssembler();
944 unsigned Flags = MCA.getELFHeaderEFlags();
Matheus Almeidaf79b2812014-03-26 13:40:29 +0000945 // This option overrides other PIC options like -KPIC.
946 Pic = false;
Jack Carter0cd3c192014-01-06 23:27:31 +0000947 Flags &= ~ELF::EF_MIPS_PIC;
948 MCA.setELFHeaderEFlags(Flags);
949}
Rafael Espindola054234f2014-01-27 03:53:56 +0000950
Matheus Almeidaf79b2812014-03-26 13:40:29 +0000951void MipsTargetELFStreamer::emitDirectiveOptionPic2() {
952 MCAssembler &MCA = getStreamer().getAssembler();
953 unsigned Flags = MCA.getELFHeaderEFlags();
954 Pic = true;
955 // NOTE: We are following the GAS behaviour here which means the directive
956 // 'pic2' also sets the CPIC bit in the ELF header. This is different from
957 // what is stated in the SYSV ABI which consider the bits EF_MIPS_PIC and
958 // EF_MIPS_CPIC to be mutually exclusive.
959 Flags |= ELF::EF_MIPS_PIC | ELF::EF_MIPS_CPIC;
960 MCA.setELFHeaderEFlags(Flags);
961}
962
Toma Tabacu9ca50962015-04-16 09:53:47 +0000963void MipsTargetELFStreamer::emitDirectiveInsn() {
964 MipsTargetStreamer::emitDirectiveInsn();
965 MipsELFStreamer &MEF = static_cast<MipsELFStreamer &>(Streamer);
966 MEF.createPendingLabelRelocs();
967}
968
Rafael Espindola054234f2014-01-27 03:53:56 +0000969void MipsTargetELFStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
Daniel Sandersd97a6342014-08-13 10:07:34 +0000970 unsigned ReturnReg_) {
971 MCContext &Context = getStreamer().getAssembler().getContext();
972 const MCRegisterInfo *RegInfo = Context.getRegisterInfo();
973
974 FrameInfoSet = true;
975 FrameReg = RegInfo->getEncodingValue(StackReg);
976 FrameOffset = StackSize;
977 ReturnReg = RegInfo->getEncodingValue(ReturnReg_);
Rafael Espindola054234f2014-01-27 03:53:56 +0000978}
Rafael Espindola25fa2912014-01-27 04:33:11 +0000979
980void MipsTargetELFStreamer::emitMask(unsigned CPUBitmask,
981 int CPUTopSavedRegOff) {
Daniel Sandersd97a6342014-08-13 10:07:34 +0000982 GPRInfoSet = true;
983 GPRBitMask = CPUBitmask;
984 GPROffset = CPUTopSavedRegOff;
Rafael Espindola25fa2912014-01-27 04:33:11 +0000985}
986
987void MipsTargetELFStreamer::emitFMask(unsigned FPUBitmask,
988 int FPUTopSavedRegOff) {
Daniel Sandersd97a6342014-08-13 10:07:34 +0000989 FPRInfoSet = true;
990 FPRBitMask = FPUBitmask;
991 FPROffset = FPUTopSavedRegOff;
Rafael Espindola25fa2912014-01-27 04:33:11 +0000992}
Vladimir Medic615b26e2014-03-04 09:54:09 +0000993
Toma Tabacuc4c202a2014-10-01 14:53:19 +0000994void MipsTargetELFStreamer::emitDirectiveCpLoad(unsigned RegNo) {
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000995 // .cpload $reg
996 // This directive expands to:
997 // lui $gp, %hi(_gp_disp)
998 // addui $gp, $gp, %lo(_gp_disp)
999 // addu $gp, $gp, $reg
1000 // when support for position independent code is enabled.
Eric Christophera5762812015-01-26 17:33:46 +00001001 if (!Pic || (getABI().IsN32() || getABI().IsN64()))
Matheus Almeida525bc4f2014-04-30 11:28:42 +00001002 return;
1003
1004 // There's a GNU extension controlled by -mno-shared that allows
1005 // locally-binding symbols to be accessed using absolute addresses.
1006 // This is currently not supported. When supported -mno-shared makes
1007 // .cpload expand to:
1008 // lui $gp, %hi(__gnu_local_gp)
1009 // addiu $gp, $gp, %lo(__gnu_local_gp)
1010
1011 StringRef SymName("_gp_disp");
1012 MCAssembler &MCA = getStreamer().getAssembler();
Jim Grosbach6f482002015-05-18 18:43:14 +00001013 MCSymbol *GP_Disp = MCA.getContext().getOrCreateSymbol(SymName);
Rafael Espindolab5d316b2015-05-29 20:21:02 +00001014 MCA.registerSymbol(*GP_Disp);
Matheus Almeida525bc4f2014-04-30 11:28:42 +00001015
1016 MCInst TmpInst;
1017 TmpInst.setOpcode(Mips::LUi);
Jim Grosbache9119e42015-05-13 18:37:00 +00001018 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
Daniel Sandersfe98b2f2016-05-03 13:35:44 +00001019 const MCExpr *HiSym = MipsMCExpr::create(
1020 MipsMCExpr::MEK_HI,
1021 MCSymbolRefExpr::create("_gp_disp", MCSymbolRefExpr::VK_None,
1022 MCA.getContext()),
1023 MCA.getContext());
Jim Grosbache9119e42015-05-13 18:37:00 +00001024 TmpInst.addOperand(MCOperand::createExpr(HiSym));
Matheus Almeida525bc4f2014-04-30 11:28:42 +00001025 getStreamer().EmitInstruction(TmpInst, STI);
1026
1027 TmpInst.clear();
1028
1029 TmpInst.setOpcode(Mips::ADDiu);
Jim Grosbache9119e42015-05-13 18:37:00 +00001030 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
1031 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
Daniel Sandersfe98b2f2016-05-03 13:35:44 +00001032 const MCExpr *LoSym = MipsMCExpr::create(
1033 MipsMCExpr::MEK_LO,
1034 MCSymbolRefExpr::create("_gp_disp", MCSymbolRefExpr::VK_None,
1035 MCA.getContext()),
1036 MCA.getContext());
Jim Grosbache9119e42015-05-13 18:37:00 +00001037 TmpInst.addOperand(MCOperand::createExpr(LoSym));
Matheus Almeida525bc4f2014-04-30 11:28:42 +00001038 getStreamer().EmitInstruction(TmpInst, STI);
1039
1040 TmpInst.clear();
1041
1042 TmpInst.setOpcode(Mips::ADDu);
Jim Grosbache9119e42015-05-13 18:37:00 +00001043 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
1044 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
1045 TmpInst.addOperand(MCOperand::createReg(RegNo));
Matheus Almeida525bc4f2014-04-30 11:28:42 +00001046 getStreamer().EmitInstruction(TmpInst, STI);
Vladimir Medicfb8a2a92014-07-08 08:59:22 +00001047
Daniel Sanderscdb45fa2014-08-14 09:18:14 +00001048 forbidModuleDirective();
Matheus Almeida525bc4f2014-04-30 11:28:42 +00001049}
Matheus Almeidad92a3fa2014-05-01 10:24:46 +00001050
Daniel Sandersdf8510d2016-05-11 12:48:19 +00001051bool MipsTargetELFStreamer::emitDirectiveCpRestore(
Benjamin Kramerd3f4c052016-06-12 16:13:55 +00001052 int Offset, function_ref<unsigned()> GetATReg, SMLoc IDLoc,
Daniel Sandersdf8510d2016-05-11 12:48:19 +00001053 const MCSubtargetInfo *STI) {
1054 MipsTargetStreamer::emitDirectiveCpRestore(Offset, GetATReg, IDLoc, STI);
Daniel Sanderse2982ad2015-09-17 16:08:39 +00001055 // .cprestore offset
1056 // When PIC mode is enabled and the O32 ABI is used, this directive expands
1057 // to:
1058 // sw $gp, offset($sp)
1059 // and adds a corresponding LW after every JAL.
1060
1061 // Note that .cprestore is ignored if used with the N32 and N64 ABIs or if it
1062 // is used in non-PIC mode.
1063 if (!Pic || (getABI().IsN32() || getABI().IsN64()))
Daniel Sandersdf8510d2016-05-11 12:48:19 +00001064 return true;
1065
Daniel Sanders7225cd52016-04-29 16:16:49 +00001066 // Store the $gp on the stack.
Daniel Sanders241c6792016-05-12 14:01:50 +00001067 emitStoreWithImmOffset(Mips::SW, Mips::GP, Mips::SP, Offset, GetATReg, IDLoc,
Daniel Sanders7225cd52016-04-29 16:16:49 +00001068 STI);
Daniel Sandersdf8510d2016-05-11 12:48:19 +00001069 return true;
Daniel Sanderse2982ad2015-09-17 16:08:39 +00001070}
1071
Matheus Almeidad92a3fa2014-05-01 10:24:46 +00001072void MipsTargetELFStreamer::emitDirectiveCpsetup(unsigned RegNo,
1073 int RegOrOffset,
1074 const MCSymbol &Sym,
1075 bool IsReg) {
1076 // Only N32 and N64 emit anything for .cpsetup iff PIC is set.
Eric Christophera5762812015-01-26 17:33:46 +00001077 if (!Pic || !(getABI().IsN32() || getABI().IsN64()))
Matheus Almeidad92a3fa2014-05-01 10:24:46 +00001078 return;
1079
Daniel Sanderse8581362016-06-14 10:13:47 +00001080 forbidModuleDirective();
1081
Matheus Almeidad92a3fa2014-05-01 10:24:46 +00001082 MCAssembler &MCA = getStreamer().getAssembler();
1083 MCInst Inst;
1084
1085 // Either store the old $gp in a register or on the stack
1086 if (IsReg) {
1087 // move $save, $gpreg
Daniel Sanderse8581362016-06-14 10:13:47 +00001088 emitRRR(Mips::OR64, RegOrOffset, Mips::GP, Mips::ZERO, SMLoc(), &STI);
Matheus Almeidad92a3fa2014-05-01 10:24:46 +00001089 } else {
1090 // sd $gpreg, offset($sp)
Daniel Sanderse8581362016-06-14 10:13:47 +00001091 emitRRI(Mips::SD, Mips::GP, Mips::SP, RegOrOffset, SMLoc(), &STI);
Matheus Almeidad92a3fa2014-05-01 10:24:46 +00001092 }
Daniel Sanderse8581362016-06-14 10:13:47 +00001093
1094 if (getABI().IsN32()) {
1095 MCSymbol *GPSym = MCA.getContext().getOrCreateSymbol("__gnu_local_gp");
1096 const MipsMCExpr *HiExpr = MipsMCExpr::create(
1097 MipsMCExpr::MEK_HI, MCSymbolRefExpr::create(GPSym, MCA.getContext()),
1098 MCA.getContext());
1099 const MipsMCExpr *LoExpr = MipsMCExpr::create(
1100 MipsMCExpr::MEK_LO, MCSymbolRefExpr::create(GPSym, MCA.getContext()),
1101 MCA.getContext());
1102
1103 // lui $gp, %hi(__gnu_local_gp)
1104 emitRX(Mips::LUi, Mips::GP, MCOperand::createExpr(HiExpr), SMLoc(), &STI);
1105
1106 // addiu $gp, $gp, %lo(__gnu_local_gp)
1107 emitRRX(Mips::ADDiu, Mips::GP, Mips::GP, MCOperand::createExpr(LoExpr),
1108 SMLoc(), &STI);
1109
1110 return;
1111 }
Matheus Almeidad92a3fa2014-05-01 10:24:46 +00001112
Daniel Sandersfe98b2f2016-05-03 13:35:44 +00001113 const MipsMCExpr *HiExpr = MipsMCExpr::createGpOff(
1114 MipsMCExpr::MEK_HI, MCSymbolRefExpr::create(&Sym, MCA.getContext()),
1115 MCA.getContext());
1116 const MipsMCExpr *LoExpr = MipsMCExpr::createGpOff(
1117 MipsMCExpr::MEK_LO, MCSymbolRefExpr::create(&Sym, MCA.getContext()),
1118 MCA.getContext());
Toma Tabacu8874eac2015-02-18 13:46:53 +00001119
Matheus Almeidad92a3fa2014-05-01 10:24:46 +00001120 // lui $gp, %hi(%neg(%gp_rel(funcSym)))
Daniel Sanderse8581362016-06-14 10:13:47 +00001121 emitRX(Mips::LUi, Mips::GP, MCOperand::createExpr(HiExpr), SMLoc(), &STI);
Matheus Almeidad92a3fa2014-05-01 10:24:46 +00001122
1123 // addiu $gp, $gp, %lo(%neg(%gp_rel(funcSym)))
Daniel Sanderse8581362016-06-14 10:13:47 +00001124 emitRRX(Mips::ADDiu, Mips::GP, Mips::GP, MCOperand::createExpr(LoExpr),
1125 SMLoc(), &STI);
Matheus Almeidad92a3fa2014-05-01 10:24:46 +00001126
1127 // daddu $gp, $gp, $funcreg
Daniel Sanderse8581362016-06-14 10:13:47 +00001128 emitRRR(Mips::DADDu, Mips::GP, Mips::GP, RegNo, SMLoc(), &STI);
Vladimir Medicfb8a2a92014-07-08 08:59:22 +00001129}
1130
Daniel Sandersf173dda2015-09-22 10:50:09 +00001131void MipsTargetELFStreamer::emitDirectiveCpreturn(unsigned SaveLocation,
1132 bool SaveLocationIsRegister) {
1133 // Only N32 and N64 emit anything for .cpreturn iff PIC is set.
1134 if (!Pic || !(getABI().IsN32() || getABI().IsN64()))
1135 return;
1136
1137 MCInst Inst;
1138 // Either restore the old $gp from a register or on the stack
1139 if (SaveLocationIsRegister) {
1140 Inst.setOpcode(Mips::OR);
1141 Inst.addOperand(MCOperand::createReg(Mips::GP));
1142 Inst.addOperand(MCOperand::createReg(SaveLocation));
1143 Inst.addOperand(MCOperand::createReg(Mips::ZERO));
1144 } else {
1145 Inst.setOpcode(Mips::LD);
1146 Inst.addOperand(MCOperand::createReg(Mips::GP));
1147 Inst.addOperand(MCOperand::createReg(Mips::SP));
1148 Inst.addOperand(MCOperand::createImm(SaveLocation));
1149 }
1150 getStreamer().EmitInstruction(Inst, STI);
1151
1152 forbidModuleDirective();
1153}
1154
Vladimir Medicfb8a2a92014-07-08 08:59:22 +00001155void MipsTargetELFStreamer::emitMipsAbiFlags() {
1156 MCAssembler &MCA = getStreamer().getAssembler();
1157 MCContext &Context = MCA.getContext();
1158 MCStreamer &OS = getStreamer();
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001159 MCSectionELF *Sec = Context.getELFSection(
Rafael Espindolaba31e272015-01-29 17:33:21 +00001160 ".MIPS.abiflags", ELF::SHT_MIPS_ABIFLAGS, ELF::SHF_ALLOC, 24, "");
Rafael Espindolabb9a71c2015-05-26 15:07:25 +00001161 MCA.registerSection(*Sec);
Rafael Espindola967d6a62015-05-21 21:02:35 +00001162 Sec->setAlignment(8);
Vladimir Medicfb8a2a92014-07-08 08:59:22 +00001163 OS.SwitchSection(Sec);
1164
Daniel Sandersc7dbc632014-07-08 10:11:38 +00001165 OS << ABIFlagsSection;
Matheus Almeidad92a3fa2014-05-01 10:24:46 +00001166}