blob: 1eb21b6cc8261a1c440a27288a752d1e1783e4d3 [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"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "MCTargetDesc/MipsABIInfo.h"
Daniel Sanders68c37472014-07-21 13:30:55 +000017#include "MipsELFStreamer.h"
Daniel Sandersfe98b2f2016-05-03 13:35:44 +000018#include "MipsMCExpr.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000019#include "MipsMCTargetDesc.h"
Rafael Espindola972e71a2014-01-31 23:10:26 +000020#include "MipsTargetObjectFile.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000021#include "llvm/BinaryFormat/ELF.h"
Rafael Espindola972e71a2014-01-31 23:10:26 +000022#include "llvm/MC/MCContext.h"
Rafael Espindola972e71a2014-01-31 23:10:26 +000023#include "llvm/MC/MCSectionELF.h"
Rafael Espindolacb1953f2014-01-26 06:57:13 +000024#include "llvm/MC/MCSubtargetInfo.h"
Rafael Espindola95fb9b92015-06-02 20:38:46 +000025#include "llvm/MC/MCSymbolELF.h"
Daniel Sandersc07f06a2016-05-04 13:21:06 +000026#include "llvm/Support/CommandLine.h"
Jack Carter86ac5c12013-11-18 23:55:27 +000027#include "llvm/Support/ErrorHandling.h"
28#include "llvm/Support/FormattedStream.h"
29
30using namespace llvm;
31
Daniel Sandersc07f06a2016-05-04 13:21:06 +000032namespace {
33static cl::opt<bool> RoundSectionSizes(
34 "mips-round-section-sizes", cl::init(false),
35 cl::desc("Round section sizes up to the section alignment"), cl::Hidden);
36} // end anonymous namespace
37
Vladimir Medicfb8a2a92014-07-08 08:59:22 +000038MipsTargetStreamer::MipsTargetStreamer(MCStreamer &S)
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000039 : MCTargetStreamer(S), ModuleDirectiveAllowed(true) {
Daniel Sandersd97a6342014-08-13 10:07:34 +000040 GPRInfoSet = FPRInfoSet = FrameInfoSet = false;
41}
Rafael Espindola60890b82014-06-23 19:43:40 +000042void MipsTargetStreamer::emitDirectiveSetMicroMips() {}
43void MipsTargetStreamer::emitDirectiveSetNoMicroMips() {}
Daniel Sanderscda908a2016-05-16 09:10:13 +000044void MipsTargetStreamer::setUsesMicroMips() {}
Rafael Espindola60890b82014-06-23 19:43:40 +000045void MipsTargetStreamer::emitDirectiveSetMips16() {}
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000046void MipsTargetStreamer::emitDirectiveSetNoMips16() { forbidModuleDirective(); }
47void MipsTargetStreamer::emitDirectiveSetReorder() { forbidModuleDirective(); }
Rafael Espindola60890b82014-06-23 19:43:40 +000048void MipsTargetStreamer::emitDirectiveSetNoReorder() {}
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000049void MipsTargetStreamer::emitDirectiveSetMacro() { forbidModuleDirective(); }
50void MipsTargetStreamer::emitDirectiveSetNoMacro() { forbidModuleDirective(); }
51void MipsTargetStreamer::emitDirectiveSetMsa() { forbidModuleDirective(); }
52void MipsTargetStreamer::emitDirectiveSetNoMsa() { forbidModuleDirective(); }
Simon Dardis805f1e02017-07-11 21:28:36 +000053void MipsTargetStreamer::emitDirectiveSetMt() {}
54void MipsTargetStreamer::emitDirectiveSetNoMt() { forbidModuleDirective(); }
Petar Jovanovic3408caf2018-03-14 14:13:31 +000055void MipsTargetStreamer::emitDirectiveSetCRC() {}
56void MipsTargetStreamer::emitDirectiveSetNoCRC() {}
Petar Jovanovicd4349f32018-04-27 09:12:08 +000057void MipsTargetStreamer::emitDirectiveSetVirt() {}
58void MipsTargetStreamer::emitDirectiveSetNoVirt() {}
Petar Jovanovicdaf51692018-05-17 16:30:32 +000059void MipsTargetStreamer::emitDirectiveSetGINV() {}
60void MipsTargetStreamer::emitDirectiveSetNoGINV() {}
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000061void MipsTargetStreamer::emitDirectiveSetAt() { forbidModuleDirective(); }
Toma Tabacu16a74492015-02-13 10:30:57 +000062void MipsTargetStreamer::emitDirectiveSetAtWithArg(unsigned RegNo) {
63 forbidModuleDirective();
64}
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000065void MipsTargetStreamer::emitDirectiveSetNoAt() { forbidModuleDirective(); }
Rafael Espindola60890b82014-06-23 19:43:40 +000066void MipsTargetStreamer::emitDirectiveEnd(StringRef Name) {}
67void MipsTargetStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {}
68void MipsTargetStreamer::emitDirectiveAbiCalls() {}
69void MipsTargetStreamer::emitDirectiveNaN2008() {}
70void MipsTargetStreamer::emitDirectiveNaNLegacy() {}
71void MipsTargetStreamer::emitDirectiveOptionPic0() {}
72void MipsTargetStreamer::emitDirectiveOptionPic2() {}
Toma Tabacu9ca50962015-04-16 09:53:47 +000073void MipsTargetStreamer::emitDirectiveInsn() { forbidModuleDirective(); }
Rafael Espindola60890b82014-06-23 19:43:40 +000074void MipsTargetStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
75 unsigned ReturnReg) {}
76void MipsTargetStreamer::emitMask(unsigned CPUBitmask, int CPUTopSavedRegOff) {}
77void MipsTargetStreamer::emitFMask(unsigned FPUBitmask, int FPUTopSavedRegOff) {
78}
Toma Tabacu85618b32014-08-19 14:22:52 +000079void MipsTargetStreamer::emitDirectiveSetArch(StringRef Arch) {
80 forbidModuleDirective();
81}
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +000082void MipsTargetStreamer::emitDirectiveSetMips0() { forbidModuleDirective(); }
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000083void MipsTargetStreamer::emitDirectiveSetMips1() { forbidModuleDirective(); }
84void MipsTargetStreamer::emitDirectiveSetMips2() { forbidModuleDirective(); }
85void MipsTargetStreamer::emitDirectiveSetMips3() { forbidModuleDirective(); }
86void MipsTargetStreamer::emitDirectiveSetMips4() { forbidModuleDirective(); }
87void MipsTargetStreamer::emitDirectiveSetMips5() { forbidModuleDirective(); }
88void MipsTargetStreamer::emitDirectiveSetMips32() { forbidModuleDirective(); }
89void MipsTargetStreamer::emitDirectiveSetMips32R2() { forbidModuleDirective(); }
Daniel Sanders17793142015-02-18 16:24:50 +000090void MipsTargetStreamer::emitDirectiveSetMips32R3() { forbidModuleDirective(); }
91void MipsTargetStreamer::emitDirectiveSetMips32R5() { forbidModuleDirective(); }
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000092void MipsTargetStreamer::emitDirectiveSetMips32R6() { forbidModuleDirective(); }
93void MipsTargetStreamer::emitDirectiveSetMips64() { forbidModuleDirective(); }
94void MipsTargetStreamer::emitDirectiveSetMips64R2() { forbidModuleDirective(); }
Daniel Sanders17793142015-02-18 16:24:50 +000095void MipsTargetStreamer::emitDirectiveSetMips64R3() { forbidModuleDirective(); }
96void MipsTargetStreamer::emitDirectiveSetMips64R5() { forbidModuleDirective(); }
Daniel Sanderscdb45fa2014-08-14 09:18:14 +000097void MipsTargetStreamer::emitDirectiveSetMips64R6() { forbidModuleDirective(); }
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +000098void MipsTargetStreamer::emitDirectiveSetPop() { forbidModuleDirective(); }
99void MipsTargetStreamer::emitDirectiveSetPush() { forbidModuleDirective(); }
Toma Tabacu29696502015-06-02 09:48:04 +0000100void MipsTargetStreamer::emitDirectiveSetSoftFloat() {
101 forbidModuleDirective();
102}
103void MipsTargetStreamer::emitDirectiveSetHardFloat() {
104 forbidModuleDirective();
105}
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000106void MipsTargetStreamer::emitDirectiveSetDsp() { forbidModuleDirective(); }
Petar Jovanovic65f10242017-10-05 17:40:32 +0000107void MipsTargetStreamer::emitDirectiveSetDspr2() { forbidModuleDirective(); }
Toma Tabacu351b2fe2014-09-17 09:01:54 +0000108void MipsTargetStreamer::emitDirectiveSetNoDsp() { forbidModuleDirective(); }
Toma Tabacuc4c202a2014-10-01 14:53:19 +0000109void MipsTargetStreamer::emitDirectiveCpLoad(unsigned RegNo) {}
Daniel Sandersdf8510d2016-05-11 12:48:19 +0000110bool MipsTargetStreamer::emitDirectiveCpRestore(
Benjamin Kramerd3f4c052016-06-12 16:13:55 +0000111 int Offset, function_ref<unsigned()> GetATReg, SMLoc IDLoc,
Daniel Sandersdf8510d2016-05-11 12:48:19 +0000112 const MCSubtargetInfo *STI) {
Daniel Sanderse2982ad2015-09-17 16:08:39 +0000113 forbidModuleDirective();
Daniel Sandersdf8510d2016-05-11 12:48:19 +0000114 return true;
Daniel Sanderse2982ad2015-09-17 16:08:39 +0000115}
Rafael Espindola60890b82014-06-23 19:43:40 +0000116void MipsTargetStreamer::emitDirectiveCpsetup(unsigned RegNo, int RegOrOffset,
117 const MCSymbol &Sym, bool IsReg) {
118}
Daniel Sandersf173dda2015-09-22 10:50:09 +0000119void MipsTargetStreamer::emitDirectiveCpreturn(unsigned SaveLocation,
120 bool SaveLocationIsRegister) {}
Toma Tabacubfcbfd52015-06-23 12:34:19 +0000121
Toma Tabacua64e5402015-06-25 12:44:38 +0000122void MipsTargetStreamer::emitDirectiveModuleFP() {}
Toma Tabacubfcbfd52015-06-23 12:34:19 +0000123
Toma Tabacu3c499582015-06-25 10:56:57 +0000124void MipsTargetStreamer::emitDirectiveModuleOddSPReg() {
125 if (!ABIFlagsSection.OddSPReg && !ABIFlagsSection.Is32BitABI)
Daniel Sanders7e527422014-07-10 13:38:23 +0000126 report_fatal_error("+nooddspreg is only valid for O32");
127}
Toma Tabacu0f093132015-06-30 13:46:03 +0000128void MipsTargetStreamer::emitDirectiveModuleSoftFloat() {}
129void MipsTargetStreamer::emitDirectiveModuleHardFloat() {}
Simon Dardis805f1e02017-07-11 21:28:36 +0000130void MipsTargetStreamer::emitDirectiveModuleMT() {}
Petar Jovanovic3408caf2018-03-14 14:13:31 +0000131void MipsTargetStreamer::emitDirectiveModuleCRC() {}
132void MipsTargetStreamer::emitDirectiveModuleNoCRC() {}
Petar Jovanovicd4349f32018-04-27 09:12:08 +0000133void MipsTargetStreamer::emitDirectiveModuleVirt() {}
134void MipsTargetStreamer::emitDirectiveModuleNoVirt() {}
Petar Jovanovicdaf51692018-05-17 16:30:32 +0000135void MipsTargetStreamer::emitDirectiveModuleGINV() {}
136void MipsTargetStreamer::emitDirectiveModuleNoGINV() {}
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +0000137void MipsTargetStreamer::emitDirectiveSetFp(
138 MipsABIFlagsSection::FpABIKind Value) {
139 forbidModuleDirective();
140}
Toma Tabacu32c72aa2015-06-30 09:36:50 +0000141void MipsTargetStreamer::emitDirectiveSetOddSPReg() { forbidModuleDirective(); }
142void MipsTargetStreamer::emitDirectiveSetNoOddSPReg() {
143 forbidModuleDirective();
144}
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000145
Daniel Sandersa736b372016-04-29 13:33:12 +0000146void MipsTargetStreamer::emitR(unsigned Opcode, unsigned Reg0, SMLoc IDLoc,
147 const MCSubtargetInfo *STI) {
148 MCInst TmpInst;
149 TmpInst.setOpcode(Opcode);
150 TmpInst.addOperand(MCOperand::createReg(Reg0));
151 TmpInst.setLoc(IDLoc);
152 getStreamer().EmitInstruction(TmpInst, *STI);
153}
154
155void MipsTargetStreamer::emitRX(unsigned Opcode, unsigned Reg0, MCOperand Op1,
156 SMLoc IDLoc, const MCSubtargetInfo *STI) {
157 MCInst TmpInst;
158 TmpInst.setOpcode(Opcode);
159 TmpInst.addOperand(MCOperand::createReg(Reg0));
160 TmpInst.addOperand(Op1);
161 TmpInst.setLoc(IDLoc);
162 getStreamer().EmitInstruction(TmpInst, *STI);
163}
164
165void MipsTargetStreamer::emitRI(unsigned Opcode, unsigned Reg0, int32_t Imm,
166 SMLoc IDLoc, const MCSubtargetInfo *STI) {
167 emitRX(Opcode, Reg0, MCOperand::createImm(Imm), IDLoc, STI);
168}
169
170void MipsTargetStreamer::emitRR(unsigned Opcode, unsigned Reg0, unsigned Reg1,
171 SMLoc IDLoc, const MCSubtargetInfo *STI) {
172 emitRX(Opcode, Reg0, MCOperand::createReg(Reg1), IDLoc, STI);
173}
174
175void MipsTargetStreamer::emitII(unsigned Opcode, int16_t Imm1, int16_t Imm2,
176 SMLoc IDLoc, const MCSubtargetInfo *STI) {
177 MCInst TmpInst;
178 TmpInst.setOpcode(Opcode);
179 TmpInst.addOperand(MCOperand::createImm(Imm1));
180 TmpInst.addOperand(MCOperand::createImm(Imm2));
181 TmpInst.setLoc(IDLoc);
182 getStreamer().EmitInstruction(TmpInst, *STI);
183}
184
185void MipsTargetStreamer::emitRRX(unsigned Opcode, unsigned Reg0, unsigned Reg1,
186 MCOperand Op2, SMLoc IDLoc,
187 const MCSubtargetInfo *STI) {
188 MCInst TmpInst;
189 TmpInst.setOpcode(Opcode);
190 TmpInst.addOperand(MCOperand::createReg(Reg0));
191 TmpInst.addOperand(MCOperand::createReg(Reg1));
192 TmpInst.addOperand(Op2);
193 TmpInst.setLoc(IDLoc);
194 getStreamer().EmitInstruction(TmpInst, *STI);
195}
196
197void MipsTargetStreamer::emitRRR(unsigned Opcode, unsigned Reg0, unsigned Reg1,
198 unsigned Reg2, SMLoc IDLoc,
199 const MCSubtargetInfo *STI) {
200 emitRRX(Opcode, Reg0, Reg1, MCOperand::createReg(Reg2), IDLoc, STI);
201}
202
203void MipsTargetStreamer::emitRRI(unsigned Opcode, unsigned Reg0, unsigned Reg1,
204 int16_t Imm, SMLoc IDLoc,
205 const MCSubtargetInfo *STI) {
206 emitRRX(Opcode, Reg0, Reg1, MCOperand::createImm(Imm), IDLoc, STI);
207}
208
Simon Dardisde5ed0c2017-11-14 22:26:42 +0000209void MipsTargetStreamer::emitRRIII(unsigned Opcode, unsigned Reg0,
210 unsigned Reg1, int16_t Imm0, int16_t Imm1,
211 int16_t Imm2, SMLoc IDLoc,
212 const MCSubtargetInfo *STI) {
213 MCInst TmpInst;
214 TmpInst.setOpcode(Opcode);
215 TmpInst.addOperand(MCOperand::createReg(Reg0));
216 TmpInst.addOperand(MCOperand::createReg(Reg1));
217 TmpInst.addOperand(MCOperand::createImm(Imm0));
218 TmpInst.addOperand(MCOperand::createImm(Imm1));
219 TmpInst.addOperand(MCOperand::createImm(Imm2));
220 TmpInst.setLoc(IDLoc);
221 getStreamer().EmitInstruction(TmpInst, *STI);
222}
223
Daniel Sandersa736b372016-04-29 13:33:12 +0000224void MipsTargetStreamer::emitAddu(unsigned DstReg, unsigned SrcReg,
225 unsigned TrgReg, bool Is64Bit,
226 const MCSubtargetInfo *STI) {
227 emitRRR(Is64Bit ? Mips::DADDu : Mips::ADDu, DstReg, SrcReg, TrgReg, SMLoc(),
228 STI);
229}
230
231void MipsTargetStreamer::emitDSLL(unsigned DstReg, unsigned SrcReg,
232 int16_t ShiftAmount, SMLoc IDLoc,
233 const MCSubtargetInfo *STI) {
234 if (ShiftAmount >= 32) {
235 emitRRI(Mips::DSLL32, DstReg, SrcReg, ShiftAmount - 32, IDLoc, STI);
236 return;
237 }
238
239 emitRRI(Mips::DSLL, DstReg, SrcReg, ShiftAmount, IDLoc, STI);
240}
241
242void MipsTargetStreamer::emitEmptyDelaySlot(bool hasShortDelaySlot, SMLoc IDLoc,
243 const MCSubtargetInfo *STI) {
244 if (hasShortDelaySlot)
245 emitRR(Mips::MOVE16_MM, Mips::ZERO, Mips::ZERO, IDLoc, STI);
246 else
247 emitRRI(Mips::SLL, Mips::ZERO, Mips::ZERO, 0, IDLoc, STI);
248}
249
250void MipsTargetStreamer::emitNop(SMLoc IDLoc, const MCSubtargetInfo *STI) {
251 emitRRI(Mips::SLL, Mips::ZERO, Mips::ZERO, 0, IDLoc, STI);
252}
253
Daniel Sanders7225cd52016-04-29 16:16:49 +0000254/// Emit the $gp restore operation for .cprestore.
255void MipsTargetStreamer::emitGPRestore(int Offset, SMLoc IDLoc,
256 const MCSubtargetInfo *STI) {
257 emitLoadWithImmOffset(Mips::LW, Mips::GP, Mips::SP, Offset, Mips::GP, IDLoc,
258 STI);
259}
260
261/// Emit a store instruction with an immediate offset.
Daniel Sandersfba875f2016-04-29 13:43:45 +0000262void MipsTargetStreamer::emitStoreWithImmOffset(
263 unsigned Opcode, unsigned SrcReg, unsigned BaseReg, int64_t Offset,
Benjamin Kramerd3f4c052016-06-12 16:13:55 +0000264 function_ref<unsigned()> GetATReg, SMLoc IDLoc,
Daniel Sanders241c6792016-05-12 14:01:50 +0000265 const MCSubtargetInfo *STI) {
Daniel Sanders7225cd52016-04-29 16:16:49 +0000266 if (isInt<16>(Offset)) {
267 emitRRI(Opcode, SrcReg, BaseReg, Offset, IDLoc, STI);
268 return;
269 }
270
Daniel Sandersfba875f2016-04-29 13:43:45 +0000271 // sw $8, offset($8) => lui $at, %hi(offset)
272 // add $at, $at, $8
273 // sw $8, %lo(offset)($at)
274
Daniel Sanders241c6792016-05-12 14:01:50 +0000275 unsigned ATReg = GetATReg();
276 if (!ATReg)
277 return;
278
Daniel Sandersfba875f2016-04-29 13:43:45 +0000279 unsigned LoOffset = Offset & 0x0000ffff;
280 unsigned HiOffset = (Offset & 0xffff0000) >> 16;
281
282 // If msb of LoOffset is 1(negative number) we must increment HiOffset
283 // to account for the sign-extension of the low part.
284 if (LoOffset & 0x8000)
285 HiOffset++;
286
287 // Generate the base address in ATReg.
288 emitRI(Mips::LUi, ATReg, HiOffset, IDLoc, STI);
289 if (BaseReg != Mips::ZERO)
290 emitRRR(Mips::ADDu, ATReg, ATReg, BaseReg, IDLoc, STI);
291 // Emit the store with the adjusted base and offset.
292 emitRRI(Opcode, SrcReg, ATReg, LoOffset, IDLoc, STI);
293}
294
295/// Emit a store instruction with an symbol offset. Symbols are assumed to be
296/// out of range for a simm16 will be expanded to appropriate instructions.
297void MipsTargetStreamer::emitStoreWithSymOffset(
298 unsigned Opcode, unsigned SrcReg, unsigned BaseReg, MCOperand &HiOperand,
299 MCOperand &LoOperand, unsigned ATReg, SMLoc IDLoc,
300 const MCSubtargetInfo *STI) {
301 // sw $8, sym => lui $at, %hi(sym)
302 // sw $8, %lo(sym)($at)
303
304 // Generate the base address in ATReg.
305 emitRX(Mips::LUi, ATReg, HiOperand, IDLoc, STI);
306 if (BaseReg != Mips::ZERO)
307 emitRRR(Mips::ADDu, ATReg, ATReg, BaseReg, IDLoc, STI);
308 // Emit the store with the adjusted base and offset.
309 emitRRX(Opcode, SrcReg, ATReg, LoOperand, IDLoc, STI);
310}
311
Daniel Sanders7225cd52016-04-29 16:16:49 +0000312/// Emit a load instruction with an immediate offset. DstReg and TmpReg are
313/// permitted to be the same register iff DstReg is distinct from BaseReg and
314/// DstReg is a GPR. It is the callers responsibility to identify such cases
315/// and pass the appropriate register in TmpReg.
Daniel Sandersfba875f2016-04-29 13:43:45 +0000316void MipsTargetStreamer::emitLoadWithImmOffset(unsigned Opcode, unsigned DstReg,
317 unsigned BaseReg, int64_t Offset,
318 unsigned TmpReg, SMLoc IDLoc,
319 const MCSubtargetInfo *STI) {
Daniel Sanders7225cd52016-04-29 16:16:49 +0000320 if (isInt<16>(Offset)) {
321 emitRRI(Opcode, DstReg, BaseReg, Offset, IDLoc, STI);
322 return;
323 }
324
Daniel Sandersfba875f2016-04-29 13:43:45 +0000325 // 1) lw $8, offset($9) => lui $8, %hi(offset)
326 // add $8, $8, $9
327 // lw $8, %lo(offset)($9)
328 // 2) lw $8, offset($8) => lui $at, %hi(offset)
329 // add $at, $at, $8
330 // lw $8, %lo(offset)($at)
331
332 unsigned LoOffset = Offset & 0x0000ffff;
333 unsigned HiOffset = (Offset & 0xffff0000) >> 16;
334
335 // If msb of LoOffset is 1(negative number) we must increment HiOffset
336 // to account for the sign-extension of the low part.
337 if (LoOffset & 0x8000)
338 HiOffset++;
339
340 // Generate the base address in TmpReg.
341 emitRI(Mips::LUi, TmpReg, HiOffset, IDLoc, STI);
342 if (BaseReg != Mips::ZERO)
343 emitRRR(Mips::ADDu, TmpReg, TmpReg, BaseReg, IDLoc, STI);
344 // Emit the load with the adjusted base and offset.
345 emitRRI(Opcode, DstReg, TmpReg, LoOffset, IDLoc, STI);
346}
347
348/// Emit a load instruction with an symbol offset. Symbols are assumed to be
349/// out of range for a simm16 will be expanded to appropriate instructions.
350/// DstReg and TmpReg are permitted to be the same register iff DstReg is a
351/// GPR. It is the callers responsibility to identify such cases and pass the
352/// appropriate register in TmpReg.
353void MipsTargetStreamer::emitLoadWithSymOffset(unsigned Opcode, unsigned DstReg,
354 unsigned BaseReg,
355 MCOperand &HiOperand,
356 MCOperand &LoOperand,
357 unsigned TmpReg, SMLoc IDLoc,
358 const MCSubtargetInfo *STI) {
359 // 1) lw $8, sym => lui $8, %hi(sym)
360 // lw $8, %lo(sym)($8)
361 // 2) ldc1 $f0, sym => lui $at, %hi(sym)
362 // ldc1 $f0, %lo(sym)($at)
363
364 // Generate the base address in TmpReg.
365 emitRX(Mips::LUi, TmpReg, HiOperand, IDLoc, STI);
366 if (BaseReg != Mips::ZERO)
367 emitRRR(Mips::ADDu, TmpReg, TmpReg, BaseReg, IDLoc, STI);
368 // Emit the load with the adjusted base and offset.
369 emitRRX(Opcode, DstReg, TmpReg, LoOperand, IDLoc, STI);
370}
371
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000372MipsTargetAsmStreamer::MipsTargetAsmStreamer(MCStreamer &S,
373 formatted_raw_ostream &OS)
374 : MipsTargetStreamer(S), OS(OS) {}
Jack Carter6ef6cc52013-11-19 20:53:28 +0000375
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000376void MipsTargetAsmStreamer::emitDirectiveSetMicroMips() {
377 OS << "\t.set\tmicromips\n";
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000378 forbidModuleDirective();
Jack Carter6ef6cc52013-11-19 20:53:28 +0000379}
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000380
381void MipsTargetAsmStreamer::emitDirectiveSetNoMicroMips() {
382 OS << "\t.set\tnomicromips\n";
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000383 forbidModuleDirective();
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000384}
385
Rafael Espindola6633d572014-01-14 18:57:12 +0000386void MipsTargetAsmStreamer::emitDirectiveSetMips16() {
387 OS << "\t.set\tmips16\n";
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000388 forbidModuleDirective();
Rafael Espindola6633d572014-01-14 18:57:12 +0000389}
390
391void MipsTargetAsmStreamer::emitDirectiveSetNoMips16() {
392 OS << "\t.set\tnomips16\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000393 MipsTargetStreamer::emitDirectiveSetNoMips16();
Rafael Espindola6633d572014-01-14 18:57:12 +0000394}
395
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000396void MipsTargetAsmStreamer::emitDirectiveSetReorder() {
397 OS << "\t.set\treorder\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000398 MipsTargetStreamer::emitDirectiveSetReorder();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000399}
400
401void MipsTargetAsmStreamer::emitDirectiveSetNoReorder() {
402 OS << "\t.set\tnoreorder\n";
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000403 forbidModuleDirective();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000404}
405
406void MipsTargetAsmStreamer::emitDirectiveSetMacro() {
407 OS << "\t.set\tmacro\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000408 MipsTargetStreamer::emitDirectiveSetMacro();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000409}
410
411void MipsTargetAsmStreamer::emitDirectiveSetNoMacro() {
412 OS << "\t.set\tnomacro\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000413 MipsTargetStreamer::emitDirectiveSetNoMacro();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000414}
415
Daniel Sanders44934432014-08-07 12:03:36 +0000416void MipsTargetAsmStreamer::emitDirectiveSetMsa() {
417 OS << "\t.set\tmsa\n";
418 MipsTargetStreamer::emitDirectiveSetMsa();
419}
420
421void MipsTargetAsmStreamer::emitDirectiveSetNoMsa() {
422 OS << "\t.set\tnomsa\n";
423 MipsTargetStreamer::emitDirectiveSetNoMsa();
424}
425
Simon Dardis805f1e02017-07-11 21:28:36 +0000426void MipsTargetAsmStreamer::emitDirectiveSetMt() {
427 OS << "\t.set\tmt\n";
428 MipsTargetStreamer::emitDirectiveSetMt();
429}
430
431void MipsTargetAsmStreamer::emitDirectiveSetNoMt() {
432 OS << "\t.set\tnomt\n";
433 MipsTargetStreamer::emitDirectiveSetNoMt();
434}
435
Petar Jovanovic3408caf2018-03-14 14:13:31 +0000436void MipsTargetAsmStreamer::emitDirectiveSetCRC() {
437 OS << "\t.set\tcrc\n";
438 MipsTargetStreamer::emitDirectiveSetCRC();
439}
440
441void MipsTargetAsmStreamer::emitDirectiveSetNoCRC() {
442 OS << "\t.set\tnocrc\n";
443 MipsTargetStreamer::emitDirectiveSetNoCRC();
444}
445
Petar Jovanovicd4349f32018-04-27 09:12:08 +0000446void MipsTargetAsmStreamer::emitDirectiveSetVirt() {
447 OS << "\t.set\tvirt\n";
448 MipsTargetStreamer::emitDirectiveSetVirt();
449}
450
451void MipsTargetAsmStreamer::emitDirectiveSetNoVirt() {
452 OS << "\t.set\tnovirt\n";
453 MipsTargetStreamer::emitDirectiveSetNoVirt();
454}
455
Petar Jovanovicdaf51692018-05-17 16:30:32 +0000456void MipsTargetAsmStreamer::emitDirectiveSetGINV() {
457 OS << "\t.set\tginv\n";
458 MipsTargetStreamer::emitDirectiveSetGINV();
459}
460
461void MipsTargetAsmStreamer::emitDirectiveSetNoGINV() {
462 OS << "\t.set\tnoginv\n";
463 MipsTargetStreamer::emitDirectiveSetNoGINV();
464}
465
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000466void MipsTargetAsmStreamer::emitDirectiveSetAt() {
467 OS << "\t.set\tat\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000468 MipsTargetStreamer::emitDirectiveSetAt();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000469}
470
Toma Tabacu16a74492015-02-13 10:30:57 +0000471void MipsTargetAsmStreamer::emitDirectiveSetAtWithArg(unsigned RegNo) {
472 OS << "\t.set\tat=$" << Twine(RegNo) << "\n";
473 MipsTargetStreamer::emitDirectiveSetAtWithArg(RegNo);
474}
475
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000476void MipsTargetAsmStreamer::emitDirectiveSetNoAt() {
477 OS << "\t.set\tnoat\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000478 MipsTargetStreamer::emitDirectiveSetNoAt();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000479}
480
481void MipsTargetAsmStreamer::emitDirectiveEnd(StringRef Name) {
482 OS << "\t.end\t" << Name << '\n';
483}
484
Rafael Espindola6633d572014-01-14 18:57:12 +0000485void MipsTargetAsmStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {
486 OS << "\t.ent\t" << Symbol.getName() << '\n';
487}
488
Jack Carter0cd3c192014-01-06 23:27:31 +0000489void MipsTargetAsmStreamer::emitDirectiveAbiCalls() { OS << "\t.abicalls\n"; }
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000490
491void MipsTargetAsmStreamer::emitDirectiveNaN2008() { OS << "\t.nan\t2008\n"; }
492
493void MipsTargetAsmStreamer::emitDirectiveNaNLegacy() {
494 OS << "\t.nan\tlegacy\n";
495}
496
Jack Carter0cd3c192014-01-06 23:27:31 +0000497void MipsTargetAsmStreamer::emitDirectiveOptionPic0() {
498 OS << "\t.option\tpic0\n";
499}
500
Matheus Almeidaf79b2812014-03-26 13:40:29 +0000501void MipsTargetAsmStreamer::emitDirectiveOptionPic2() {
502 OS << "\t.option\tpic2\n";
503}
504
Toma Tabacu9ca50962015-04-16 09:53:47 +0000505void MipsTargetAsmStreamer::emitDirectiveInsn() {
506 MipsTargetStreamer::emitDirectiveInsn();
507 OS << "\t.insn\n";
508}
509
Rafael Espindola054234f2014-01-27 03:53:56 +0000510void MipsTargetAsmStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
511 unsigned ReturnReg) {
512 OS << "\t.frame\t$"
513 << StringRef(MipsInstPrinter::getRegisterName(StackReg)).lower() << ","
514 << StackSize << ",$"
Rafael Espindola25fa2912014-01-27 04:33:11 +0000515 << StringRef(MipsInstPrinter::getRegisterName(ReturnReg)).lower() << '\n';
516}
517
Toma Tabacu85618b32014-08-19 14:22:52 +0000518void MipsTargetAsmStreamer::emitDirectiveSetArch(StringRef Arch) {
519 OS << "\t.set arch=" << Arch << "\n";
520 MipsTargetStreamer::emitDirectiveSetArch(Arch);
521}
522
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +0000523void MipsTargetAsmStreamer::emitDirectiveSetMips0() {
524 OS << "\t.set\tmips0\n";
525 MipsTargetStreamer::emitDirectiveSetMips0();
526}
Toma Tabacu26647792014-09-09 12:52:14 +0000527
Daniel Sandersf0df2212014-08-04 12:20:00 +0000528void MipsTargetAsmStreamer::emitDirectiveSetMips1() {
529 OS << "\t.set\tmips1\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000530 MipsTargetStreamer::emitDirectiveSetMips1();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000531}
532
533void MipsTargetAsmStreamer::emitDirectiveSetMips2() {
534 OS << "\t.set\tmips2\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000535 MipsTargetStreamer::emitDirectiveSetMips2();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000536}
537
538void MipsTargetAsmStreamer::emitDirectiveSetMips3() {
539 OS << "\t.set\tmips3\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000540 MipsTargetStreamer::emitDirectiveSetMips3();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000541}
542
543void MipsTargetAsmStreamer::emitDirectiveSetMips4() {
544 OS << "\t.set\tmips4\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000545 MipsTargetStreamer::emitDirectiveSetMips4();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000546}
547
548void MipsTargetAsmStreamer::emitDirectiveSetMips5() {
549 OS << "\t.set\tmips5\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000550 MipsTargetStreamer::emitDirectiveSetMips5();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000551}
552
553void MipsTargetAsmStreamer::emitDirectiveSetMips32() {
554 OS << "\t.set\tmips32\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000555 MipsTargetStreamer::emitDirectiveSetMips32();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000556}
557
Vladimir Medic615b26e2014-03-04 09:54:09 +0000558void MipsTargetAsmStreamer::emitDirectiveSetMips32R2() {
559 OS << "\t.set\tmips32r2\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000560 MipsTargetStreamer::emitDirectiveSetMips32R2();
Vladimir Medic615b26e2014-03-04 09:54:09 +0000561}
562
Daniel Sanders17793142015-02-18 16:24:50 +0000563void MipsTargetAsmStreamer::emitDirectiveSetMips32R3() {
564 OS << "\t.set\tmips32r3\n";
565 MipsTargetStreamer::emitDirectiveSetMips32R3();
566}
567
568void MipsTargetAsmStreamer::emitDirectiveSetMips32R5() {
569 OS << "\t.set\tmips32r5\n";
570 MipsTargetStreamer::emitDirectiveSetMips32R5();
571}
572
Daniel Sandersf0df2212014-08-04 12:20:00 +0000573void MipsTargetAsmStreamer::emitDirectiveSetMips32R6() {
574 OS << "\t.set\tmips32r6\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000575 MipsTargetStreamer::emitDirectiveSetMips32R6();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000576}
577
Matheus Almeida3b9c63d2014-03-26 15:14:32 +0000578void MipsTargetAsmStreamer::emitDirectiveSetMips64() {
579 OS << "\t.set\tmips64\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000580 MipsTargetStreamer::emitDirectiveSetMips64();
Matheus Almeida3b9c63d2014-03-26 15:14:32 +0000581}
582
Matheus Almeidaa2cd0092014-03-26 14:52:22 +0000583void MipsTargetAsmStreamer::emitDirectiveSetMips64R2() {
584 OS << "\t.set\tmips64r2\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000585 MipsTargetStreamer::emitDirectiveSetMips64R2();
Matheus Almeidaa2cd0092014-03-26 14:52:22 +0000586}
587
Daniel Sanders17793142015-02-18 16:24:50 +0000588void MipsTargetAsmStreamer::emitDirectiveSetMips64R3() {
589 OS << "\t.set\tmips64r3\n";
590 MipsTargetStreamer::emitDirectiveSetMips64R3();
591}
592
593void MipsTargetAsmStreamer::emitDirectiveSetMips64R5() {
594 OS << "\t.set\tmips64r5\n";
595 MipsTargetStreamer::emitDirectiveSetMips64R5();
596}
597
Daniel Sandersf0df2212014-08-04 12:20:00 +0000598void MipsTargetAsmStreamer::emitDirectiveSetMips64R6() {
599 OS << "\t.set\tmips64r6\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000600 MipsTargetStreamer::emitDirectiveSetMips64R6();
Daniel Sandersf0df2212014-08-04 12:20:00 +0000601}
602
Vladimir Medic27c398e2014-03-05 11:05:09 +0000603void MipsTargetAsmStreamer::emitDirectiveSetDsp() {
604 OS << "\t.set\tdsp\n";
Toma Tabacu88f05ce2014-08-13 12:48:12 +0000605 MipsTargetStreamer::emitDirectiveSetDsp();
Vladimir Medic27c398e2014-03-05 11:05:09 +0000606}
Toma Tabacu9db22db2014-09-09 10:15:38 +0000607
Petar Jovanovic65f10242017-10-05 17:40:32 +0000608void MipsTargetAsmStreamer::emitDirectiveSetDspr2() {
609 OS << "\t.set\tdspr2\n";
610 MipsTargetStreamer::emitDirectiveSetDspr2();
611}
612
Toma Tabacu351b2fe2014-09-17 09:01:54 +0000613void MipsTargetAsmStreamer::emitDirectiveSetNoDsp() {
614 OS << "\t.set\tnodsp\n";
615 MipsTargetStreamer::emitDirectiveSetNoDsp();
616}
617
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +0000618void MipsTargetAsmStreamer::emitDirectiveSetPop() {
619 OS << "\t.set\tpop\n";
620 MipsTargetStreamer::emitDirectiveSetPop();
621}
Toma Tabacu9db22db2014-09-09 10:15:38 +0000622
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +0000623void MipsTargetAsmStreamer::emitDirectiveSetPush() {
624 OS << "\t.set\tpush\n";
625 MipsTargetStreamer::emitDirectiveSetPush();
626}
Toma Tabacu9db22db2014-09-09 10:15:38 +0000627
Toma Tabacu29696502015-06-02 09:48:04 +0000628void MipsTargetAsmStreamer::emitDirectiveSetSoftFloat() {
629 OS << "\t.set\tsoftfloat\n";
630 MipsTargetStreamer::emitDirectiveSetSoftFloat();
631}
632
633void MipsTargetAsmStreamer::emitDirectiveSetHardFloat() {
634 OS << "\t.set\thardfloat\n";
635 MipsTargetStreamer::emitDirectiveSetHardFloat();
636}
637
Rafael Espindola25fa2912014-01-27 04:33:11 +0000638// Print a 32 bit hex number with all numbers.
639static void printHex32(unsigned Value, raw_ostream &OS) {
640 OS << "0x";
641 for (int i = 7; i >= 0; i--)
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000642 OS.write_hex((Value & (0xF << (i * 4))) >> (i * 4));
Rafael Espindola25fa2912014-01-27 04:33:11 +0000643}
644
645void MipsTargetAsmStreamer::emitMask(unsigned CPUBitmask,
646 int CPUTopSavedRegOff) {
647 OS << "\t.mask \t";
648 printHex32(CPUBitmask, OS);
649 OS << ',' << CPUTopSavedRegOff << '\n';
650}
651
652void MipsTargetAsmStreamer::emitFMask(unsigned FPUBitmask,
653 int FPUTopSavedRegOff) {
654 OS << "\t.fmask\t";
655 printHex32(FPUBitmask, OS);
656 OS << "," << FPUTopSavedRegOff << '\n';
Rafael Espindola054234f2014-01-27 03:53:56 +0000657}
658
Toma Tabacuc4c202a2014-10-01 14:53:19 +0000659void MipsTargetAsmStreamer::emitDirectiveCpLoad(unsigned RegNo) {
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000660 OS << "\t.cpload\t$"
661 << StringRef(MipsInstPrinter::getRegisterName(RegNo)).lower() << "\n";
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000662 forbidModuleDirective();
Matheus Almeida525bc4f2014-04-30 11:28:42 +0000663}
664
Daniel Sandersdf8510d2016-05-11 12:48:19 +0000665bool MipsTargetAsmStreamer::emitDirectiveCpRestore(
Benjamin Kramerd3f4c052016-06-12 16:13:55 +0000666 int Offset, function_ref<unsigned()> GetATReg, SMLoc IDLoc,
Daniel Sandersdf8510d2016-05-11 12:48:19 +0000667 const MCSubtargetInfo *STI) {
668 MipsTargetStreamer::emitDirectiveCpRestore(Offset, GetATReg, IDLoc, STI);
Daniel Sanderse2982ad2015-09-17 16:08:39 +0000669 OS << "\t.cprestore\t" << Offset << "\n";
Daniel Sandersdf8510d2016-05-11 12:48:19 +0000670 return true;
Daniel Sanderse2982ad2015-09-17 16:08:39 +0000671}
672
Matheus Almeidad92a3fa2014-05-01 10:24:46 +0000673void MipsTargetAsmStreamer::emitDirectiveCpsetup(unsigned RegNo,
674 int RegOrOffset,
675 const MCSymbol &Sym,
676 bool IsReg) {
677 OS << "\t.cpsetup\t$"
678 << StringRef(MipsInstPrinter::getRegisterName(RegNo)).lower() << ", ";
679
680 if (IsReg)
681 OS << "$"
682 << StringRef(MipsInstPrinter::getRegisterName(RegOrOffset)).lower();
683 else
684 OS << RegOrOffset;
685
686 OS << ", ";
687
Daniel Sanders5d796282015-09-21 09:26:55 +0000688 OS << Sym.getName();
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000689 forbidModuleDirective();
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000690}
691
Daniel Sandersf173dda2015-09-22 10:50:09 +0000692void MipsTargetAsmStreamer::emitDirectiveCpreturn(unsigned SaveLocation,
693 bool SaveLocationIsRegister) {
694 OS << "\t.cpreturn";
695 forbidModuleDirective();
696}
697
Toma Tabacua64e5402015-06-25 12:44:38 +0000698void MipsTargetAsmStreamer::emitDirectiveModuleFP() {
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000699 OS << "\t.module\tfp=";
Toma Tabacua64e5402015-06-25 12:44:38 +0000700 OS << ABIFlagsSection.getFpABIString(ABIFlagsSection.getFpABI()) << "\n";
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000701}
702
Daniel Sanders7e527422014-07-10 13:38:23 +0000703void MipsTargetAsmStreamer::emitDirectiveSetFp(
704 MipsABIFlagsSection::FpABIKind Value) {
Toma Tabacu4e0cf8e2015-03-06 12:15:12 +0000705 MipsTargetStreamer::emitDirectiveSetFp(Value);
706
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000707 OS << "\t.set\tfp=";
Daniel Sanders7e527422014-07-10 13:38:23 +0000708 OS << ABIFlagsSection.getFpABIString(Value) << "\n";
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000709}
710
Toma Tabacu3c499582015-06-25 10:56:57 +0000711void MipsTargetAsmStreamer::emitDirectiveModuleOddSPReg() {
712 MipsTargetStreamer::emitDirectiveModuleOddSPReg();
Daniel Sanders7e527422014-07-10 13:38:23 +0000713
Toma Tabacu3c499582015-06-25 10:56:57 +0000714 OS << "\t.module\t" << (ABIFlagsSection.OddSPReg ? "" : "no") << "oddspreg\n";
Daniel Sanders7e527422014-07-10 13:38:23 +0000715}
716
Toma Tabacu32c72aa2015-06-30 09:36:50 +0000717void MipsTargetAsmStreamer::emitDirectiveSetOddSPReg() {
718 MipsTargetStreamer::emitDirectiveSetOddSPReg();
719 OS << "\t.set\toddspreg\n";
720}
721
722void MipsTargetAsmStreamer::emitDirectiveSetNoOddSPReg() {
723 MipsTargetStreamer::emitDirectiveSetNoOddSPReg();
724 OS << "\t.set\tnooddspreg\n";
725}
726
Toma Tabacu0f093132015-06-30 13:46:03 +0000727void MipsTargetAsmStreamer::emitDirectiveModuleSoftFloat() {
728 OS << "\t.module\tsoftfloat\n";
729}
730
731void MipsTargetAsmStreamer::emitDirectiveModuleHardFloat() {
732 OS << "\t.module\thardfloat\n";
733}
734
Simon Dardis805f1e02017-07-11 21:28:36 +0000735void MipsTargetAsmStreamer::emitDirectiveModuleMT() {
736 OS << "\t.module\tmt\n";
737}
738
Petar Jovanovic3408caf2018-03-14 14:13:31 +0000739void MipsTargetAsmStreamer::emitDirectiveModuleCRC() {
740 OS << "\t.module\tcrc\n";
741}
742
743void MipsTargetAsmStreamer::emitDirectiveModuleNoCRC() {
744 OS << "\t.module\tnocrc\n";
745}
746
Petar Jovanovicd4349f32018-04-27 09:12:08 +0000747void MipsTargetAsmStreamer::emitDirectiveModuleVirt() {
748 OS << "\t.module\tvirt\n";
749}
750
751void MipsTargetAsmStreamer::emitDirectiveModuleNoVirt() {
752 OS << "\t.module\tnovirt\n";
753}
754
Petar Jovanovicdaf51692018-05-17 16:30:32 +0000755void MipsTargetAsmStreamer::emitDirectiveModuleGINV() {
756 OS << "\t.module\tginv\n";
757}
758
759void MipsTargetAsmStreamer::emitDirectiveModuleNoGINV() {
760 OS << "\t.module\tnoginv\n";
761}
762
Jack Carter0cd3c192014-01-06 23:27:31 +0000763// This part is for ELF object output.
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000764MipsTargetELFStreamer::MipsTargetELFStreamer(MCStreamer &S,
765 const MCSubtargetInfo &STI)
Rafael Espindola972e71a2014-01-31 23:10:26 +0000766 : MipsTargetStreamer(S), MicroMipsEnabled(false), STI(STI) {
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000767 MCAssembler &MCA = getStreamer().getAssembler();
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000768
769 // It's possible that MCObjectFileInfo isn't fully initialized at this point
Matthias Braunbb8507e2017-10-12 22:57:28 +0000770 // due to an initialization order problem where LLVMTargetMachine creates the
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000771 // target streamer before TargetLoweringObjectFile calls
772 // InitializeMCObjectFileInfo. There doesn't seem to be a single place that
773 // covers all cases so this statement covers most cases and direct object
774 // emission must call setPic() once MCObjectFileInfo has been initialized. The
775 // cases we don't handle here are covered by MipsAsmPrinter.
Rafael Espindola699281c2016-05-18 11:58:50 +0000776 Pic = MCA.getContext().getObjectFileInfo()->isPositionIndependent();
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000777
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000778 const FeatureBitset &Features = STI.getFeatureBits();
Eric Christophera5762812015-01-26 17:33:46 +0000779
780 // Set the header flags that we can in the constructor.
781 // FIXME: This is a fairly terrible hack. We set the rest
782 // of these in the destructor. The problem here is two-fold:
783 //
784 // a: Some of the eflags can be set/reset by directives.
785 // b: There aren't any usage paths that initialize the ABI
786 // pointer until after we initialize either an assembler
787 // or the target machine.
788 // We can fix this by making the target streamer construct
789 // the ABI, but this is fraught with wide ranging dependency
790 // issues as well.
791 unsigned EFlags = MCA.getELFHeaderEFlags();
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000792
Simon Dardis6433d5a2017-02-01 15:39:23 +0000793 // FIXME: Fix a dependency issue by instantiating the ABI object to some
794 // default based off the triple. The triple doesn't describe the target
795 // fully, but any external user of the API that uses the MCTargetStreamer
796 // would otherwise crash on assertion failure.
797
798 ABI = MipsABIInfo(
799 STI.getTargetTriple().getArch() == Triple::ArchType::mipsel ||
800 STI.getTargetTriple().getArch() == Triple::ArchType::mips
801 ? MipsABIInfo::O32()
802 : MipsABIInfo::N64());
803
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000804 // Architecture
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000805 if (Features[Mips::FeatureMips64r6])
Daniel Sanders950f48d2014-07-04 15:21:53 +0000806 EFlags |= ELF::EF_MIPS_ARCH_64R6;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000807 else if (Features[Mips::FeatureMips64r2] ||
808 Features[Mips::FeatureMips64r3] ||
809 Features[Mips::FeatureMips64r5])
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000810 EFlags |= ELF::EF_MIPS_ARCH_64R2;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000811 else if (Features[Mips::FeatureMips64])
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000812 EFlags |= ELF::EF_MIPS_ARCH_64;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000813 else if (Features[Mips::FeatureMips5])
Daniel Sanders950f48d2014-07-04 15:21:53 +0000814 EFlags |= ELF::EF_MIPS_ARCH_5;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000815 else if (Features[Mips::FeatureMips4])
Daniel Sandersf7b32292014-04-03 12:13:36 +0000816 EFlags |= ELF::EF_MIPS_ARCH_4;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000817 else if (Features[Mips::FeatureMips3])
Daniel Sanders950f48d2014-07-04 15:21:53 +0000818 EFlags |= ELF::EF_MIPS_ARCH_3;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000819 else if (Features[Mips::FeatureMips32r6])
Daniel Sanders950f48d2014-07-04 15:21:53 +0000820 EFlags |= ELF::EF_MIPS_ARCH_32R6;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000821 else if (Features[Mips::FeatureMips32r2] ||
822 Features[Mips::FeatureMips32r3] ||
823 Features[Mips::FeatureMips32r5])
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000824 EFlags |= ELF::EF_MIPS_ARCH_32R2;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000825 else if (Features[Mips::FeatureMips32])
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000826 EFlags |= ELF::EF_MIPS_ARCH_32;
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000827 else if (Features[Mips::FeatureMips2])
Daniel Sanders950f48d2014-07-04 15:21:53 +0000828 EFlags |= ELF::EF_MIPS_ARCH_2;
829 else
830 EFlags |= ELF::EF_MIPS_ARCH_1;
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000831
Daniel Sanders415c1592016-05-12 11:31:19 +0000832 // Machine
833 if (Features[Mips::FeatureCnMips])
834 EFlags |= ELF::EF_MIPS_MACH_OCTEON;
835
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000836 // Other options.
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000837 if (Features[Mips::FeatureNaN2008])
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000838 EFlags |= ELF::EF_MIPS_NAN2008;
839
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000840 MCA.setELFHeaderEFlags(EFlags);
841}
Jack Carter86ac5c12013-11-18 23:55:27 +0000842
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000843void MipsTargetELFStreamer::emitLabel(MCSymbol *S) {
844 auto *Symbol = cast<MCSymbolELF>(S);
Rafael Espindolac73aed12015-06-03 19:03:11 +0000845 getStreamer().getAssembler().registerSymbol(*Symbol);
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000846 uint8_t Type = Symbol->getType();
Rafael Espindola26e917c2014-01-15 03:07:12 +0000847 if (Type != ELF::STT_FUNC)
848 return;
849
Simon Dardis3c82a642017-02-08 16:25:05 +0000850 if (isMicroMipsEnabled())
851 Symbol->setOther(ELF::STO_MIPS_MICROMIPS);
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000852}
853
Rafael Espindola972e71a2014-01-31 23:10:26 +0000854void MipsTargetELFStreamer::finish() {
855 MCAssembler &MCA = getStreamer().getAssembler();
Daniel Sanders68c37472014-07-21 13:30:55 +0000856 const MCObjectFileInfo &OFI = *MCA.getContext().getObjectFileInfo();
Rafael Espindola972e71a2014-01-31 23:10:26 +0000857
Daniel Sanders41ffa5d2014-07-14 15:05:51 +0000858 // .bss, .text and .data are always at least 16-byte aligned.
Rafael Espindola967d6a62015-05-21 21:02:35 +0000859 MCSection &TextSection = *OFI.getTextSection();
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000860 MCA.registerSection(TextSection);
Rafael Espindola967d6a62015-05-21 21:02:35 +0000861 MCSection &DataSection = *OFI.getDataSection();
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000862 MCA.registerSection(DataSection);
Rafael Espindola967d6a62015-05-21 21:02:35 +0000863 MCSection &BSSSection = *OFI.getBSSSection();
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000864 MCA.registerSection(BSSSection);
Daniel Sanders41ffa5d2014-07-14 15:05:51 +0000865
Rafael Espindola967d6a62015-05-21 21:02:35 +0000866 TextSection.setAlignment(std::max(16u, TextSection.getAlignment()));
867 DataSection.setAlignment(std::max(16u, DataSection.getAlignment()));
868 BSSSection.setAlignment(std::max(16u, BSSSection.getAlignment()));
Daniel Sanders41ffa5d2014-07-14 15:05:51 +0000869
Daniel Sandersc07f06a2016-05-04 13:21:06 +0000870 if (RoundSectionSizes) {
871 // Make sections sizes a multiple of the alignment. This is useful for
872 // verifying the output of IAS against the output of other assemblers but
873 // it's not necessary to produce a correct object and increases section
874 // size.
875 MCStreamer &OS = getStreamer();
876 for (MCSection &S : MCA) {
877 MCSectionELF &Section = static_cast<MCSectionELF &>(S);
Daniel Sanders9db710a2016-04-29 12:44:07 +0000878
Daniel Sandersc07f06a2016-05-04 13:21:06 +0000879 unsigned Alignment = Section.getAlignment();
880 if (Alignment) {
881 OS.SwitchSection(&Section);
882 if (Section.UseCodeAlign())
883 OS.EmitCodeAlignment(Alignment, Alignment);
884 else
885 OS.EmitValueToAlignment(Alignment, 0, 1, Alignment);
886 }
Daniel Sanders9db710a2016-04-29 12:44:07 +0000887 }
888 }
889
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000890 const FeatureBitset &Features = STI.getFeatureBits();
Eric Christophera5762812015-01-26 17:33:46 +0000891
892 // Update e_header flags. See the FIXME and comment above in
893 // the constructor for a full rundown on this.
894 unsigned EFlags = MCA.getELFHeaderEFlags();
895
896 // ABI
897 // N64 does not require any ABI bits.
898 if (getABI().IsO32())
899 EFlags |= ELF::EF_MIPS_ABI_O32;
900 else if (getABI().IsN32())
901 EFlags |= ELF::EF_MIPS_ABI2;
902
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000903 if (Features[Mips::FeatureGP64Bit]) {
Eric Christophera5762812015-01-26 17:33:46 +0000904 if (getABI().IsO32())
905 EFlags |= ELF::EF_MIPS_32BITMODE; /* Compatibility Mode */
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000906 } else if (Features[Mips::FeatureMips64r2] || Features[Mips::FeatureMips64])
Eric Christophera5762812015-01-26 17:33:46 +0000907 EFlags |= ELF::EF_MIPS_32BITMODE;
908
Simon Dardisca74dd72017-01-27 11:36:52 +0000909 // -mplt is not implemented but we should act as if it was
910 // given.
911 if (!Features[Mips::FeatureNoABICalls])
912 EFlags |= ELF::EF_MIPS_CPIC;
913
914 if (Pic)
915 EFlags |= ELF::EF_MIPS_PIC | ELF::EF_MIPS_CPIC;
Eric Christophera5762812015-01-26 17:33:46 +0000916
917 MCA.setELFHeaderEFlags(EFlags);
918
Daniel Sanders68c37472014-07-21 13:30:55 +0000919 // Emit all the option records.
920 // At the moment we are only emitting .Mips.options (ODK_REGINFO) and
921 // .reginfo.
922 MipsELFStreamer &MEF = static_cast<MipsELFStreamer &>(Streamer);
923 MEF.EmitMipsOptionRecords();
Rafael Espindola972e71a2014-01-31 23:10:26 +0000924
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000925 emitMipsAbiFlags();
Rafael Espindola972e71a2014-01-31 23:10:26 +0000926}
927
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000928void MipsTargetELFStreamer::emitAssignment(MCSymbol *S, const MCExpr *Value) {
929 auto *Symbol = cast<MCSymbolELF>(S);
Zoran Jovanovic28221d82014-03-20 09:44:49 +0000930 // If on rhs is micromips symbol then mark Symbol as microMips.
931 if (Value->getKind() != MCExpr::SymbolRef)
932 return;
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000933 const auto &RhsSym = cast<MCSymbolELF>(
934 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol());
Toma Tabacu2cc44f52015-04-16 13:37:32 +0000935
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000936 if (!(RhsSym.getOther() & ELF::STO_MIPS_MICROMIPS))
Zoran Jovanovic28221d82014-03-20 09:44:49 +0000937 return;
938
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000939 Symbol->setOther(ELF::STO_MIPS_MICROMIPS);
Zoran Jovanovic28221d82014-03-20 09:44:49 +0000940}
941
Jack Carter86ac5c12013-11-18 23:55:27 +0000942MCELFStreamer &MipsTargetELFStreamer::getStreamer() {
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000943 return static_cast<MCELFStreamer &>(Streamer);
Jack Carter86ac5c12013-11-18 23:55:27 +0000944}
945
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000946void MipsTargetELFStreamer::emitDirectiveSetMicroMips() {
947 MicroMipsEnabled = true;
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000948 forbidModuleDirective();
Jack Carter86ac5c12013-11-18 23:55:27 +0000949}
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000950
951void MipsTargetELFStreamer::emitDirectiveSetNoMicroMips() {
952 MicroMipsEnabled = false;
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000953 forbidModuleDirective();
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000954}
955
Daniel Sanderscda908a2016-05-16 09:10:13 +0000956void MipsTargetELFStreamer::setUsesMicroMips() {
957 MCAssembler &MCA = getStreamer().getAssembler();
958 unsigned Flags = MCA.getELFHeaderEFlags();
959 Flags |= ELF::EF_MIPS_MICROMIPS;
960 MCA.setELFHeaderEFlags(Flags);
961}
962
Rafael Espindola6633d572014-01-14 18:57:12 +0000963void MipsTargetELFStreamer::emitDirectiveSetMips16() {
Rafael Espindolae7583752014-01-24 16:13:20 +0000964 MCAssembler &MCA = getStreamer().getAssembler();
965 unsigned Flags = MCA.getELFHeaderEFlags();
966 Flags |= ELF::EF_MIPS_ARCH_ASE_M16;
967 MCA.setELFHeaderEFlags(Flags);
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000968 forbidModuleDirective();
Rafael Espindola6633d572014-01-14 18:57:12 +0000969}
970
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000971void MipsTargetELFStreamer::emitDirectiveSetNoReorder() {
Rafael Espindolacb1953f2014-01-26 06:57:13 +0000972 MCAssembler &MCA = getStreamer().getAssembler();
973 unsigned Flags = MCA.getELFHeaderEFlags();
974 Flags |= ELF::EF_MIPS_NOREORDER;
975 MCA.setELFHeaderEFlags(Flags);
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000976 forbidModuleDirective();
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000977}
978
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000979void MipsTargetELFStreamer::emitDirectiveEnd(StringRef Name) {
Daniel Sandersd97a6342014-08-13 10:07:34 +0000980 MCAssembler &MCA = getStreamer().getAssembler();
981 MCContext &Context = MCA.getContext();
982 MCStreamer &OS = getStreamer();
983
Scott Egerton219fae92016-02-17 11:15:16 +0000984 MCSectionELF *Sec = Context.getELFSection(".pdr", ELF::SHT_PROGBITS, 0);
Daniel Sandersd97a6342014-08-13 10:07:34 +0000985
Daniel Sanders2b561332015-11-23 16:08:03 +0000986 MCSymbol *Sym = Context.getOrCreateSymbol(Name);
Daniel Sandersd97a6342014-08-13 10:07:34 +0000987 const MCSymbolRefExpr *ExprRef =
Daniel Sanders2b561332015-11-23 16:08:03 +0000988 MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, Context);
Daniel Sandersd97a6342014-08-13 10:07:34 +0000989
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000990 MCA.registerSection(*Sec);
Rafael Espindola967d6a62015-05-21 21:02:35 +0000991 Sec->setAlignment(4);
Daniel Sandersd97a6342014-08-13 10:07:34 +0000992
993 OS.PushSection();
994
995 OS.SwitchSection(Sec);
996
997 OS.EmitValueImpl(ExprRef, 4);
998
999 OS.EmitIntValue(GPRInfoSet ? GPRBitMask : 0, 4); // reg_mask
1000 OS.EmitIntValue(GPRInfoSet ? GPROffset : 0, 4); // reg_offset
1001
1002 OS.EmitIntValue(FPRInfoSet ? FPRBitMask : 0, 4); // fpreg_mask
1003 OS.EmitIntValue(FPRInfoSet ? FPROffset : 0, 4); // fpreg_offset
1004
1005 OS.EmitIntValue(FrameInfoSet ? FrameOffset : 0, 4); // frame_offset
1006 OS.EmitIntValue(FrameInfoSet ? FrameReg : 0, 4); // frame_reg
1007 OS.EmitIntValue(FrameInfoSet ? ReturnReg : 0, 4); // return_reg
1008
1009 // The .end directive marks the end of a procedure. Invalidate
1010 // the information gathered up until this point.
1011 GPRInfoSet = FPRInfoSet = FrameInfoSet = false;
1012
1013 OS.PopSection();
Daniel Sanders2b561332015-11-23 16:08:03 +00001014
1015 // .end also implicitly sets the size.
1016 MCSymbol *CurPCSym = Context.createTempSymbol();
1017 OS.EmitLabel(CurPCSym);
1018 const MCExpr *Size = MCBinaryExpr::createSub(
1019 MCSymbolRefExpr::create(CurPCSym, MCSymbolRefExpr::VK_None, Context),
1020 ExprRef, Context);
Simon Dardis68e9d942017-02-03 15:48:53 +00001021
1022 // The ELFObjectWriter can determine the absolute size as it has access to
1023 // the layout information of the assembly file, so a size expression rather
1024 // than an absolute value is ok here.
Daniel Sanders2b561332015-11-23 16:08:03 +00001025 static_cast<MCSymbolELF *>(Sym)->setSize(Size);
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +00001026}
1027
Rafael Espindola6633d572014-01-14 18:57:12 +00001028void MipsTargetELFStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {
Daniel Sandersd97a6342014-08-13 10:07:34 +00001029 GPRInfoSet = FPRInfoSet = FrameInfoSet = false;
Daniel Sanders2b561332015-11-23 16:08:03 +00001030
1031 // .ent also acts like an implicit '.type symbol, STT_FUNC'
1032 static_cast<const MCSymbolELF &>(Symbol).setType(ELF::STT_FUNC);
Rafael Espindola6633d572014-01-14 18:57:12 +00001033}
1034
Jack Carter0cd3c192014-01-06 23:27:31 +00001035void MipsTargetELFStreamer::emitDirectiveAbiCalls() {
1036 MCAssembler &MCA = getStreamer().getAssembler();
1037 unsigned Flags = MCA.getELFHeaderEFlags();
Rafael Espindolacb1953f2014-01-26 06:57:13 +00001038 Flags |= ELF::EF_MIPS_CPIC | ELF::EF_MIPS_PIC;
Jack Carter0cd3c192014-01-06 23:27:31 +00001039 MCA.setELFHeaderEFlags(Flags);
1040}
Matheus Almeida0051f2d2014-04-16 15:48:55 +00001041
1042void MipsTargetELFStreamer::emitDirectiveNaN2008() {
1043 MCAssembler &MCA = getStreamer().getAssembler();
1044 unsigned Flags = MCA.getELFHeaderEFlags();
1045 Flags |= ELF::EF_MIPS_NAN2008;
1046 MCA.setELFHeaderEFlags(Flags);
1047}
1048
1049void MipsTargetELFStreamer::emitDirectiveNaNLegacy() {
1050 MCAssembler &MCA = getStreamer().getAssembler();
1051 unsigned Flags = MCA.getELFHeaderEFlags();
1052 Flags &= ~ELF::EF_MIPS_NAN2008;
1053 MCA.setELFHeaderEFlags(Flags);
1054}
1055
Jack Carter0cd3c192014-01-06 23:27:31 +00001056void MipsTargetELFStreamer::emitDirectiveOptionPic0() {
1057 MCAssembler &MCA = getStreamer().getAssembler();
1058 unsigned Flags = MCA.getELFHeaderEFlags();
Matheus Almeidaf79b2812014-03-26 13:40:29 +00001059 // This option overrides other PIC options like -KPIC.
1060 Pic = false;
Jack Carter0cd3c192014-01-06 23:27:31 +00001061 Flags &= ~ELF::EF_MIPS_PIC;
1062 MCA.setELFHeaderEFlags(Flags);
1063}
Rafael Espindola054234f2014-01-27 03:53:56 +00001064
Matheus Almeidaf79b2812014-03-26 13:40:29 +00001065void MipsTargetELFStreamer::emitDirectiveOptionPic2() {
1066 MCAssembler &MCA = getStreamer().getAssembler();
1067 unsigned Flags = MCA.getELFHeaderEFlags();
1068 Pic = true;
1069 // NOTE: We are following the GAS behaviour here which means the directive
1070 // 'pic2' also sets the CPIC bit in the ELF header. This is different from
1071 // what is stated in the SYSV ABI which consider the bits EF_MIPS_PIC and
1072 // EF_MIPS_CPIC to be mutually exclusive.
1073 Flags |= ELF::EF_MIPS_PIC | ELF::EF_MIPS_CPIC;
1074 MCA.setELFHeaderEFlags(Flags);
1075}
1076
Toma Tabacu9ca50962015-04-16 09:53:47 +00001077void MipsTargetELFStreamer::emitDirectiveInsn() {
1078 MipsTargetStreamer::emitDirectiveInsn();
1079 MipsELFStreamer &MEF = static_cast<MipsELFStreamer &>(Streamer);
1080 MEF.createPendingLabelRelocs();
1081}
1082
Rafael Espindola054234f2014-01-27 03:53:56 +00001083void MipsTargetELFStreamer::emitFrame(unsigned StackReg, unsigned StackSize,
Daniel Sandersd97a6342014-08-13 10:07:34 +00001084 unsigned ReturnReg_) {
1085 MCContext &Context = getStreamer().getAssembler().getContext();
1086 const MCRegisterInfo *RegInfo = Context.getRegisterInfo();
1087
1088 FrameInfoSet = true;
1089 FrameReg = RegInfo->getEncodingValue(StackReg);
1090 FrameOffset = StackSize;
1091 ReturnReg = RegInfo->getEncodingValue(ReturnReg_);
Rafael Espindola054234f2014-01-27 03:53:56 +00001092}
Rafael Espindola25fa2912014-01-27 04:33:11 +00001093
1094void MipsTargetELFStreamer::emitMask(unsigned CPUBitmask,
1095 int CPUTopSavedRegOff) {
Daniel Sandersd97a6342014-08-13 10:07:34 +00001096 GPRInfoSet = true;
1097 GPRBitMask = CPUBitmask;
1098 GPROffset = CPUTopSavedRegOff;
Rafael Espindola25fa2912014-01-27 04:33:11 +00001099}
1100
1101void MipsTargetELFStreamer::emitFMask(unsigned FPUBitmask,
1102 int FPUTopSavedRegOff) {
Daniel Sandersd97a6342014-08-13 10:07:34 +00001103 FPRInfoSet = true;
1104 FPRBitMask = FPUBitmask;
1105 FPROffset = FPUTopSavedRegOff;
Rafael Espindola25fa2912014-01-27 04:33:11 +00001106}
Vladimir Medic615b26e2014-03-04 09:54:09 +00001107
Toma Tabacuc4c202a2014-10-01 14:53:19 +00001108void MipsTargetELFStreamer::emitDirectiveCpLoad(unsigned RegNo) {
Matheus Almeida525bc4f2014-04-30 11:28:42 +00001109 // .cpload $reg
1110 // This directive expands to:
1111 // lui $gp, %hi(_gp_disp)
1112 // addui $gp, $gp, %lo(_gp_disp)
1113 // addu $gp, $gp, $reg
1114 // when support for position independent code is enabled.
Eric Christophera5762812015-01-26 17:33:46 +00001115 if (!Pic || (getABI().IsN32() || getABI().IsN64()))
Matheus Almeida525bc4f2014-04-30 11:28:42 +00001116 return;
1117
1118 // There's a GNU extension controlled by -mno-shared that allows
1119 // locally-binding symbols to be accessed using absolute addresses.
1120 // This is currently not supported. When supported -mno-shared makes
1121 // .cpload expand to:
1122 // lui $gp, %hi(__gnu_local_gp)
1123 // addiu $gp, $gp, %lo(__gnu_local_gp)
1124
1125 StringRef SymName("_gp_disp");
1126 MCAssembler &MCA = getStreamer().getAssembler();
Jim Grosbach6f482002015-05-18 18:43:14 +00001127 MCSymbol *GP_Disp = MCA.getContext().getOrCreateSymbol(SymName);
Rafael Espindolab5d316b2015-05-29 20:21:02 +00001128 MCA.registerSymbol(*GP_Disp);
Matheus Almeida525bc4f2014-04-30 11:28:42 +00001129
1130 MCInst TmpInst;
1131 TmpInst.setOpcode(Mips::LUi);
Jim Grosbache9119e42015-05-13 18:37:00 +00001132 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
Daniel Sandersfe98b2f2016-05-03 13:35:44 +00001133 const MCExpr *HiSym = MipsMCExpr::create(
1134 MipsMCExpr::MEK_HI,
1135 MCSymbolRefExpr::create("_gp_disp", MCSymbolRefExpr::VK_None,
1136 MCA.getContext()),
1137 MCA.getContext());
Jim Grosbache9119e42015-05-13 18:37:00 +00001138 TmpInst.addOperand(MCOperand::createExpr(HiSym));
Matheus Almeida525bc4f2014-04-30 11:28:42 +00001139 getStreamer().EmitInstruction(TmpInst, STI);
1140
1141 TmpInst.clear();
1142
1143 TmpInst.setOpcode(Mips::ADDiu);
Jim Grosbache9119e42015-05-13 18:37:00 +00001144 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
1145 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
Daniel Sandersfe98b2f2016-05-03 13:35:44 +00001146 const MCExpr *LoSym = MipsMCExpr::create(
1147 MipsMCExpr::MEK_LO,
1148 MCSymbolRefExpr::create("_gp_disp", MCSymbolRefExpr::VK_None,
1149 MCA.getContext()),
1150 MCA.getContext());
Jim Grosbache9119e42015-05-13 18:37:00 +00001151 TmpInst.addOperand(MCOperand::createExpr(LoSym));
Matheus Almeida525bc4f2014-04-30 11:28:42 +00001152 getStreamer().EmitInstruction(TmpInst, STI);
1153
1154 TmpInst.clear();
1155
1156 TmpInst.setOpcode(Mips::ADDu);
Jim Grosbache9119e42015-05-13 18:37:00 +00001157 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
1158 TmpInst.addOperand(MCOperand::createReg(Mips::GP));
1159 TmpInst.addOperand(MCOperand::createReg(RegNo));
Matheus Almeida525bc4f2014-04-30 11:28:42 +00001160 getStreamer().EmitInstruction(TmpInst, STI);
Vladimir Medicfb8a2a92014-07-08 08:59:22 +00001161
Daniel Sanderscdb45fa2014-08-14 09:18:14 +00001162 forbidModuleDirective();
Matheus Almeida525bc4f2014-04-30 11:28:42 +00001163}
Matheus Almeidad92a3fa2014-05-01 10:24:46 +00001164
Daniel Sandersdf8510d2016-05-11 12:48:19 +00001165bool MipsTargetELFStreamer::emitDirectiveCpRestore(
Benjamin Kramerd3f4c052016-06-12 16:13:55 +00001166 int Offset, function_ref<unsigned()> GetATReg, SMLoc IDLoc,
Daniel Sandersdf8510d2016-05-11 12:48:19 +00001167 const MCSubtargetInfo *STI) {
1168 MipsTargetStreamer::emitDirectiveCpRestore(Offset, GetATReg, IDLoc, STI);
Daniel Sanderse2982ad2015-09-17 16:08:39 +00001169 // .cprestore offset
1170 // When PIC mode is enabled and the O32 ABI is used, this directive expands
1171 // to:
1172 // sw $gp, offset($sp)
1173 // and adds a corresponding LW after every JAL.
1174
1175 // Note that .cprestore is ignored if used with the N32 and N64 ABIs or if it
1176 // is used in non-PIC mode.
1177 if (!Pic || (getABI().IsN32() || getABI().IsN64()))
Daniel Sandersdf8510d2016-05-11 12:48:19 +00001178 return true;
1179
Daniel Sanders7225cd52016-04-29 16:16:49 +00001180 // Store the $gp on the stack.
Daniel Sanders241c6792016-05-12 14:01:50 +00001181 emitStoreWithImmOffset(Mips::SW, Mips::GP, Mips::SP, Offset, GetATReg, IDLoc,
Daniel Sanders7225cd52016-04-29 16:16:49 +00001182 STI);
Daniel Sandersdf8510d2016-05-11 12:48:19 +00001183 return true;
Daniel Sanderse2982ad2015-09-17 16:08:39 +00001184}
1185
Matheus Almeidad92a3fa2014-05-01 10:24:46 +00001186void MipsTargetELFStreamer::emitDirectiveCpsetup(unsigned RegNo,
1187 int RegOrOffset,
1188 const MCSymbol &Sym,
1189 bool IsReg) {
1190 // Only N32 and N64 emit anything for .cpsetup iff PIC is set.
Eric Christophera5762812015-01-26 17:33:46 +00001191 if (!Pic || !(getABI().IsN32() || getABI().IsN64()))
Matheus Almeidad92a3fa2014-05-01 10:24:46 +00001192 return;
1193
Daniel Sanderse8581362016-06-14 10:13:47 +00001194 forbidModuleDirective();
1195
Matheus Almeidad92a3fa2014-05-01 10:24:46 +00001196 MCAssembler &MCA = getStreamer().getAssembler();
1197 MCInst Inst;
1198
1199 // Either store the old $gp in a register or on the stack
1200 if (IsReg) {
1201 // move $save, $gpreg
Daniel Sanderse8581362016-06-14 10:13:47 +00001202 emitRRR(Mips::OR64, RegOrOffset, Mips::GP, Mips::ZERO, SMLoc(), &STI);
Matheus Almeidad92a3fa2014-05-01 10:24:46 +00001203 } else {
1204 // sd $gpreg, offset($sp)
Daniel Sanderse8581362016-06-14 10:13:47 +00001205 emitRRI(Mips::SD, Mips::GP, Mips::SP, RegOrOffset, SMLoc(), &STI);
Matheus Almeidad92a3fa2014-05-01 10:24:46 +00001206 }
Daniel Sanderse8581362016-06-14 10:13:47 +00001207
1208 if (getABI().IsN32()) {
1209 MCSymbol *GPSym = MCA.getContext().getOrCreateSymbol("__gnu_local_gp");
1210 const MipsMCExpr *HiExpr = MipsMCExpr::create(
1211 MipsMCExpr::MEK_HI, MCSymbolRefExpr::create(GPSym, MCA.getContext()),
1212 MCA.getContext());
1213 const MipsMCExpr *LoExpr = MipsMCExpr::create(
1214 MipsMCExpr::MEK_LO, MCSymbolRefExpr::create(GPSym, MCA.getContext()),
1215 MCA.getContext());
1216
1217 // lui $gp, %hi(__gnu_local_gp)
1218 emitRX(Mips::LUi, Mips::GP, MCOperand::createExpr(HiExpr), SMLoc(), &STI);
1219
1220 // addiu $gp, $gp, %lo(__gnu_local_gp)
1221 emitRRX(Mips::ADDiu, Mips::GP, Mips::GP, MCOperand::createExpr(LoExpr),
1222 SMLoc(), &STI);
1223
1224 return;
1225 }
Matheus Almeidad92a3fa2014-05-01 10:24:46 +00001226
Daniel Sandersfe98b2f2016-05-03 13:35:44 +00001227 const MipsMCExpr *HiExpr = MipsMCExpr::createGpOff(
1228 MipsMCExpr::MEK_HI, MCSymbolRefExpr::create(&Sym, MCA.getContext()),
1229 MCA.getContext());
1230 const MipsMCExpr *LoExpr = MipsMCExpr::createGpOff(
1231 MipsMCExpr::MEK_LO, MCSymbolRefExpr::create(&Sym, MCA.getContext()),
1232 MCA.getContext());
Toma Tabacu8874eac2015-02-18 13:46:53 +00001233
Matheus Almeidad92a3fa2014-05-01 10:24:46 +00001234 // lui $gp, %hi(%neg(%gp_rel(funcSym)))
Daniel Sanderse8581362016-06-14 10:13:47 +00001235 emitRX(Mips::LUi, Mips::GP, MCOperand::createExpr(HiExpr), SMLoc(), &STI);
Matheus Almeidad92a3fa2014-05-01 10:24:46 +00001236
1237 // addiu $gp, $gp, %lo(%neg(%gp_rel(funcSym)))
Daniel Sanderse8581362016-06-14 10:13:47 +00001238 emitRRX(Mips::ADDiu, Mips::GP, Mips::GP, MCOperand::createExpr(LoExpr),
1239 SMLoc(), &STI);
Matheus Almeidad92a3fa2014-05-01 10:24:46 +00001240
1241 // daddu $gp, $gp, $funcreg
Daniel Sanderse8581362016-06-14 10:13:47 +00001242 emitRRR(Mips::DADDu, Mips::GP, Mips::GP, RegNo, SMLoc(), &STI);
Vladimir Medicfb8a2a92014-07-08 08:59:22 +00001243}
1244
Daniel Sandersf173dda2015-09-22 10:50:09 +00001245void MipsTargetELFStreamer::emitDirectiveCpreturn(unsigned SaveLocation,
1246 bool SaveLocationIsRegister) {
1247 // Only N32 and N64 emit anything for .cpreturn iff PIC is set.
1248 if (!Pic || !(getABI().IsN32() || getABI().IsN64()))
1249 return;
1250
1251 MCInst Inst;
1252 // Either restore the old $gp from a register or on the stack
1253 if (SaveLocationIsRegister) {
1254 Inst.setOpcode(Mips::OR);
1255 Inst.addOperand(MCOperand::createReg(Mips::GP));
1256 Inst.addOperand(MCOperand::createReg(SaveLocation));
1257 Inst.addOperand(MCOperand::createReg(Mips::ZERO));
1258 } else {
1259 Inst.setOpcode(Mips::LD);
1260 Inst.addOperand(MCOperand::createReg(Mips::GP));
1261 Inst.addOperand(MCOperand::createReg(Mips::SP));
1262 Inst.addOperand(MCOperand::createImm(SaveLocation));
1263 }
1264 getStreamer().EmitInstruction(Inst, STI);
1265
1266 forbidModuleDirective();
1267}
1268
Vladimir Medicfb8a2a92014-07-08 08:59:22 +00001269void MipsTargetELFStreamer::emitMipsAbiFlags() {
1270 MCAssembler &MCA = getStreamer().getAssembler();
1271 MCContext &Context = MCA.getContext();
1272 MCStreamer &OS = getStreamer();
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001273 MCSectionELF *Sec = Context.getELFSection(
Rafael Espindolaba31e272015-01-29 17:33:21 +00001274 ".MIPS.abiflags", ELF::SHT_MIPS_ABIFLAGS, ELF::SHF_ALLOC, 24, "");
Rafael Espindolabb9a71c2015-05-26 15:07:25 +00001275 MCA.registerSection(*Sec);
Rafael Espindola967d6a62015-05-21 21:02:35 +00001276 Sec->setAlignment(8);
Vladimir Medicfb8a2a92014-07-08 08:59:22 +00001277 OS.SwitchSection(Sec);
1278
Daniel Sandersc7dbc632014-07-08 10:11:38 +00001279 OS << ABIFlagsSection;
Matheus Almeidad92a3fa2014-05-01 10:24:46 +00001280}