blob: c523e20ab03cd5050f1460bcd14341af0049f8c0 [file] [log] [blame]
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001//===- MipsAsmPrinter.cpp - Mips LLVM Assembly Printer --------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00006//
Akira Hatanakae2489122011-04-15 21:51:11 +00007//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00008//
9// This file contains a printer that converts from our internal representation
10// of machine-dependent LLVM code to GAS-format MIPS assembly language.
11//
Akira Hatanakae2489122011-04-15 21:51:11 +000012//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000013
Chandler Carruth6bda14b2017-06-06 11:49:48 +000014#include "MipsAsmPrinter.h"
Akira Hatanaka9c6028f2011-07-07 23:56:50 +000015#include "InstPrinter/MipsInstPrinter.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000016#include "MCTargetDesc/MipsABIInfo.h"
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000017#include "MCTargetDesc/MipsBaseInfo.h"
Sasa Stankovic8c5736b2014-02-28 10:00:38 +000018#include "MCTargetDesc/MipsMCNaCl.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000019#include "MCTargetDesc/MipsMCTargetDesc.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "Mips.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "MipsMCInstLower.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000022#include "MipsMachineFunction.h"
23#include "MipsSubtarget.h"
Eric Christophera5762812015-01-26 17:33:46 +000024#include "MipsTargetMachine.h"
Rafael Espindolaa17151a2013-10-08 13:08:17 +000025#include "MipsTargetStreamer.h"
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +000026#include "llvm/ADT/SmallString.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000027#include "llvm/ADT/StringRef.h"
28#include "llvm/ADT/Triple.h"
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +000029#include "llvm/ADT/Twine.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000030#include "llvm/BinaryFormat/ELF.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000031#include "llvm/CodeGen/MachineBasicBlock.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000032#include "llvm/CodeGen/MachineConstantPool.h"
Bruno Cardoso Lopesbcda5e22007-07-11 23:24:41 +000033#include "llvm/CodeGen/MachineFrameInfo.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000034#include "llvm/CodeGen/MachineFunction.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000035#include "llvm/CodeGen/MachineInstr.h"
Sasa Stankovic8c5736b2014-02-28 10:00:38 +000036#include "llvm/CodeGen/MachineJumpTableInfo.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000037#include "llvm/CodeGen/MachineOperand.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000038#include "llvm/CodeGen/TargetRegisterInfo.h"
39#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000040#include "llvm/IR/Attributes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000041#include "llvm/IR/BasicBlock.h"
42#include "llvm/IR/DataLayout.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000043#include "llvm/IR/Function.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000044#include "llvm/IR/InlineAsm.h"
45#include "llvm/IR/Instructions.h"
Rafael Espindola2ab7ea72014-01-27 01:33:33 +000046#include "llvm/MC/MCContext.h"
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000047#include "llvm/MC/MCExpr.h"
Akira Hatanaka9c6028f2011-07-07 23:56:50 +000048#include "llvm/MC/MCInst.h"
Sagar Thakurec657922017-02-15 10:48:11 +000049#include "llvm/MC/MCInstBuilder.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000050#include "llvm/MC/MCObjectFileInfo.h"
Rafael Espindola2ab7ea72014-01-27 01:33:33 +000051#include "llvm/MC/MCSectionELF.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000052#include "llvm/MC/MCSymbol.h"
Rafael Espindolaa8695762015-06-02 00:25:12 +000053#include "llvm/MC/MCSymbolELF.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000054#include "llvm/Support/Casting.h"
55#include "llvm/Support/ErrorHandling.h"
Jack Carterb2af5122012-07-05 23:58:21 +000056#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000057#include "llvm/Support/raw_ostream.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000058#include "llvm/Target/TargetMachine.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000059#include <cassert>
60#include <cstdint>
61#include <map>
62#include <memory>
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000063#include <string>
Eugene Zelenko79220eae2017-08-03 22:12:30 +000064#include <vector>
Akira Hatanakaf2bcad92011-07-01 01:04:43 +000065
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000066using namespace llvm;
67
Chandler Carruth84e68b22014-04-22 02:41:26 +000068#define DEBUG_TYPE "mips-asm-printer"
69
Vladimir Stefanovic3daf8bc2019-01-17 21:50:37 +000070extern cl::opt<bool> EmitJalrReloc;
71
Toma Tabacua23f13c2014-12-17 10:56:16 +000072MipsTargetStreamer &MipsAsmPrinter::getTargetStreamer() const {
Lang Hames9ff69c82015-04-24 19:11:51 +000073 return static_cast<MipsTargetStreamer &>(*OutStreamer->getTargetStreamer());
Rafael Espindolaa17151a2013-10-08 13:08:17 +000074}
75
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000076bool MipsAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Eric Christopher3ee30d02015-02-20 08:39:06 +000077 Subtarget = &MF.getSubtarget<MipsSubtarget>();
Eric Christopher8ef7a6a2014-07-18 00:08:53 +000078
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000079 MipsFI = MF.getInfo<MipsFunctionInfo>();
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000080 if (Subtarget->inMips16Mode())
81 for (std::map<
82 const char *,
Eugene Zelenko79220eae2017-08-03 22:12:30 +000083 const Mips16HardFloatInfo::FuncSignature *>::const_iterator
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000084 it = MipsFI->StubsNeeded.begin();
85 it != MipsFI->StubsNeeded.end(); ++it) {
86 const char *Symbol = it->first;
Eugene Zelenko79220eae2017-08-03 22:12:30 +000087 const Mips16HardFloatInfo::FuncSignature *Signature = it->second;
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000088 if (StubsNeeded.find(Symbol) == StubsNeeded.end())
89 StubsNeeded[Symbol] = Signature;
90 }
Reed Kotler91ae9822013-10-27 21:57:36 +000091 MCP = MF.getConstantPool();
Sasa Stankovic8c5736b2014-02-28 10:00:38 +000092
93 // In NaCl, all indirect jump targets must be aligned to bundle size.
94 if (Subtarget->isTargetNaCl())
95 NaClAlignIndirectJumpTargets(MF);
96
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000097 AsmPrinter::runOnMachineFunction(MF);
Sagar Thakurec657922017-02-15 10:48:11 +000098
Simon Dardis080d4782017-05-04 11:03:50 +000099 emitXRayTable();
Sagar Thakurec657922017-02-15 10:48:11 +0000100
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +0000101 return true;
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000102}
103
Akira Hatanaka42a35242012-09-27 01:59:07 +0000104bool MipsAsmPrinter::lowerOperand(const MachineOperand &MO, MCOperand &MCOp) {
105 MCOp = MCInstLowering.LowerOperand(MO);
106 return MCOp.isValid();
107}
108
109#include "MipsGenMCPseudoLowering.inc"
110
Daniel Sandersf5a5fbd2014-07-09 10:21:59 +0000111// Lower PseudoReturn/PseudoIndirectBranch/PseudoIndirectBranch64 to JR, JR_MM,
Aleksandar Beserminji7d610f42017-09-14 14:34:04 +0000112// JALR, or JALR64 as appropriate for the target.
Daniel Sandersf5a5fbd2014-07-09 10:21:59 +0000113void MipsAsmPrinter::emitPseudoIndirectBranch(MCStreamer &OutStreamer,
114 const MachineInstr *MI) {
Daniel Sanders338513b2014-07-09 10:16:07 +0000115 bool HasLinkReg = false;
Simon Dardisea343152016-08-18 13:22:43 +0000116 bool InMicroMipsMode = Subtarget->inMicroMipsMode();
Daniel Sanders338513b2014-07-09 10:16:07 +0000117 MCInst TmpInst0;
118
119 if (Subtarget->hasMips64r6()) {
120 // MIPS64r6 should use (JALR64 ZERO_64, $rs)
121 TmpInst0.setOpcode(Mips::JALR64);
122 HasLinkReg = true;
123 } else if (Subtarget->hasMips32r6()) {
124 // MIPS32r6 should use (JALR ZERO, $rs)
Simon Dardisea343152016-08-18 13:22:43 +0000125 if (InMicroMipsMode)
126 TmpInst0.setOpcode(Mips::JRC16_MMR6);
127 else {
128 TmpInst0.setOpcode(Mips::JALR);
129 HasLinkReg = true;
130 }
Daniel Sanders338513b2014-07-09 10:16:07 +0000131 } else if (Subtarget->inMicroMipsMode())
132 // microMIPS should use (JR_MM $rs)
133 TmpInst0.setOpcode(Mips::JR_MM);
134 else {
135 // Everything else should use (JR $rs)
136 TmpInst0.setOpcode(Mips::JR);
137 }
138
139 MCOperand MCOp;
140
141 if (HasLinkReg) {
142 unsigned ZeroReg = Subtarget->isGP64bit() ? Mips::ZERO_64 : Mips::ZERO;
Jim Grosbache9119e42015-05-13 18:37:00 +0000143 TmpInst0.addOperand(MCOperand::createReg(ZeroReg));
Daniel Sanders338513b2014-07-09 10:16:07 +0000144 }
145
146 lowerOperand(MI->getOperand(0), MCOp);
147 TmpInst0.addOperand(MCOp);
148
149 EmitToStreamer(OutStreamer, TmpInst0);
150}
151
Vladimir Stefanovic3daf8bc2019-01-17 21:50:37 +0000152// If there is an MO_JALR operand, insert:
153//
154// .reloc tmplabel, R_{MICRO}MIPS_JALR, symbol
155// tmplabel:
156//
157// This is an optimization hint for the linker which may then replace
158// an indirect call with a direct branch.
159static void emitDirectiveRelocJalr(const MachineInstr &MI,
160 MCContext &OutContext,
161 TargetMachine &TM,
162 MCStreamer &OutStreamer,
163 const MipsSubtarget &Subtarget) {
164 for (unsigned int I = MI.getDesc().getNumOperands(), E = MI.getNumOperands();
165 I < E; ++I) {
166 MachineOperand MO = MI.getOperand(I);
167 if (MO.isMCSymbol() && (MO.getTargetFlags() & MipsII::MO_JALR)) {
168 MCSymbol *Callee = MO.getMCSymbol();
169 if (Callee && !Callee->getName().empty()) {
170 MCSymbol *OffsetLabel = OutContext.createTempSymbol();
171 const MCExpr *OffsetExpr =
172 MCSymbolRefExpr::create(OffsetLabel, OutContext);
173 const MCExpr *CaleeExpr =
174 MCSymbolRefExpr::create(Callee, OutContext);
175 OutStreamer.EmitRelocDirective
176 (*OffsetExpr,
177 Subtarget.inMicroMipsMode() ? "R_MICROMIPS_JALR" : "R_MIPS_JALR",
178 CaleeExpr, SMLoc(), *TM.getMCSubtargetInfo());
179 OutStreamer.EmitLabel(OffsetLabel);
180 return;
181 }
182 }
183 }
184}
185
Akira Hatanakaddd12652011-07-07 20:10:52 +0000186void MipsAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000187 MipsTargetStreamer &TS = getTargetStreamer();
Sagar Thakurec657922017-02-15 10:48:11 +0000188 unsigned Opc = MI->getOpcode();
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000189 TS.forbidModuleDirective();
Daniel Sandersc7dbc632014-07-08 10:11:38 +0000190
Akira Hatanakaddd12652011-07-07 20:10:52 +0000191 if (MI->isDebugValue()) {
Bruno Cardoso Lopescd1d4472011-12-30 21:09:41 +0000192 SmallString<128> Str;
193 raw_svector_ostream OS(Str);
194
Akira Hatanakaddd12652011-07-07 20:10:52 +0000195 PrintDebugValueComment(MI, OS);
196 return;
197 }
Shiva Chen801bf7e2018-05-09 02:42:00 +0000198 if (MI->isDebugLabel())
199 return;
Akira Hatanakaddd12652011-07-07 20:10:52 +0000200
Reed Kotler91ae9822013-10-27 21:57:36 +0000201 // If we just ended a constant pool, mark it as such.
Sagar Thakurec657922017-02-15 10:48:11 +0000202 if (InConstantPool && Opc != Mips::CONSTPOOL_ENTRY) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000203 OutStreamer->EmitDataRegion(MCDR_DataRegionEnd);
Reed Kotler91ae9822013-10-27 21:57:36 +0000204 InConstantPool = false;
205 }
Sagar Thakurec657922017-02-15 10:48:11 +0000206 if (Opc == Mips::CONSTPOOL_ENTRY) {
Reed Kotler91ae9822013-10-27 21:57:36 +0000207 // CONSTPOOL_ENTRY - This instruction represents a floating
Sagar Thakurec657922017-02-15 10:48:11 +0000208 // constant pool in the function. The first operand is the ID#
Reed Kotler91ae9822013-10-27 21:57:36 +0000209 // for this instruction, the second is the index into the
210 // MachineConstantPool that this is, the third is the size in
211 // bytes of this constant pool entry.
212 // The required alignment is specified on the basic block holding this MI.
213 //
214 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
Sagar Thakurec657922017-02-15 10:48:11 +0000215 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
Reed Kotler91ae9822013-10-27 21:57:36 +0000216
217 // If this is the first entry of the pool, mark it.
218 if (!InConstantPool) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000219 OutStreamer->EmitDataRegion(MCDR_DataRegion);
Reed Kotler91ae9822013-10-27 21:57:36 +0000220 InConstantPool = true;
221 }
222
Lang Hames9ff69c82015-04-24 19:11:51 +0000223 OutStreamer->EmitLabel(GetCPISymbol(LabelId));
Reed Kotler91ae9822013-10-27 21:57:36 +0000224
225 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
226 if (MCPE.isMachineConstantPoolEntry())
227 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
228 else
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000229 EmitGlobalConstant(MF->getDataLayout(), MCPE.Val.ConstVal);
Reed Kotler91ae9822013-10-27 21:57:36 +0000230 return;
231 }
232
Sagar Thakurec657922017-02-15 10:48:11 +0000233 switch (Opc) {
234 case Mips::PATCHABLE_FUNCTION_ENTER:
235 LowerPATCHABLE_FUNCTION_ENTER(*MI);
236 return;
237 case Mips::PATCHABLE_FUNCTION_EXIT:
238 LowerPATCHABLE_FUNCTION_EXIT(*MI);
239 return;
240 case Mips::PATCHABLE_TAIL_CALL:
241 LowerPATCHABLE_TAIL_CALL(*MI);
242 return;
243 }
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000244
Vladimir Stefanovic3daf8bc2019-01-17 21:50:37 +0000245 if (EmitJalrReloc &&
246 (MI->isReturn() || MI->isCall() || MI->isIndirectBranch())) {
247 emitDirectiveRelocJalr(*MI, OutContext, TM, *OutStreamer, *Subtarget);
248 }
249
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000250 MachineBasicBlock::const_instr_iterator I = MI->getIterator();
251 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
Akira Hatanaka5ac78682012-06-13 23:25:52 +0000252
253 do {
Akira Hatanaka556135d2013-02-06 21:50:15 +0000254 // Do any auto-generated pseudo lowerings.
Lang Hames9ff69c82015-04-24 19:11:51 +0000255 if (emitPseudoExpansionLowering(*OutStreamer, &*I))
Akira Hatanaka556135d2013-02-06 21:50:15 +0000256 continue;
Jack Carterc20a21b2012-08-28 19:07:39 +0000257
Daniel Sanders338513b2014-07-09 10:16:07 +0000258 if (I->getOpcode() == Mips::PseudoReturn ||
Daniel Sandersf5a5fbd2014-07-09 10:21:59 +0000259 I->getOpcode() == Mips::PseudoReturn64 ||
260 I->getOpcode() == Mips::PseudoIndirectBranch ||
Simon Dardisea343152016-08-18 13:22:43 +0000261 I->getOpcode() == Mips::PseudoIndirectBranch64 ||
262 I->getOpcode() == Mips::TAILCALLREG ||
263 I->getOpcode() == Mips::TAILCALLREG64) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000264 emitPseudoIndirectBranch(*OutStreamer, &*I);
Daniel Sanders338513b2014-07-09 10:16:07 +0000265 continue;
266 }
267
Reed Kotler76c9bcd2013-02-15 21:05:58 +0000268 // The inMips16Mode() test is not permanent.
269 // Some instructions are marked as pseudo right now which
270 // would make the test fail for the wrong reason but
271 // that will be fixed soon. We need this here because we are
272 // removing another test for this situation downstream in the
273 // callchain.
274 //
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000275 if (I->isPseudo() && !Subtarget->inMips16Mode()
276 && !isLongBranchPseudo(I->getOpcode()))
Reed Kotler76c9bcd2013-02-15 21:05:58 +0000277 llvm_unreachable("Pseudo opcode found in EmitInstruction()");
278
Akira Hatanaka556135d2013-02-06 21:50:15 +0000279 MCInst TmpInst0;
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +0000280 MCInstLowering.Lower(&*I, TmpInst0);
Lang Hames9ff69c82015-04-24 19:11:51 +0000281 EmitToStreamer(*OutStreamer, TmpInst0);
Akira Hatanaka556135d2013-02-06 21:50:15 +0000282 } while ((++I != E) && I->isInsideBundle()); // Delay slot check
Akira Hatanakaddd12652011-07-07 20:10:52 +0000283}
284
Akira Hatanakae2489122011-04-15 21:51:11 +0000285//===----------------------------------------------------------------------===//
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000286//
287// Mips Asm Directives
288//
289// -- Frame directive "frame Stackpointer, Stacksize, RARegister"
290// Describe the stack frame.
291//
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000292// -- Mask directives "(f)mask bitmask, offset"
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000293// Tells the assembler which registers are saved and where.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000294// bitmask - contain a little endian bitset indicating which registers are
295// saved on function prologue (e.g. with a 0x80000000 mask, the
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000296// assembler knows the register 31 (RA) is saved at prologue.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000297// offset - the position before stack pointer subtraction indicating where
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000298// the first saved register on prologue is located. (e.g. with a
299//
300// Consider the following function prologue:
301//
Bill Wendling97925ec2008-02-27 06:33:05 +0000302// .frame $fp,48,$ra
303// .mask 0xc0000000,-8
304// addiu $sp, $sp, -48
305// sw $ra, 40($sp)
306// sw $fp, 36($sp)
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000307//
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000308// With a 0xc0000000 mask, the assembler knows the register 31 (RA) and
309// 30 (FP) are saved at prologue. As the save order on prologue is from
310// left to right, RA is saved first. A -8 offset means that after the
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000311// stack pointer subtration, the first register in the mask (RA) will be
312// saved at address 48-8=40.
313//
Akira Hatanakae2489122011-04-15 21:51:11 +0000314//===----------------------------------------------------------------------===//
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000315
Akira Hatanakae2489122011-04-15 21:51:11 +0000316//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000317// Mask directives
Akira Hatanakae2489122011-04-15 21:51:11 +0000318//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000319
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000320// Create a bitmask with all callee saved registers for CPU or Floating Point
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000321// registers. For CPU registers consider RA, GP and FP for saving if necessary.
Rafael Espindola25fa2912014-01-27 04:33:11 +0000322void MipsAsmPrinter::printSavedRegsBitmask() {
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000323 // CPU and FPU Saved Registers Bitmasks
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000324 unsigned CPUBitmask = 0, FPUBitmask = 0;
325 int CPUTopSavedRegOff, FPUTopSavedRegOff;
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000326
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000327 // Set the CPU and FPU Bitmasks
Matthias Braun941a7052016-07-28 18:40:00 +0000328 const MachineFrameInfo &MFI = MF->getFrameInfo();
Eric Christophercba722f2015-03-21 03:13:07 +0000329 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
Matthias Braun941a7052016-07-28 18:40:00 +0000330 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000331 // size of stack area to which FP callee-saved regs are saved.
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +0000332 unsigned CPURegSize = TRI->getRegSizeInBits(Mips::GPR32RegClass) / 8;
333 unsigned FGR32RegSize = TRI->getRegSizeInBits(Mips::FGR32RegClass) / 8;
334 unsigned AFGR64RegSize = TRI->getRegSizeInBits(Mips::AFGR64RegClass) / 8;
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000335 bool HasAFGR64Reg = false;
336 unsigned CSFPRegsSize = 0;
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000337
Toma Tabacube218922015-04-09 10:54:16 +0000338 for (const auto &I : CSI) {
339 unsigned Reg = I.getReg();
Eric Christophercba722f2015-03-21 03:13:07 +0000340 unsigned RegNum = TRI->getEncodingValue(Reg);
Toma Tabacube218922015-04-09 10:54:16 +0000341
342 // If it's a floating point register, set the FPU Bitmask.
343 // If it's a general purpose register, set the CPU Bitmask.
344 if (Mips::FGR32RegClass.contains(Reg)) {
345 FPUBitmask |= (1 << RegNum);
346 CSFPRegsSize += FGR32RegSize;
347 } else if (Mips::AFGR64RegClass.contains(Reg)) {
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000348 FPUBitmask |= (3 << RegNum);
349 CSFPRegsSize += AFGR64RegSize;
350 HasAFGR64Reg = true;
Toma Tabacube218922015-04-09 10:54:16 +0000351 } else if (Mips::GPR32RegClass.contains(Reg))
352 CPUBitmask |= (1 << RegNum);
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000353 }
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000354
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000355 // FP Regs are saved right below where the virtual frame pointer points to.
356 FPUTopSavedRegOff = FPUBitmask ?
357 (HasAFGR64Reg ? -AFGR64RegSize : -FGR32RegSize) : 0;
358
359 // CPU Regs are saved below FP Regs.
360 CPUTopSavedRegOff = CPUBitmask ? -CSFPRegsSize - CPURegSize : 0;
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000361
Rafael Espindola25fa2912014-01-27 04:33:11 +0000362 MipsTargetStreamer &TS = getTargetStreamer();
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000363 // Print CPUBitmask
Rafael Espindola25fa2912014-01-27 04:33:11 +0000364 TS.emitMask(CPUBitmask, CPUTopSavedRegOff);
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000365
366 // Print FPUBitmask
Rafael Espindola25fa2912014-01-27 04:33:11 +0000367 TS.emitFMask(FPUBitmask, FPUTopSavedRegOff);
Bruno Cardoso Lopesbcda5e22007-07-11 23:24:41 +0000368}
369
Akira Hatanakae2489122011-04-15 21:51:11 +0000370//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000371// Frame and Set directives
Akira Hatanakae2489122011-04-15 21:51:11 +0000372//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000373
374/// Frame Directive
Chris Lattner5e596182010-04-04 07:05:53 +0000375void MipsAsmPrinter::emitFrameDirective() {
Eric Christophercba722f2015-03-21 03:13:07 +0000376 const TargetRegisterInfo &RI = *MF->getSubtarget().getRegisterInfo();
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000377
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000378 unsigned stackReg = RI.getFrameRegister(*MF);
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000379 unsigned returnReg = RI.getRARegister();
Matthias Braun941a7052016-07-28 18:40:00 +0000380 unsigned stackSize = MF->getFrameInfo().getStackSize();
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000381
Rafael Espindola054234f2014-01-27 03:53:56 +0000382 getTargetStreamer().emitFrame(stackReg, stackSize, returnReg);
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000383}
384
385/// Emit Set directives.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000386const char *MipsAsmPrinter::getCurrentABIString() const {
Eric Christophera5762812015-01-26 17:33:46 +0000387 switch (static_cast<MipsTargetMachine &>(TM).getABI().GetEnumValue()) {
Daniel Sanderse2e25da2014-10-24 16:15:27 +0000388 case MipsABIInfo::ABI::O32: return "abi32";
389 case MipsABIInfo::ABI::N32: return "abiN32";
390 case MipsABIInfo::ABI::N64: return "abi64";
Dmitri Gribenkoca1e27b2012-09-10 21:26:47 +0000391 default: llvm_unreachable("Unknown Mips ABI");
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000392 }
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000393}
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000394
Chris Lattner5d9fb4b2010-01-27 23:23:58 +0000395void MipsAsmPrinter::EmitFunctionEntryLabel() {
Rafael Espindola6633d572014-01-14 18:57:12 +0000396 MipsTargetStreamer &TS = getTargetStreamer();
Sasa Stankovic8c5736b2014-02-28 10:00:38 +0000397
398 // NaCl sandboxing requires that indirect call instructions are masked.
399 // This means that function entry points should be bundle-aligned.
400 if (Subtarget->isTargetNaCl())
401 EmitAlignment(std::max(MF->getAlignment(), MIPS_NACL_BUNDLE_ALIGN));
402
Daniel Sanders1d148642016-06-16 09:17:03 +0000403 if (Subtarget->inMicroMipsMode()) {
Rafael Espindola6633d572014-01-14 18:57:12 +0000404 TS.emitDirectiveSetMicroMips();
Daniel Sanders1d148642016-06-16 09:17:03 +0000405 TS.setUsesMicroMips();
Aleksandar Beserminji590f0792017-11-24 14:00:47 +0000406 TS.updateABIInfo(*Subtarget);
Daniel Sanders1d148642016-06-16 09:17:03 +0000407 } else
Matheus Almeidadc7e48e2014-04-16 11:46:59 +0000408 TS.emitDirectiveSetNoMicroMips();
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000409
Rafael Espindola6633d572014-01-14 18:57:12 +0000410 if (Subtarget->inMips16Mode())
411 TS.emitDirectiveSetMips16();
412 else
413 TS.emitDirectiveSetNoMips16();
Jack Carterab3cb422013-02-19 22:04:37 +0000414
Rafael Espindola6633d572014-01-14 18:57:12 +0000415 TS.emitDirectiveEnt(*CurrentFnSym);
Lang Hames9ff69c82015-04-24 19:11:51 +0000416 OutStreamer->EmitLabel(CurrentFnSym);
Chris Lattner5d9fb4b2010-01-27 23:23:58 +0000417}
418
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000419/// EmitFunctionBodyStart - Targets can override this to emit stuff before
420/// the first basic block in the function.
421void MipsAsmPrinter::EmitFunctionBodyStart() {
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000422 MipsTargetStreamer &TS = getTargetStreamer();
423
Rafael Espindola7d78b2a2013-10-29 16:24:21 +0000424 MCInstLowering.Initialize(&MF->getContext());
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +0000425
Matthias Braunf1caa282017-12-15 22:22:58 +0000426 bool IsNakedFunction = MF->getFunction().hasFnAttribute(Attribute::Naked);
Reed Kotler0f2b10e2013-05-03 23:17:24 +0000427 if (!IsNakedFunction)
428 emitFrameDirective();
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000429
Rafael Espindola25fa2912014-01-27 04:33:11 +0000430 if (!IsNakedFunction)
431 printSavedRegsBitmask();
432
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000433 if (!Subtarget->inMips16Mode()) {
434 TS.emitDirectiveSetNoReorder();
435 TS.emitDirectiveSetNoMacro();
436 TS.emitDirectiveSetNoAt();
Akira Hatanaka8f3573032012-05-12 00:48:43 +0000437 }
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000438}
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000439
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000440/// EmitFunctionBodyEnd - Targets can override this to emit stuff after
441/// the last basic block in the function.
442void MipsAsmPrinter::EmitFunctionBodyEnd() {
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000443 MipsTargetStreamer &TS = getTargetStreamer();
444
Chris Lattnerfd97a332010-01-28 01:48:52 +0000445 // There are instruction for this macros, but they must
446 // always be at the function end, and we can't emit and
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000447 // break with BB logic.
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000448 if (!Subtarget->inMips16Mode()) {
449 TS.emitDirectiveSetAt();
450 TS.emitDirectiveSetMacro();
451 TS.emitDirectiveSetReorder();
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000452 }
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000453 TS.emitDirectiveEnd(CurrentFnSym->getName());
Reed Kotler91ae9822013-10-27 21:57:36 +0000454 // Make sure to terminate any constant pools that were at the end
455 // of the function.
456 if (!InConstantPool)
457 return;
458 InConstantPool = false;
Lang Hames9ff69c82015-04-24 19:11:51 +0000459 OutStreamer->EmitDataRegion(MCDR_DataRegionEnd);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000460}
461
Vasileios Kalintiris42544d62015-05-08 09:10:15 +0000462void MipsAsmPrinter::EmitBasicBlockEnd(const MachineBasicBlock &MBB) {
Omer Paparo Bivas2251c792017-10-24 06:16:03 +0000463 AsmPrinter::EmitBasicBlockEnd(MBB);
Vasileios Kalintiris42544d62015-05-08 09:10:15 +0000464 MipsTargetStreamer &TS = getTargetStreamer();
Eugene Zelenko79220eae2017-08-03 22:12:30 +0000465 if (MBB.empty())
Vasileios Kalintiris42544d62015-05-08 09:10:15 +0000466 TS.emitDirectiveInsn();
467}
468
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000469/// isBlockOnlyReachableByFallthough - Return true if the basic block has
470/// exactly one predecessor and the control transfer mechanism between
471/// the predecessor and this block is a fall-through.
Akira Hatanakae2489122011-04-15 21:51:11 +0000472bool MipsAsmPrinter::isBlockOnlyReachableByFallthrough(const MachineBasicBlock*
473 MBB) const {
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000474 // The predecessor has to be immediately before this block.
475 const MachineBasicBlock *Pred = *MBB->pred_begin();
476
477 // If the predecessor is a switch statement, assume a jump table
478 // implementation, so it is not a fall through.
479 if (const BasicBlock *bb = Pred->getBasicBlock())
480 if (isa<SwitchInst>(bb->getTerminator()))
481 return false;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000482
Akira Hatanakae625ba42011-04-01 18:57:38 +0000483 // If this is a landing pad, it isn't a fall through. If it has no preds,
484 // then nothing falls through to it.
Reid Kleckner0e288232015-08-27 23:27:47 +0000485 if (MBB->isEHPad() || MBB->pred_empty())
Akira Hatanakae625ba42011-04-01 18:57:38 +0000486 return false;
487
488 // If there isn't exactly one predecessor, it can't be a fall through.
489 MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI;
490 ++PI2;
Jia Liuf54f60f2012-02-28 07:46:26 +0000491
Akira Hatanakae625ba42011-04-01 18:57:38 +0000492 if (PI2 != MBB->pred_end())
Jia Liuf54f60f2012-02-28 07:46:26 +0000493 return false;
Akira Hatanakae625ba42011-04-01 18:57:38 +0000494
495 // The predecessor has to be immediately before this block.
496 if (!Pred->isLayoutSuccessor(MBB))
497 return false;
Jia Liuf54f60f2012-02-28 07:46:26 +0000498
Akira Hatanakae625ba42011-04-01 18:57:38 +0000499 // If the block is completely empty, then it definitely does fall through.
500 if (Pred->empty())
501 return true;
Jia Liuf54f60f2012-02-28 07:46:26 +0000502
Akira Hatanakae625ba42011-04-01 18:57:38 +0000503 // Otherwise, check the last instruction.
504 // Check if the last terminator is an unconditional branch.
505 MachineBasicBlock::const_iterator I = Pred->end();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000506 while (I != Pred->begin() && !(--I)->isTerminator()) ;
Akira Hatanakae625ba42011-04-01 18:57:38 +0000507
Evan Cheng7f8e5632011-12-07 07:15:52 +0000508 return !I->isBarrier();
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000509}
510
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000511// Print out an operand for an inline asm expression.
Eric Christophered51b9e2012-05-10 21:48:22 +0000512bool MipsAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000513 unsigned AsmVariant, const char *ExtraCode,
Chris Lattner3bb09762010-04-04 05:29:35 +0000514 raw_ostream &O) {
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000515 // Does this asm operand have a single letter operand modifier?
Eric Christophered51b9e2012-05-10 21:48:22 +0000516 if (ExtraCode && ExtraCode[0]) {
517 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000518
Eric Christophered51b9e2012-05-10 21:48:22 +0000519 const MachineOperand &MO = MI->getOperand(OpNum);
520 switch (ExtraCode[0]) {
Eric Christopherbc5d2492012-05-19 00:51:56 +0000521 default:
Jack Carterb2fd5f62012-06-21 17:14:46 +0000522 // See if this is a generic print operand
523 return AsmPrinter::PrintAsmOperand(MI,OpNum,AsmVariant,ExtraCode,O);
Eric Christopherbc5d2492012-05-19 00:51:56 +0000524 case 'X': // hex const int
525 if ((MO.getType()) != MachineOperand::MO_Immediate)
526 return true;
Benjamin Kramer33b46912015-05-23 16:53:07 +0000527 O << "0x" << Twine::utohexstr(MO.getImm());
Eric Christopherbc5d2492012-05-19 00:51:56 +0000528 return false;
529 case 'x': // hex const int (low 16 bits)
530 if ((MO.getType()) != MachineOperand::MO_Immediate)
531 return true;
Benjamin Kramer33b46912015-05-23 16:53:07 +0000532 O << "0x" << Twine::utohexstr(MO.getImm() & 0xffff);
Eric Christopherbc5d2492012-05-19 00:51:56 +0000533 return false;
534 case 'd': // decimal const int
535 if ((MO.getType()) != MachineOperand::MO_Immediate)
536 return true;
537 O << MO.getImm();
538 return false;
Eric Christopherf481ab32012-05-30 19:05:19 +0000539 case 'm': // decimal const int minus 1
540 if ((MO.getType()) != MachineOperand::MO_Immediate)
541 return true;
542 O << MO.getImm() - 1;
543 return false;
Simon Atanasyan70498f82018-02-07 12:36:39 +0000544 case 'y': // exact log2
545 if ((MO.getType()) != MachineOperand::MO_Immediate)
546 return true;
547 if (!isPowerOf2_64(MO.getImm()))
548 return true;
549 O << Log2_64(MO.getImm());
550 return false;
Eugene Zelenko79220eae2017-08-03 22:12:30 +0000551 case 'z':
Jack Carter27747b52012-06-28 20:46:26 +0000552 // $0 if zero, regular printing otherwise
Toma Tabacu27cab752014-11-06 14:25:42 +0000553 if (MO.getType() == MachineOperand::MO_Immediate && MO.getImm() == 0) {
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000554 O << "$0";
Toma Tabacu27cab752014-11-06 14:25:42 +0000555 return false;
556 }
557 // If not, call printOperand as normal.
558 break;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000559 case 'D': // Second part of a double word register operand
560 case 'L': // Low order register of a double word register operand
Jack Cartera62ba822012-07-18 06:41:36 +0000561 case 'M': // High order register of a double word register operand
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000562 {
Jack Carterb2af5122012-07-05 23:58:21 +0000563 if (OpNum == 0)
564 return true;
565 const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
566 if (!FlagsOP.isImm())
567 return true;
568 unsigned Flags = FlagsOP.getImm();
569 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Jack Carter2ab73b12012-07-06 02:44:22 +0000570 // Number of registers represented by this operand. We are looking
571 // for 2 for 32 bit mode and 1 for 64 bit mode.
Jack Carterb2af5122012-07-05 23:58:21 +0000572 if (NumVals != 2) {
Jack Carter2ab73b12012-07-06 02:44:22 +0000573 if (Subtarget->isGP64bit() && NumVals == 1 && MO.isReg()) {
Jack Carterb2af5122012-07-05 23:58:21 +0000574 unsigned Reg = MO.getReg();
575 O << '$' << MipsInstPrinter::getRegisterName(Reg);
576 return false;
577 }
578 return true;
579 }
Jack Carter42ebf982012-07-11 21:41:49 +0000580
581 unsigned RegOp = OpNum;
582 if (!Subtarget->isGP64bit()){
Simon Pilgrimdcd84332016-11-18 11:53:36 +0000583 // Endianness reverses which register holds the high or low value
Jack Cartera62ba822012-07-18 06:41:36 +0000584 // between M and L.
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000585 switch(ExtraCode[0]) {
Jack Cartera62ba822012-07-18 06:41:36 +0000586 case 'M':
587 RegOp = (Subtarget->isLittle()) ? OpNum + 1 : OpNum;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000588 break;
589 case 'L':
Jack Cartera62ba822012-07-18 06:41:36 +0000590 RegOp = (Subtarget->isLittle()) ? OpNum : OpNum + 1;
591 break;
592 case 'D': // Always the second part
593 RegOp = OpNum + 1;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000594 }
595 if (RegOp >= MI->getNumOperands())
596 return true;
597 const MachineOperand &MO = MI->getOperand(RegOp);
598 if (!MO.isReg())
599 return true;
600 unsigned Reg = MO.getReg();
601 O << '$' << MipsInstPrinter::getRegisterName(Reg);
602 return false;
Jack Carterb2af5122012-07-05 23:58:21 +0000603 }
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +0000604 break;
Eric Christophered51b9e2012-05-10 21:48:22 +0000605 }
Daniel Sanders8b59af12013-11-12 12:56:01 +0000606 case 'w':
607 // Print MSA registers for the 'f' constraint
608 // In LLVM, the 'w' modifier doesn't need to do anything.
609 // We can just call printOperand as normal.
610 break;
Jack Carter2ab73b12012-07-06 02:44:22 +0000611 }
612 }
Eric Christophered51b9e2012-05-10 21:48:22 +0000613
614 printOperand(MI, OpNum, O);
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000615 return false;
616}
617
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000618bool MipsAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
619 unsigned OpNum, unsigned AsmVariant,
620 const char *ExtraCode,
621 raw_ostream &O) {
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000622 assert(OpNum + 1 < MI->getNumOperands() && "Insufficient operands");
623 const MachineOperand &BaseMO = MI->getOperand(OpNum);
624 const MachineOperand &OffsetMO = MI->getOperand(OpNum + 1);
625 assert(BaseMO.isReg() && "Unexpected base pointer for inline asm memory operand.");
626 assert(OffsetMO.isImm() && "Unexpected offset for inline asm memory operand.");
627 int Offset = OffsetMO.getImm();
628
Simon Atanasyan737bec32018-02-07 12:36:33 +0000629 // Currently we are expecting either no ExtraCode or 'D','M','L'.
Jack Carterb04e3572013-04-09 23:19:50 +0000630 if (ExtraCode) {
Simon Atanasyan737bec32018-02-07 12:36:33 +0000631 switch (ExtraCode[0]) {
632 case 'D':
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000633 Offset += 4;
Simon Atanasyan737bec32018-02-07 12:36:33 +0000634 break;
635 case 'M':
636 if (Subtarget->isLittle())
637 Offset += 4;
638 break;
639 case 'L':
640 if (!Subtarget->isLittle())
641 Offset += 4;
642 break;
643 default:
Jack Carterb04e3572013-04-09 23:19:50 +0000644 return true; // Unknown modifier.
Simon Atanasyan737bec32018-02-07 12:36:33 +0000645 }
Jack Carterb04e3572013-04-09 23:19:50 +0000646 }
Jia Liuf54f60f2012-02-28 07:46:26 +0000647
Simon Atanasyan737bec32018-02-07 12:36:33 +0000648 O << Offset << "($" << MipsInstPrinter::getRegisterName(BaseMO.getReg())
649 << ")";
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000650
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000651 return false;
652}
653
Chris Lattner76c564b2010-04-04 04:47:45 +0000654void MipsAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
655 raw_ostream &O) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000656 const MachineOperand &MO = MI->getOperand(opNum);
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +0000657 bool closeP = false;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000658
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000659 if (MO.getTargetFlags())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000660 closeP = true;
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000661
662 switch(MO.getTargetFlags()) {
663 case MipsII::MO_GPREL: O << "%gp_rel("; break;
664 case MipsII::MO_GOT_CALL: O << "%call16("; break;
Akira Hatanaka56d9ef52011-04-01 21:41:06 +0000665 case MipsII::MO_GOT: O << "%got("; break;
666 case MipsII::MO_ABS_HI: O << "%hi("; break;
667 case MipsII::MO_ABS_LO: O << "%lo("; break;
Simon Dardisca74dd72017-01-27 11:36:52 +0000668 case MipsII::MO_HIGHER: O << "%higher("; break;
669 case MipsII::MO_HIGHEST: O << "%highest(("; break;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +0000670 case MipsII::MO_TLSGD: O << "%tlsgd("; break;
671 case MipsII::MO_GOTTPREL: O << "%gottprel("; break;
672 case MipsII::MO_TPREL_HI: O << "%tprel_hi("; break;
673 case MipsII::MO_TPREL_LO: O << "%tprel_lo("; break;
Akira Hatanaka25ce3642011-09-22 03:09:07 +0000674 case MipsII::MO_GPOFF_HI: O << "%hi(%neg(%gp_rel("; break;
675 case MipsII::MO_GPOFF_LO: O << "%lo(%neg(%gp_rel("; break;
676 case MipsII::MO_GOT_DISP: O << "%got_disp("; break;
677 case MipsII::MO_GOT_PAGE: O << "%got_page("; break;
678 case MipsII::MO_GOT_OFST: O << "%got_ofst("; break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000679 }
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000680
Chris Lattnereb2cc682009-09-13 20:31:40 +0000681 switch (MO.getType()) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000682 case MachineOperand::MO_Register:
Akira Hatanaka9c6028f2011-07-07 23:56:50 +0000683 O << '$'
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000684 << StringRef(MipsInstPrinter::getRegisterName(MO.getReg())).lower();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000685 break;
686
687 case MachineOperand::MO_Immediate:
Akira Hatanaka2db176c2011-05-24 21:22:21 +0000688 O << MO.getImm();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000689 break;
690
691 case MachineOperand::MO_MachineBasicBlock:
Matt Arsenault8b643552015-06-09 00:31:39 +0000692 MO.getMBB()->getSymbol()->print(O, MAI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000693 return;
694
695 case MachineOperand::MO_GlobalAddress:
Matt Arsenault8b643552015-06-09 00:31:39 +0000696 getSymbol(MO.getGlobal())->print(O, MAI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000697 break;
698
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000699 case MachineOperand::MO_BlockAddress: {
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000700 MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000701 O << BA->getName();
702 break;
703 }
704
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000705 case MachineOperand::MO_ConstantPoolIndex:
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000706 O << getDataLayout().getPrivateGlobalPrefix() << "CPI"
Chris Lattnera5bb3702007-12-30 23:10:15 +0000707 << getFunctionNumber() << "_" << MO.getIndex();
Bruno Cardoso Lopes4713b282009-11-19 06:06:13 +0000708 if (MO.getOffset())
709 O << "+" << MO.getOffset();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000710 break;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000711
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000712 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000713 llvm_unreachable("<unknown operand type>");
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000714 }
715
716 if (closeP) O << ")";
717}
718
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000719void MipsAsmPrinter::
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000720printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O) {
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000721 // Load/Store memory operands -- imm($reg)
722 // If PIC target the target is loaded as the
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +0000723 // pattern lw $25,%call16($28)
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000724
725 // opNum can be invalid if instruction has reglist as operand.
726 // MemOperand is always last operand of instruction (base + offset).
727 switch (MI->getOpcode()) {
728 default:
729 break;
730 case Mips::SWM32_MM:
731 case Mips::LWM32_MM:
732 opNum = MI->getNumOperands() - 2;
733 break;
734 }
735
Chris Lattner76c564b2010-04-04 04:47:45 +0000736 printOperand(MI, opNum+1, O);
Akira Hatanaka2e766ed2011-07-07 18:57:00 +0000737 O << "(";
738 printOperand(MI, opNum, O);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000739 O << ")";
740}
741
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000742void MipsAsmPrinter::
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000743printMemOperandEA(const MachineInstr *MI, int opNum, raw_ostream &O) {
744 // when using stack locations for not load/store instructions
745 // print the same way as all normal 3 operand instructions.
746 printOperand(MI, opNum, O);
747 O << ", ";
748 printOperand(MI, opNum+1, O);
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000749}
750
751void MipsAsmPrinter::
Simon Dardisba92b032016-09-09 11:06:01 +0000752printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
753 const char *Modifier) {
754 const MachineOperand &MO = MI->getOperand(opNum);
755 O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
756}
757
758void MipsAsmPrinter::
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000759printRegisterList(const MachineInstr *MI, int opNum, raw_ostream &O) {
760 for (int i = opNum, e = MI->getNumOperands(); i != e; ++i) {
761 if (i != opNum) O << ", ";
762 printOperand(MI, i, O);
763 }
764}
765
Bob Wilsonb633d7a2009-09-30 22:06:26 +0000766void MipsAsmPrinter::EmitStartOfAsmFile(Module &M) {
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000767 MipsTargetStreamer &TS = getTargetStreamer();
768
769 // MipsTargetStreamer has an initialization order problem when emitting an
770 // object file directly (see MipsTargetELFStreamer for full details). Work
771 // around it by re-initializing the PIC state here.
Rafael Espindola699281c2016-05-18 11:58:50 +0000772 TS.setPic(OutContext.getObjectFileInfo()->isPositionIndependent());
Eric Christopher8af49b32015-02-18 01:01:57 +0000773
774 // Compute MIPS architecture attributes based on the default subtarget
775 // that we'd have constructed. Module level directives aren't LTO
776 // clean anyhow.
777 // FIXME: For ifunc related functions we could iterate over and look
778 // for a feature string that doesn't match the default one.
Daniel Sanders50f17232015-09-15 16:17:27 +0000779 const Triple &TT = TM.getTargetTriple();
Daniel Sandersa73f1fd2015-06-10 12:11:26 +0000780 StringRef CPU = MIPS_MC::selectMipsCPU(TT, TM.getTargetCPU());
Eric Christopher8af49b32015-02-18 01:01:57 +0000781 StringRef FS = TM.getTargetFeatureString();
782 const MipsTargetMachine &MTM = static_cast<const MipsTargetMachine &>(TM);
John Baldwin1255b162017-08-14 21:49:38 +0000783 const MipsSubtarget STI(TT, CPU, FS, MTM.isLittleEndian(), MTM, 0);
Eric Christopher8af49b32015-02-18 01:01:57 +0000784
785 bool IsABICalls = STI.isABICalls();
786 const MipsABIInfo &ABI = MTM.getABI();
Daniel Sanders16fa1db2014-04-16 13:58:57 +0000787 if (IsABICalls) {
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000788 TS.emitDirectiveAbiCalls();
Daniel Sanders16fa1db2014-04-16 13:58:57 +0000789 // FIXME: This condition should be a lot more complicated that it is here.
790 // Ideally it should test for properties of the ABI and not the ABI
791 // itself.
792 // For the moment, I'm only correcting enough to make MIPS-IV work.
Simon Dardisca74dd72017-01-27 11:36:52 +0000793 if (!isPositionIndependent() && STI.hasSym32())
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000794 TS.emitDirectiveOptionPic0();
Daniel Sanders16fa1db2014-04-16 13:58:57 +0000795 }
Jack Carterf9f753c2013-06-18 19:47:15 +0000796
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000797 // Tell the assembler which ABI we are using
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000798 std::string SectionName = std::string(".mdebug.") + getCurrentABIString();
Lang Hames9ff69c82015-04-24 19:11:51 +0000799 OutStreamer->SwitchSection(
Rafael Espindolaba31e272015-01-29 17:33:21 +0000800 OutContext.getELFSection(SectionName, ELF::SHT_PROGBITS, 0));
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000801
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000802 // NaN: At the moment we only support:
803 // 1. .nan legacy (default)
804 // 2. .nan 2008
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000805 STI.isNaN2008() ? TS.emitDirectiveNaN2008()
806 : TS.emitDirectiveNaNLegacy();
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000807
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000808 // TODO: handle O64 ABI
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000809
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000810 TS.updateABIInfo(STI);
Daniel Sanders7e527422014-07-10 13:38:23 +0000811
Daniel Sanderse22244b2014-07-21 15:25:24 +0000812 // We should always emit a '.module fp=...' but binutils 2.24 does not accept
813 // it. We therefore emit it when it contradicts the ABI defaults (-mfpxx or
814 // -mfp64) and omit it otherwise.
Simon Atanasyan8cb49702019-02-26 14:45:17 +0000815 if ((ABI.IsO32() && (STI.isABI_FPXX() || STI.isFP64bit())) ||
816 STI.useSoftFloat())
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000817 TS.emitDirectiveModuleFP();
Daniel Sanderse22244b2014-07-21 15:25:24 +0000818
819 // We should always emit a '.module [no]oddspreg' but binutils 2.24 does not
820 // accept it. We therefore emit it when it contradicts the default or an
821 // option has changed the default (i.e. FPXX) and omit it otherwise.
Eric Christopher8af49b32015-02-18 01:01:57 +0000822 if (ABI.IsO32() && (!STI.useOddSPReg() || STI.isABI_FPXX()))
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000823 TS.emitDirectiveModuleOddSPReg();
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000824}
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000825
Eric Christopher64d35be2015-02-19 19:52:25 +0000826void MipsAsmPrinter::emitInlineAsmStart() const {
Toma Tabacua23f13c2014-12-17 10:56:16 +0000827 MipsTargetStreamer &TS = getTargetStreamer();
828
Toma Tabacu68e8a9c2015-01-09 15:00:30 +0000829 // GCC's choice of assembler options for inline assembly code ('at', 'macro'
830 // and 'reorder') is different from LLVM's choice for generated code ('noat',
831 // 'nomacro' and 'noreorder').
832 // In order to maintain compatibility with inline assembly code which depends
833 // on GCC's assembler options being used, we have to switch to those options
834 // for the duration of the inline assembly block and then switch back.
Toma Tabacua23f13c2014-12-17 10:56:16 +0000835 TS.emitDirectiveSetPush();
836 TS.emitDirectiveSetAt();
837 TS.emitDirectiveSetMacro();
838 TS.emitDirectiveSetReorder();
Lang Hames9ff69c82015-04-24 19:11:51 +0000839 OutStreamer->AddBlankLine();
Toma Tabacua23f13c2014-12-17 10:56:16 +0000840}
841
842void MipsAsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
843 const MCSubtargetInfo *EndInfo) const {
Lang Hames9ff69c82015-04-24 19:11:51 +0000844 OutStreamer->AddBlankLine();
Toma Tabacua23f13c2014-12-17 10:56:16 +0000845 getTargetStreamer().emitDirectiveSetPop();
846}
847
Eric Christopher327fc972015-02-21 08:48:22 +0000848void MipsAsmPrinter::EmitJal(const MCSubtargetInfo &STI, MCSymbol *Symbol) {
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000849 MCInst I;
850 I.setOpcode(Mips::JAL);
851 I.addOperand(
Jim Grosbach13760bd2015-05-30 01:25:56 +0000852 MCOperand::createExpr(MCSymbolRefExpr::create(Symbol, OutContext)));
Lang Hames9ff69c82015-04-24 19:11:51 +0000853 OutStreamer->EmitInstruction(I, STI);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000854}
855
Eric Christopher327fc972015-02-21 08:48:22 +0000856void MipsAsmPrinter::EmitInstrReg(const MCSubtargetInfo &STI, unsigned Opcode,
857 unsigned Reg) {
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000858 MCInst I;
859 I.setOpcode(Opcode);
Jim Grosbache9119e42015-05-13 18:37:00 +0000860 I.addOperand(MCOperand::createReg(Reg));
Lang Hames9ff69c82015-04-24 19:11:51 +0000861 OutStreamer->EmitInstruction(I, STI);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000862}
863
Eric Christopher327fc972015-02-21 08:48:22 +0000864void MipsAsmPrinter::EmitInstrRegReg(const MCSubtargetInfo &STI,
865 unsigned Opcode, unsigned Reg1,
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000866 unsigned Reg2) {
867 MCInst I;
868 //
869 // Because of the current td files for Mips32, the operands for MTC1
870 // appear backwards from their normal assembly order. It's not a trivial
871 // change to fix this in the td file so we adjust for it here.
872 //
873 if (Opcode == Mips::MTC1) {
874 unsigned Temp = Reg1;
875 Reg1 = Reg2;
876 Reg2 = Temp;
877 }
878 I.setOpcode(Opcode);
Jim Grosbache9119e42015-05-13 18:37:00 +0000879 I.addOperand(MCOperand::createReg(Reg1));
880 I.addOperand(MCOperand::createReg(Reg2));
Lang Hames9ff69c82015-04-24 19:11:51 +0000881 OutStreamer->EmitInstruction(I, STI);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000882}
883
Eric Christopher327fc972015-02-21 08:48:22 +0000884void MipsAsmPrinter::EmitInstrRegRegReg(const MCSubtargetInfo &STI,
885 unsigned Opcode, unsigned Reg1,
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000886 unsigned Reg2, unsigned Reg3) {
887 MCInst I;
888 I.setOpcode(Opcode);
Jim Grosbache9119e42015-05-13 18:37:00 +0000889 I.addOperand(MCOperand::createReg(Reg1));
890 I.addOperand(MCOperand::createReg(Reg2));
891 I.addOperand(MCOperand::createReg(Reg3));
Lang Hames9ff69c82015-04-24 19:11:51 +0000892 OutStreamer->EmitInstruction(I, STI);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000893}
894
Eric Christopher327fc972015-02-21 08:48:22 +0000895void MipsAsmPrinter::EmitMovFPIntPair(const MCSubtargetInfo &STI,
896 unsigned MovOpc, unsigned Reg1,
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000897 unsigned Reg2, unsigned FPReg1,
898 unsigned FPReg2, bool LE) {
899 if (!LE) {
900 unsigned temp = Reg1;
901 Reg1 = Reg2;
902 Reg2 = temp;
903 }
Eric Christopher327fc972015-02-21 08:48:22 +0000904 EmitInstrRegReg(STI, MovOpc, Reg1, FPReg1);
905 EmitInstrRegReg(STI, MovOpc, Reg2, FPReg2);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000906}
907
Eric Christopher327fc972015-02-21 08:48:22 +0000908void MipsAsmPrinter::EmitSwapFPIntParams(const MCSubtargetInfo &STI,
909 Mips16HardFloatInfo::FPParamVariant PV,
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000910 bool LE, bool ToFP) {
911 using namespace Mips16HardFloatInfo;
Eugene Zelenko79220eae2017-08-03 22:12:30 +0000912
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000913 unsigned MovOpc = ToFP ? Mips::MTC1 : Mips::MFC1;
914 switch (PV) {
915 case FSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000916 EmitInstrRegReg(STI, MovOpc, Mips::A0, Mips::F12);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000917 break;
918 case FFSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000919 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F14, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000920 break;
921 case FDSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000922 EmitInstrRegReg(STI, MovOpc, Mips::A0, Mips::F12);
923 EmitMovFPIntPair(STI, MovOpc, Mips::A2, Mips::A3, Mips::F14, Mips::F15, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000924 break;
925 case DSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000926 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000927 break;
928 case DDSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000929 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
930 EmitMovFPIntPair(STI, MovOpc, Mips::A2, Mips::A3, Mips::F14, Mips::F15, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000931 break;
932 case DFSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000933 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
934 EmitInstrRegReg(STI, MovOpc, Mips::A2, Mips::F14);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000935 break;
936 case NoSig:
937 return;
938 }
939}
940
Eric Christopher327fc972015-02-21 08:48:22 +0000941void MipsAsmPrinter::EmitSwapFPIntRetval(
942 const MCSubtargetInfo &STI, Mips16HardFloatInfo::FPReturnVariant RV,
943 bool LE) {
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000944 using namespace Mips16HardFloatInfo;
Eugene Zelenko79220eae2017-08-03 22:12:30 +0000945
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000946 unsigned MovOpc = Mips::MFC1;
947 switch (RV) {
948 case FRet:
Eric Christopher327fc972015-02-21 08:48:22 +0000949 EmitInstrRegReg(STI, MovOpc, Mips::V0, Mips::F0);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000950 break;
951 case DRet:
Eric Christopher327fc972015-02-21 08:48:22 +0000952 EmitMovFPIntPair(STI, MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000953 break;
954 case CFRet:
Eric Christopher327fc972015-02-21 08:48:22 +0000955 EmitMovFPIntPair(STI, MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000956 break;
957 case CDRet:
Eric Christopher327fc972015-02-21 08:48:22 +0000958 EmitMovFPIntPair(STI, MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
959 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F2, Mips::F3, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000960 break;
961 case NoFPRet:
962 break;
963 }
964}
965
966void MipsAsmPrinter::EmitFPCallStub(
967 const char *Symbol, const Mips16HardFloatInfo::FuncSignature *Signature) {
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000968 using namespace Mips16HardFloatInfo;
Eugene Zelenko79220eae2017-08-03 22:12:30 +0000969
970 MCSymbol *MSymbol = OutContext.getOrCreateSymbol(StringRef(Symbol));
Eric Christopherbb401642015-02-21 08:32:22 +0000971 bool LE = getDataLayout().isLittleEndian();
Eric Christopher327fc972015-02-21 08:48:22 +0000972 // Construct a local MCSubtargetInfo here.
973 // This is because the MachineFunction won't exist (but have not yet been
974 // freed) and since we're at the global level we can use the default
975 // constructed subtarget.
976 std::unique_ptr<MCSubtargetInfo> STI(TM.getTarget().createMCSubtargetInfo(
Daniel Sanders335487a2015-06-16 13:15:50 +0000977 TM.getTargetTriple().str(), TM.getTargetCPU(),
978 TM.getTargetFeatureString()));
Eric Christopher327fc972015-02-21 08:48:22 +0000979
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000980 //
981 // .global xxxx
982 //
Lang Hames9ff69c82015-04-24 19:11:51 +0000983 OutStreamer->EmitSymbolAttribute(MSymbol, MCSA_Global);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000984 const char *RetType;
985 //
986 // make the comment field identifying the return and parameter
987 // types of the floating point stub
988 // # Stub function to call rettype xxxx (params)
989 //
990 switch (Signature->RetSig) {
991 case FRet:
992 RetType = "float";
993 break;
994 case DRet:
995 RetType = "double";
996 break;
997 case CFRet:
998 RetType = "complex";
999 break;
1000 case CDRet:
1001 RetType = "double complex";
1002 break;
1003 case NoFPRet:
1004 RetType = "";
1005 break;
1006 }
1007 const char *Parms;
1008 switch (Signature->ParamSig) {
1009 case FSig:
1010 Parms = "float";
1011 break;
1012 case FFSig:
1013 Parms = "float, float";
1014 break;
1015 case FDSig:
1016 Parms = "float, double";
1017 break;
1018 case DSig:
1019 Parms = "double";
1020 break;
1021 case DDSig:
1022 Parms = "double, double";
1023 break;
1024 case DFSig:
1025 Parms = "double, float";
1026 break;
1027 case NoSig:
1028 Parms = "";
1029 break;
1030 }
Lang Hames9ff69c82015-04-24 19:11:51 +00001031 OutStreamer->AddComment("\t# Stub function to call " + Twine(RetType) + " " +
1032 Twine(Symbol) + " (" + Twine(Parms) + ")");
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001033 //
1034 // probably not necessary but we save and restore the current section state
1035 //
Lang Hames9ff69c82015-04-24 19:11:51 +00001036 OutStreamer->PushSection();
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001037 //
1038 // .section mips16.call.fpxxxx,"ax",@progbits
1039 //
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001040 MCSectionELF *M = OutContext.getELFSection(
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001041 ".mips16.call.fp." + std::string(Symbol), ELF::SHT_PROGBITS,
Rafael Espindolaba31e272015-01-29 17:33:21 +00001042 ELF::SHF_ALLOC | ELF::SHF_EXECINSTR);
Lang Hames9ff69c82015-04-24 19:11:51 +00001043 OutStreamer->SwitchSection(M, nullptr);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001044 //
1045 // .align 2
1046 //
Lang Hames9ff69c82015-04-24 19:11:51 +00001047 OutStreamer->EmitValueToAlignment(4);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001048 MipsTargetStreamer &TS = getTargetStreamer();
1049 //
1050 // .set nomips16
1051 // .set nomicromips
1052 //
1053 TS.emitDirectiveSetNoMips16();
1054 TS.emitDirectiveSetNoMicroMips();
1055 //
1056 // .ent __call_stub_fp_xxxx
Vladimir Medicfb8a2a92014-07-08 08:59:22 +00001057 // .type __call_stub_fp_xxxx,@function
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001058 // __call_stub_fp_xxxx:
1059 //
1060 std::string x = "__call_stub_fp_" + std::string(Symbol);
Rafael Espindolaa8695762015-06-02 00:25:12 +00001061 MCSymbolELF *Stub =
1062 cast<MCSymbolELF>(OutContext.getOrCreateSymbol(StringRef(x)));
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001063 TS.emitDirectiveEnt(*Stub);
1064 MCSymbol *MType =
Jim Grosbach6f482002015-05-18 18:43:14 +00001065 OutContext.getOrCreateSymbol("__call_stub_fp_" + Twine(Symbol));
Lang Hames9ff69c82015-04-24 19:11:51 +00001066 OutStreamer->EmitSymbolAttribute(MType, MCSA_ELF_TypeFunction);
1067 OutStreamer->EmitLabel(Stub);
Eric Christopherd5bc07e2015-02-21 08:32:38 +00001068
1069 // Only handle non-pic for now.
Rafael Espindolab0f59cb2016-06-27 17:21:46 +00001070 assert(!isPositionIndependent() &&
Eric Christopherd5bc07e2015-02-21 08:32:38 +00001071 "should not be here if we are compiling pic");
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001072 TS.emitDirectiveSetReorder();
1073 //
1074 // We need to add a MipsMCExpr class to MCTargetDesc to fully implement
1075 // stubs without raw text but this current patch is for compiler generated
1076 // functions and they all return some value.
1077 // The calling sequence for non pic is different in that case and we need
1078 // to implement %lo and %hi in order to handle the case of no return value
1079 // See the corresponding method in Mips16HardFloat for details.
1080 //
1081 // mov the return address to S2.
1082 // we have no stack space to store it and we are about to make another call.
1083 // We need to make sure that the enclosing function knows to save S2
1084 // This should have already been handled.
1085 //
1086 // Mov $18, $31
1087
Vasileios Kalintiris1c78ca62015-08-11 08:56:25 +00001088 EmitInstrRegRegReg(*STI, Mips::OR, Mips::S2, Mips::RA, Mips::ZERO);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001089
Eric Christopher327fc972015-02-21 08:48:22 +00001090 EmitSwapFPIntParams(*STI, Signature->ParamSig, LE, true);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001091
1092 // Jal xxxx
1093 //
Eric Christopher327fc972015-02-21 08:48:22 +00001094 EmitJal(*STI, MSymbol);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001095
1096 // fix return values
Eric Christopher327fc972015-02-21 08:48:22 +00001097 EmitSwapFPIntRetval(*STI, Signature->RetSig, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001098 //
1099 // do the return
1100 // if (Signature->RetSig == NoFPRet)
1101 // llvm_unreachable("should not be any stubs here with no return value");
1102 // else
Eric Christopher327fc972015-02-21 08:48:22 +00001103 EmitInstrReg(*STI, Mips::JR, Mips::S2);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001104
Jim Grosbach6f482002015-05-18 18:43:14 +00001105 MCSymbol *Tmp = OutContext.createTempSymbol();
Lang Hames9ff69c82015-04-24 19:11:51 +00001106 OutStreamer->EmitLabel(Tmp);
Jim Grosbach13760bd2015-05-30 01:25:56 +00001107 const MCSymbolRefExpr *E = MCSymbolRefExpr::create(Stub, OutContext);
1108 const MCSymbolRefExpr *T = MCSymbolRefExpr::create(Tmp, OutContext);
1109 const MCExpr *T_min_E = MCBinaryExpr::createSub(T, E, OutContext);
Rafael Espindolaa8695762015-06-02 00:25:12 +00001110 OutStreamer->emitELFSize(Stub, T_min_E);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001111 TS.emitDirectiveEnd(x);
Lang Hames9ff69c82015-04-24 19:11:51 +00001112 OutStreamer->PopSection();
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001113}
1114
1115void MipsAsmPrinter::EmitEndOfAsmFile(Module &M) {
1116 // Emit needed stubs
1117 //
1118 for (std::map<
1119 const char *,
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001120 const Mips16HardFloatInfo::FuncSignature *>::const_iterator
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001121 it = StubsNeeded.begin();
1122 it != StubsNeeded.end(); ++it) {
1123 const char *Symbol = it->first;
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001124 const Mips16HardFloatInfo::FuncSignature *Signature = it->second;
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001125 EmitFPCallStub(Symbol, Signature);
1126 }
Rafael Espindola2ab7ea72014-01-27 01:33:33 +00001127 // return to the text section
Lang Hames9ff69c82015-04-24 19:11:51 +00001128 OutStreamer->SwitchSection(OutContext.getObjectFileInfo()->getTextSection());
Jack Carterc1b17ed2013-01-18 21:20:38 +00001129}
1130
Sagar Thakurec657922017-02-15 10:48:11 +00001131void MipsAsmPrinter::EmitSled(const MachineInstr &MI, SledKind Kind) {
1132 const uint8_t NoopsInSledCount = Subtarget->isGP64bit() ? 15 : 11;
1133 // For mips32 we want to emit the following pattern:
1134 //
1135 // .Lxray_sled_N:
1136 // ALIGN
1137 // B .tmpN
1138 // 11 NOP instructions (44 bytes)
Fangrui Songf78650a2018-07-30 19:41:25 +00001139 // ADDIU T9, T9, 52
Sagar Thakurec657922017-02-15 10:48:11 +00001140 // .tmpN
1141 //
1142 // We need the 44 bytes (11 instructions) because at runtime, we'd
1143 // be patching over the full 48 bytes (12 instructions) with the following
1144 // pattern:
1145 //
NAKAMURA Takumi6f43bd42017-10-18 13:31:28 +00001146 // ADDIU SP, SP, -8
Sagar Thakurec657922017-02-15 10:48:11 +00001147 // NOP
NAKAMURA Takumi6f43bd42017-10-18 13:31:28 +00001148 // SW RA, 4(SP)
Sagar Thakurec657922017-02-15 10:48:11 +00001149 // SW T9, 0(SP)
1150 // LUI T9, %hi(__xray_FunctionEntry/Exit)
1151 // ORI T9, T9, %lo(__xray_FunctionEntry/Exit)
1152 // LUI T0, %hi(function_id)
NAKAMURA Takumi6f43bd42017-10-18 13:31:28 +00001153 // JALR T9
1154 // ORI T0, T0, %lo(function_id)
1155 // LW T9, 0(SP)
Sagar Thakurec657922017-02-15 10:48:11 +00001156 // LW RA, 4(SP)
1157 // ADDIU SP, SP, 8
1158 //
1159 // We add 52 bytes to t9 because we want to adjust the function pointer to
1160 // the actual start of function i.e. the address just after the noop sled.
1161 // We do this because gp displacement relocation is emitted at the start of
1162 // of the function i.e after the nop sled and to correctly calculate the
1163 // global offset table address, t9 must hold the address of the instruction
1164 // containing the gp displacement relocation.
1165 // FIXME: Is this correct for the static relocation model?
1166 //
1167 // For mips64 we want to emit the following pattern:
1168 //
1169 // .Lxray_sled_N:
1170 // ALIGN
1171 // B .tmpN
1172 // 15 NOP instructions (60 bytes)
1173 // .tmpN
1174 //
1175 // We need the 60 bytes (15 instructions) because at runtime, we'd
1176 // be patching over the full 64 bytes (16 instructions) with the following
1177 // pattern:
1178 //
1179 // DADDIU SP, SP, -16
1180 // NOP
1181 // SD RA, 8(SP)
1182 // SD T9, 0(SP)
1183 // LUI T9, %highest(__xray_FunctionEntry/Exit)
1184 // ORI T9, T9, %higher(__xray_FunctionEntry/Exit)
1185 // DSLL T9, T9, 16
1186 // ORI T9, T9, %hi(__xray_FunctionEntry/Exit)
1187 // DSLL T9, T9, 16
1188 // ORI T9, T9, %lo(__xray_FunctionEntry/Exit)
1189 // LUI T0, %hi(function_id)
1190 // JALR T9
1191 // ADDIU T0, T0, %lo(function_id)
1192 // LD T9, 0(SP)
1193 // LD RA, 8(SP)
1194 // DADDIU SP, SP, 16
1195 //
1196 OutStreamer->EmitCodeAlignment(4);
1197 auto CurSled = OutContext.createTempSymbol("xray_sled_", true);
1198 OutStreamer->EmitLabel(CurSled);
1199 auto Target = OutContext.createTempSymbol();
1200
1201 // Emit "B .tmpN" instruction, which jumps over the nop sled to the actual
1202 // start of function
1203 const MCExpr *TargetExpr = MCSymbolRefExpr::create(
1204 Target, MCSymbolRefExpr::VariantKind::VK_None, OutContext);
1205 EmitToStreamer(*OutStreamer, MCInstBuilder(Mips::BEQ)
1206 .addReg(Mips::ZERO)
1207 .addReg(Mips::ZERO)
1208 .addExpr(TargetExpr));
1209
1210 for (int8_t I = 0; I < NoopsInSledCount; I++)
1211 EmitToStreamer(*OutStreamer, MCInstBuilder(Mips::SLL)
1212 .addReg(Mips::ZERO)
1213 .addReg(Mips::ZERO)
1214 .addImm(0));
1215
1216 OutStreamer->EmitLabel(Target);
1217
1218 if (!Subtarget->isGP64bit()) {
1219 EmitToStreamer(*OutStreamer,
1220 MCInstBuilder(Mips::ADDiu)
1221 .addReg(Mips::T9)
1222 .addReg(Mips::T9)
1223 .addImm(0x34));
1224 }
1225
1226 recordSled(CurSled, MI, Kind);
1227}
1228
Sagar Thakurec657922017-02-15 10:48:11 +00001229void MipsAsmPrinter::LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr &MI) {
1230 EmitSled(MI, SledKind::FUNCTION_ENTER);
1231}
1232
1233void MipsAsmPrinter::LowerPATCHABLE_FUNCTION_EXIT(const MachineInstr &MI) {
1234 EmitSled(MI, SledKind::FUNCTION_EXIT);
1235}
1236
1237void MipsAsmPrinter::LowerPATCHABLE_TAIL_CALL(const MachineInstr &MI) {
1238 EmitSled(MI, SledKind::TAIL_CALL);
1239}
1240
Akira Hatanakaf2bcad92011-07-01 01:04:43 +00001241void MipsAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
1242 raw_ostream &OS) {
1243 // TODO: implement
1244}
1245
Petar Jovanovicdbb39352017-01-20 17:53:30 +00001246// Emit .dtprelword or .dtpreldword directive
1247// and value for debug thread local expression.
Simon Atanasyanaf860d42018-11-28 11:48:07 +00001248void MipsAsmPrinter::EmitDebugValue(const MCExpr *Value, unsigned Size) const {
Simon Atanasyanf76884b2018-12-03 21:54:43 +00001249 if (auto *MipsExpr = dyn_cast<MipsMCExpr>(Value)) {
1250 if (MipsExpr && MipsExpr->getKind() == MipsMCExpr::MEK_DTPREL) {
1251 switch (Size) {
1252 case 4:
1253 OutStreamer->EmitDTPRel32Value(MipsExpr->getSubExpr());
1254 break;
1255 case 8:
1256 OutStreamer->EmitDTPRel64Value(MipsExpr->getSubExpr());
1257 break;
1258 default:
1259 llvm_unreachable("Unexpected size of expression value.");
1260 }
1261 return;
1262 }
Petar Jovanovicdbb39352017-01-20 17:53:30 +00001263 }
Simon Atanasyanf76884b2018-12-03 21:54:43 +00001264 AsmPrinter::EmitDebugValue(Value, Size);
Petar Jovanovicdbb39352017-01-20 17:53:30 +00001265}
1266
Sasa Stankovic8c5736b2014-02-28 10:00:38 +00001267// Align all targets of indirect branches on bundle size. Used only if target
1268// is NaCl.
1269void MipsAsmPrinter::NaClAlignIndirectJumpTargets(MachineFunction &MF) {
1270 // Align all blocks that are jumped to through jump table.
1271 if (MachineJumpTableInfo *JtInfo = MF.getJumpTableInfo()) {
1272 const std::vector<MachineJumpTableEntry> &JT = JtInfo->getJumpTables();
1273 for (unsigned I = 0; I < JT.size(); ++I) {
1274 const std::vector<MachineBasicBlock*> &MBBs = JT[I].MBBs;
1275
1276 for (unsigned J = 0; J < MBBs.size(); ++J)
1277 MBBs[J]->setAlignment(MIPS_NACL_BUNDLE_ALIGN);
1278 }
1279 }
1280
1281 // If basic block address is taken, block can be target of indirect branch.
Vasileios Kalintiris5a971a42016-04-15 20:43:17 +00001282 for (auto &MBB : MF) {
1283 if (MBB.hasAddressTaken())
1284 MBB.setAlignment(MIPS_NACL_BUNDLE_ALIGN);
Sasa Stankovic8c5736b2014-02-28 10:00:38 +00001285 }
1286}
1287
Sasa Stankovic7b061a42014-04-30 15:06:25 +00001288bool MipsAsmPrinter::isLongBranchPseudo(int Opcode) const {
1289 return (Opcode == Mips::LONG_BRANCH_LUi
Stefan Maksimovic8d7c3512018-11-05 14:37:41 +00001290 || Opcode == Mips::LONG_BRANCH_LUi2Op
1291 || Opcode == Mips::LONG_BRANCH_LUi2Op_64
Sasa Stankovic7b061a42014-04-30 15:06:25 +00001292 || Opcode == Mips::LONG_BRANCH_ADDiu
Stefan Maksimovic8d7c3512018-11-05 14:37:41 +00001293 || Opcode == Mips::LONG_BRANCH_ADDiu2Op
1294 || Opcode == Mips::LONG_BRANCH_DADDiu
1295 || Opcode == Mips::LONG_BRANCH_DADDiu2Op);
Sasa Stankovic7b061a42014-04-30 15:06:25 +00001296}
1297
Bob Wilson5a495fe2009-06-23 23:59:40 +00001298// Force static initialization.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +00001299extern "C" void LLVMInitializeMipsAsmPrinter() {
Mehdi Aminif42454b2016-10-09 23:00:34 +00001300 RegisterAsmPrinter<MipsAsmPrinter> X(getTheMipsTarget());
1301 RegisterAsmPrinter<MipsAsmPrinter> Y(getTheMipselTarget());
1302 RegisterAsmPrinter<MipsAsmPrinter> A(getTheMips64Target());
1303 RegisterAsmPrinter<MipsAsmPrinter> B(getTheMips64elTarget());
Daniel Dunbare8338102009-07-15 20:24:03 +00001304}