blob: 2a9d96205eb96e8d26111b7be22fc7b43494dc38 [file] [log] [blame]
Jia Liu9f610112012-02-17 08:55:11 +00001//===-- MipsAsmPrinter.cpp - Mips LLVM Assembly Printer -------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00007//
Akira Hatanakae2489122011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00009//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to GAS-format MIPS assembly language.
12//
Akira Hatanakae2489122011-04-15 21:51:11 +000013//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000014
Akira Hatanaka9c6028f2011-07-07 23:56:50 +000015#include "InstPrinter/MipsInstPrinter.h"
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000016#include "MCTargetDesc/MipsBaseInfo.h"
Sasa Stankovic8c5736b2014-02-28 10:00:38 +000017#include "MCTargetDesc/MipsMCNaCl.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "Mips.h"
Jack Carterc1b17ed2013-01-18 21:20:38 +000019#include "MipsAsmPrinter.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "MipsInstrInfo.h"
21#include "MipsMCInstLower.h"
Eric Christophera5762812015-01-26 17:33:46 +000022#include "MipsTargetMachine.h"
Rafael Espindolaa17151a2013-10-08 13:08:17 +000023#include "MipsTargetStreamer.h"
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +000024#include "llvm/ADT/SmallString.h"
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +000025#include "llvm/ADT/Twine.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000026#include "llvm/CodeGen/MachineConstantPool.h"
Bruno Cardoso Lopesbcda5e22007-07-11 23:24:41 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
Jack Carterb2af5122012-07-05 23:58:21 +000028#include "llvm/CodeGen/MachineFunctionPass.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000029#include "llvm/CodeGen/MachineInstr.h"
Sasa Stankovic8c5736b2014-02-28 10:00:38 +000030#include "llvm/CodeGen/MachineJumpTableInfo.h"
Akira Hatanaka2fcc1cf2011-08-12 21:30:06 +000031#include "llvm/CodeGen/MachineMemOperand.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/BasicBlock.h"
33#include "llvm/IR/DataLayout.h"
34#include "llvm/IR/InlineAsm.h"
35#include "llvm/IR/Instructions.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000036#include "llvm/IR/Mangler.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000037#include "llvm/MC/MCAsmInfo.h"
Rafael Espindola2ab7ea72014-01-27 01:33:33 +000038#include "llvm/MC/MCContext.h"
Rafael Espindolaac4ad252013-10-05 16:42:21 +000039#include "llvm/MC/MCELFStreamer.h"
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000040#include "llvm/MC/MCExpr.h"
Akira Hatanaka9c6028f2011-07-07 23:56:50 +000041#include "llvm/MC/MCInst.h"
Sagar Thakurec657922017-02-15 10:48:11 +000042#include "llvm/MC/MCInstBuilder.h"
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000043#include "llvm/MC/MCSection.h"
Rafael Espindola2ab7ea72014-01-27 01:33:33 +000044#include "llvm/MC/MCSectionELF.h"
Rafael Espindolaa8695762015-06-02 00:25:12 +000045#include "llvm/MC/MCSymbolELF.h"
Jack Carterab3cb422013-02-19 22:04:37 +000046#include "llvm/Support/ELF.h"
Jack Carterb2af5122012-07-05 23:58:21 +000047#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000048#include "llvm/Support/raw_ostream.h"
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +000049#include "llvm/Target/TargetLoweringObjectFile.h"
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +000050#include "llvm/Target/TargetOptions.h"
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000051#include <string>
Akira Hatanakaf2bcad92011-07-01 01:04:43 +000052
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000053using namespace llvm;
54
Chandler Carruth84e68b22014-04-22 02:41:26 +000055#define DEBUG_TYPE "mips-asm-printer"
56
Toma Tabacua23f13c2014-12-17 10:56:16 +000057MipsTargetStreamer &MipsAsmPrinter::getTargetStreamer() const {
Lang Hames9ff69c82015-04-24 19:11:51 +000058 return static_cast<MipsTargetStreamer &>(*OutStreamer->getTargetStreamer());
Rafael Espindolaa17151a2013-10-08 13:08:17 +000059}
60
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000061bool MipsAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Eric Christopher3ee30d02015-02-20 08:39:06 +000062 Subtarget = &MF.getSubtarget<MipsSubtarget>();
Eric Christopher8ef7a6a2014-07-18 00:08:53 +000063
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000064 MipsFI = MF.getInfo<MipsFunctionInfo>();
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000065 if (Subtarget->inMips16Mode())
66 for (std::map<
67 const char *,
68 const llvm::Mips16HardFloatInfo::FuncSignature *>::const_iterator
69 it = MipsFI->StubsNeeded.begin();
70 it != MipsFI->StubsNeeded.end(); ++it) {
71 const char *Symbol = it->first;
72 const llvm::Mips16HardFloatInfo::FuncSignature *Signature = it->second;
73 if (StubsNeeded.find(Symbol) == StubsNeeded.end())
74 StubsNeeded[Symbol] = Signature;
75 }
Reed Kotler91ae9822013-10-27 21:57:36 +000076 MCP = MF.getConstantPool();
Sasa Stankovic8c5736b2014-02-28 10:00:38 +000077
78 // In NaCl, all indirect jump targets must be aligned to bundle size.
79 if (Subtarget->isTargetNaCl())
80 NaClAlignIndirectJumpTargets(MF);
81
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000082 AsmPrinter::runOnMachineFunction(MF);
Sagar Thakurec657922017-02-15 10:48:11 +000083
84 EmitXRayTable();
85
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000086 return true;
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +000087}
88
Akira Hatanaka42a35242012-09-27 01:59:07 +000089bool MipsAsmPrinter::lowerOperand(const MachineOperand &MO, MCOperand &MCOp) {
90 MCOp = MCInstLowering.LowerOperand(MO);
91 return MCOp.isValid();
92}
93
94#include "MipsGenMCPseudoLowering.inc"
95
Daniel Sandersf5a5fbd2014-07-09 10:21:59 +000096// Lower PseudoReturn/PseudoIndirectBranch/PseudoIndirectBranch64 to JR, JR_MM,
97// JALR, or JALR64 as appropriate for the target
98void MipsAsmPrinter::emitPseudoIndirectBranch(MCStreamer &OutStreamer,
99 const MachineInstr *MI) {
Daniel Sanders338513b2014-07-09 10:16:07 +0000100 bool HasLinkReg = false;
Simon Dardisea343152016-08-18 13:22:43 +0000101 bool InMicroMipsMode = Subtarget->inMicroMipsMode();
Daniel Sanders338513b2014-07-09 10:16:07 +0000102 MCInst TmpInst0;
103
104 if (Subtarget->hasMips64r6()) {
105 // MIPS64r6 should use (JALR64 ZERO_64, $rs)
106 TmpInst0.setOpcode(Mips::JALR64);
107 HasLinkReg = true;
108 } else if (Subtarget->hasMips32r6()) {
109 // MIPS32r6 should use (JALR ZERO, $rs)
Simon Dardisea343152016-08-18 13:22:43 +0000110 if (InMicroMipsMode)
111 TmpInst0.setOpcode(Mips::JRC16_MMR6);
112 else {
113 TmpInst0.setOpcode(Mips::JALR);
114 HasLinkReg = true;
115 }
Daniel Sanders338513b2014-07-09 10:16:07 +0000116 } else if (Subtarget->inMicroMipsMode())
117 // microMIPS should use (JR_MM $rs)
118 TmpInst0.setOpcode(Mips::JR_MM);
119 else {
120 // Everything else should use (JR $rs)
121 TmpInst0.setOpcode(Mips::JR);
122 }
123
124 MCOperand MCOp;
125
126 if (HasLinkReg) {
127 unsigned ZeroReg = Subtarget->isGP64bit() ? Mips::ZERO_64 : Mips::ZERO;
Jim Grosbache9119e42015-05-13 18:37:00 +0000128 TmpInst0.addOperand(MCOperand::createReg(ZeroReg));
Daniel Sanders338513b2014-07-09 10:16:07 +0000129 }
130
131 lowerOperand(MI->getOperand(0), MCOp);
132 TmpInst0.addOperand(MCOp);
133
134 EmitToStreamer(OutStreamer, TmpInst0);
135}
136
Akira Hatanakaddd12652011-07-07 20:10:52 +0000137void MipsAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000138 MipsTargetStreamer &TS = getTargetStreamer();
Sagar Thakurec657922017-02-15 10:48:11 +0000139 unsigned Opc = MI->getOpcode();
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000140 TS.forbidModuleDirective();
Daniel Sandersc7dbc632014-07-08 10:11:38 +0000141
Akira Hatanakaddd12652011-07-07 20:10:52 +0000142 if (MI->isDebugValue()) {
Bruno Cardoso Lopescd1d4472011-12-30 21:09:41 +0000143 SmallString<128> Str;
144 raw_svector_ostream OS(Str);
145
Akira Hatanakaddd12652011-07-07 20:10:52 +0000146 PrintDebugValueComment(MI, OS);
147 return;
148 }
149
Reed Kotler91ae9822013-10-27 21:57:36 +0000150 // If we just ended a constant pool, mark it as such.
Sagar Thakurec657922017-02-15 10:48:11 +0000151 if (InConstantPool && Opc != Mips::CONSTPOOL_ENTRY) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000152 OutStreamer->EmitDataRegion(MCDR_DataRegionEnd);
Reed Kotler91ae9822013-10-27 21:57:36 +0000153 InConstantPool = false;
154 }
Sagar Thakurec657922017-02-15 10:48:11 +0000155 if (Opc == Mips::CONSTPOOL_ENTRY) {
Reed Kotler91ae9822013-10-27 21:57:36 +0000156 // CONSTPOOL_ENTRY - This instruction represents a floating
Sagar Thakurec657922017-02-15 10:48:11 +0000157 // constant pool in the function. The first operand is the ID#
Reed Kotler91ae9822013-10-27 21:57:36 +0000158 // for this instruction, the second is the index into the
159 // MachineConstantPool that this is, the third is the size in
160 // bytes of this constant pool entry.
161 // The required alignment is specified on the basic block holding this MI.
162 //
163 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
Sagar Thakurec657922017-02-15 10:48:11 +0000164 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
Reed Kotler91ae9822013-10-27 21:57:36 +0000165
166 // If this is the first entry of the pool, mark it.
167 if (!InConstantPool) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000168 OutStreamer->EmitDataRegion(MCDR_DataRegion);
Reed Kotler91ae9822013-10-27 21:57:36 +0000169 InConstantPool = true;
170 }
171
Lang Hames9ff69c82015-04-24 19:11:51 +0000172 OutStreamer->EmitLabel(GetCPISymbol(LabelId));
Reed Kotler91ae9822013-10-27 21:57:36 +0000173
174 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
175 if (MCPE.isMachineConstantPoolEntry())
176 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
177 else
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000178 EmitGlobalConstant(MF->getDataLayout(), MCPE.Val.ConstVal);
Reed Kotler91ae9822013-10-27 21:57:36 +0000179 return;
180 }
181
Sagar Thakurec657922017-02-15 10:48:11 +0000182 switch (Opc) {
183 case Mips::PATCHABLE_FUNCTION_ENTER:
184 LowerPATCHABLE_FUNCTION_ENTER(*MI);
185 return;
186 case Mips::PATCHABLE_FUNCTION_EXIT:
187 LowerPATCHABLE_FUNCTION_EXIT(*MI);
188 return;
189 case Mips::PATCHABLE_TAIL_CALL:
190 LowerPATCHABLE_TAIL_CALL(*MI);
191 return;
192 }
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000193
194 MachineBasicBlock::const_instr_iterator I = MI->getIterator();
195 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
Akira Hatanaka5ac78682012-06-13 23:25:52 +0000196
197 do {
Akira Hatanaka556135d2013-02-06 21:50:15 +0000198 // Do any auto-generated pseudo lowerings.
Lang Hames9ff69c82015-04-24 19:11:51 +0000199 if (emitPseudoExpansionLowering(*OutStreamer, &*I))
Akira Hatanaka556135d2013-02-06 21:50:15 +0000200 continue;
Jack Carterc20a21b2012-08-28 19:07:39 +0000201
Daniel Sanders338513b2014-07-09 10:16:07 +0000202 if (I->getOpcode() == Mips::PseudoReturn ||
Daniel Sandersf5a5fbd2014-07-09 10:21:59 +0000203 I->getOpcode() == Mips::PseudoReturn64 ||
204 I->getOpcode() == Mips::PseudoIndirectBranch ||
Simon Dardisea343152016-08-18 13:22:43 +0000205 I->getOpcode() == Mips::PseudoIndirectBranch64 ||
206 I->getOpcode() == Mips::TAILCALLREG ||
207 I->getOpcode() == Mips::TAILCALLREG64) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000208 emitPseudoIndirectBranch(*OutStreamer, &*I);
Daniel Sanders338513b2014-07-09 10:16:07 +0000209 continue;
210 }
211
Reed Kotler76c9bcd2013-02-15 21:05:58 +0000212 // The inMips16Mode() test is not permanent.
213 // Some instructions are marked as pseudo right now which
214 // would make the test fail for the wrong reason but
215 // that will be fixed soon. We need this here because we are
216 // removing another test for this situation downstream in the
217 // callchain.
218 //
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000219 if (I->isPseudo() && !Subtarget->inMips16Mode()
220 && !isLongBranchPseudo(I->getOpcode()))
Reed Kotler76c9bcd2013-02-15 21:05:58 +0000221 llvm_unreachable("Pseudo opcode found in EmitInstruction()");
222
Akira Hatanaka556135d2013-02-06 21:50:15 +0000223 MCInst TmpInst0;
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +0000224 MCInstLowering.Lower(&*I, TmpInst0);
Lang Hames9ff69c82015-04-24 19:11:51 +0000225 EmitToStreamer(*OutStreamer, TmpInst0);
Akira Hatanaka556135d2013-02-06 21:50:15 +0000226 } while ((++I != E) && I->isInsideBundle()); // Delay slot check
Akira Hatanakaddd12652011-07-07 20:10:52 +0000227}
228
Akira Hatanakae2489122011-04-15 21:51:11 +0000229//===----------------------------------------------------------------------===//
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000230//
231// Mips Asm Directives
232//
233// -- Frame directive "frame Stackpointer, Stacksize, RARegister"
234// Describe the stack frame.
235//
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000236// -- Mask directives "(f)mask bitmask, offset"
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000237// Tells the assembler which registers are saved and where.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000238// bitmask - contain a little endian bitset indicating which registers are
239// saved on function prologue (e.g. with a 0x80000000 mask, the
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000240// assembler knows the register 31 (RA) is saved at prologue.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000241// offset - the position before stack pointer subtraction indicating where
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000242// the first saved register on prologue is located. (e.g. with a
243//
244// Consider the following function prologue:
245//
Bill Wendling97925ec2008-02-27 06:33:05 +0000246// .frame $fp,48,$ra
247// .mask 0xc0000000,-8
248// addiu $sp, $sp, -48
249// sw $ra, 40($sp)
250// sw $fp, 36($sp)
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000251//
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000252// With a 0xc0000000 mask, the assembler knows the register 31 (RA) and
253// 30 (FP) are saved at prologue. As the save order on prologue is from
254// left to right, RA is saved first. A -8 offset means that after the
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000255// stack pointer subtration, the first register in the mask (RA) will be
256// saved at address 48-8=40.
257//
Akira Hatanakae2489122011-04-15 21:51:11 +0000258//===----------------------------------------------------------------------===//
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000259
Akira Hatanakae2489122011-04-15 21:51:11 +0000260//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000261// Mask directives
Akira Hatanakae2489122011-04-15 21:51:11 +0000262//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000263
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000264// Create a bitmask with all callee saved registers for CPU or Floating Point
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000265// registers. For CPU registers consider RA, GP and FP for saving if necessary.
Rafael Espindola25fa2912014-01-27 04:33:11 +0000266void MipsAsmPrinter::printSavedRegsBitmask() {
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000267 // CPU and FPU Saved Registers Bitmasks
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000268 unsigned CPUBitmask = 0, FPUBitmask = 0;
269 int CPUTopSavedRegOff, FPUTopSavedRegOff;
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000270
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000271 // Set the CPU and FPU Bitmasks
Matthias Braun941a7052016-07-28 18:40:00 +0000272 const MachineFrameInfo &MFI = MF->getFrameInfo();
Eric Christophercba722f2015-03-21 03:13:07 +0000273 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
Matthias Braun941a7052016-07-28 18:40:00 +0000274 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000275 // size of stack area to which FP callee-saved regs are saved.
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000276 unsigned CPURegSize = Mips::GPR32RegClass.getSize();
Craig Topperc7242e02012-04-20 07:30:17 +0000277 unsigned FGR32RegSize = Mips::FGR32RegClass.getSize();
278 unsigned AFGR64RegSize = Mips::AFGR64RegClass.getSize();
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000279 bool HasAFGR64Reg = false;
280 unsigned CSFPRegsSize = 0;
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000281
Toma Tabacube218922015-04-09 10:54:16 +0000282 for (const auto &I : CSI) {
283 unsigned Reg = I.getReg();
Eric Christophercba722f2015-03-21 03:13:07 +0000284 unsigned RegNum = TRI->getEncodingValue(Reg);
Toma Tabacube218922015-04-09 10:54:16 +0000285
286 // If it's a floating point register, set the FPU Bitmask.
287 // If it's a general purpose register, set the CPU Bitmask.
288 if (Mips::FGR32RegClass.contains(Reg)) {
289 FPUBitmask |= (1 << RegNum);
290 CSFPRegsSize += FGR32RegSize;
291 } else if (Mips::AFGR64RegClass.contains(Reg)) {
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000292 FPUBitmask |= (3 << RegNum);
293 CSFPRegsSize += AFGR64RegSize;
294 HasAFGR64Reg = true;
Toma Tabacube218922015-04-09 10:54:16 +0000295 } else if (Mips::GPR32RegClass.contains(Reg))
296 CPUBitmask |= (1 << RegNum);
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000297 }
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000298
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000299 // FP Regs are saved right below where the virtual frame pointer points to.
300 FPUTopSavedRegOff = FPUBitmask ?
301 (HasAFGR64Reg ? -AFGR64RegSize : -FGR32RegSize) : 0;
302
303 // CPU Regs are saved below FP Regs.
304 CPUTopSavedRegOff = CPUBitmask ? -CSFPRegsSize - CPURegSize : 0;
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000305
Rafael Espindola25fa2912014-01-27 04:33:11 +0000306 MipsTargetStreamer &TS = getTargetStreamer();
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000307 // Print CPUBitmask
Rafael Espindola25fa2912014-01-27 04:33:11 +0000308 TS.emitMask(CPUBitmask, CPUTopSavedRegOff);
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000309
310 // Print FPUBitmask
Rafael Espindola25fa2912014-01-27 04:33:11 +0000311 TS.emitFMask(FPUBitmask, FPUTopSavedRegOff);
Bruno Cardoso Lopesbcda5e22007-07-11 23:24:41 +0000312}
313
Akira Hatanakae2489122011-04-15 21:51:11 +0000314//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000315// Frame and Set directives
Akira Hatanakae2489122011-04-15 21:51:11 +0000316//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000317
318/// Frame Directive
Chris Lattner5e596182010-04-04 07:05:53 +0000319void MipsAsmPrinter::emitFrameDirective() {
Eric Christophercba722f2015-03-21 03:13:07 +0000320 const TargetRegisterInfo &RI = *MF->getSubtarget().getRegisterInfo();
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000321
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000322 unsigned stackReg = RI.getFrameRegister(*MF);
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000323 unsigned returnReg = RI.getRARegister();
Matthias Braun941a7052016-07-28 18:40:00 +0000324 unsigned stackSize = MF->getFrameInfo().getStackSize();
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000325
Rafael Espindola054234f2014-01-27 03:53:56 +0000326 getTargetStreamer().emitFrame(stackReg, stackSize, returnReg);
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000327}
328
329/// Emit Set directives.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000330const char *MipsAsmPrinter::getCurrentABIString() const {
Eric Christophera5762812015-01-26 17:33:46 +0000331 switch (static_cast<MipsTargetMachine &>(TM).getABI().GetEnumValue()) {
Daniel Sanderse2e25da2014-10-24 16:15:27 +0000332 case MipsABIInfo::ABI::O32: return "abi32";
333 case MipsABIInfo::ABI::N32: return "abiN32";
334 case MipsABIInfo::ABI::N64: return "abi64";
Dmitri Gribenkoca1e27b2012-09-10 21:26:47 +0000335 default: llvm_unreachable("Unknown Mips ABI");
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000336 }
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000337}
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000338
Chris Lattner5d9fb4b2010-01-27 23:23:58 +0000339void MipsAsmPrinter::EmitFunctionEntryLabel() {
Rafael Espindola6633d572014-01-14 18:57:12 +0000340 MipsTargetStreamer &TS = getTargetStreamer();
Sasa Stankovic8c5736b2014-02-28 10:00:38 +0000341
342 // NaCl sandboxing requires that indirect call instructions are masked.
343 // This means that function entry points should be bundle-aligned.
344 if (Subtarget->isTargetNaCl())
345 EmitAlignment(std::max(MF->getAlignment(), MIPS_NACL_BUNDLE_ALIGN));
346
Daniel Sanders1d148642016-06-16 09:17:03 +0000347 if (Subtarget->inMicroMipsMode()) {
Rafael Espindola6633d572014-01-14 18:57:12 +0000348 TS.emitDirectiveSetMicroMips();
Daniel Sanders1d148642016-06-16 09:17:03 +0000349 TS.setUsesMicroMips();
350 } else
Matheus Almeidadc7e48e2014-04-16 11:46:59 +0000351 TS.emitDirectiveSetNoMicroMips();
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000352
Rafael Espindola6633d572014-01-14 18:57:12 +0000353 if (Subtarget->inMips16Mode())
354 TS.emitDirectiveSetMips16();
355 else
356 TS.emitDirectiveSetNoMips16();
Jack Carterab3cb422013-02-19 22:04:37 +0000357
Rafael Espindola6633d572014-01-14 18:57:12 +0000358 TS.emitDirectiveEnt(*CurrentFnSym);
Lang Hames9ff69c82015-04-24 19:11:51 +0000359 OutStreamer->EmitLabel(CurrentFnSym);
Chris Lattner5d9fb4b2010-01-27 23:23:58 +0000360}
361
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000362/// EmitFunctionBodyStart - Targets can override this to emit stuff before
363/// the first basic block in the function.
364void MipsAsmPrinter::EmitFunctionBodyStart() {
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000365 MipsTargetStreamer &TS = getTargetStreamer();
366
Rafael Espindola7d78b2a2013-10-29 16:24:21 +0000367 MCInstLowering.Initialize(&MF->getContext());
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +0000368
Duncan P. N. Exon Smith2e753142015-02-14 02:37:48 +0000369 bool IsNakedFunction = MF->getFunction()->hasFnAttribute(Attribute::Naked);
Reed Kotler0f2b10e2013-05-03 23:17:24 +0000370 if (!IsNakedFunction)
371 emitFrameDirective();
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000372
Rafael Espindola25fa2912014-01-27 04:33:11 +0000373 if (!IsNakedFunction)
374 printSavedRegsBitmask();
375
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000376 if (!Subtarget->inMips16Mode()) {
377 TS.emitDirectiveSetNoReorder();
378 TS.emitDirectiveSetNoMacro();
379 TS.emitDirectiveSetNoAt();
Akira Hatanaka8f3573032012-05-12 00:48:43 +0000380 }
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000381}
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000382
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000383/// EmitFunctionBodyEnd - Targets can override this to emit stuff after
384/// the last basic block in the function.
385void MipsAsmPrinter::EmitFunctionBodyEnd() {
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000386 MipsTargetStreamer &TS = getTargetStreamer();
387
Chris Lattnerfd97a332010-01-28 01:48:52 +0000388 // There are instruction for this macros, but they must
389 // always be at the function end, and we can't emit and
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000390 // break with BB logic.
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000391 if (!Subtarget->inMips16Mode()) {
392 TS.emitDirectiveSetAt();
393 TS.emitDirectiveSetMacro();
394 TS.emitDirectiveSetReorder();
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000395 }
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000396 TS.emitDirectiveEnd(CurrentFnSym->getName());
Reed Kotler91ae9822013-10-27 21:57:36 +0000397 // Make sure to terminate any constant pools that were at the end
398 // of the function.
399 if (!InConstantPool)
400 return;
401 InConstantPool = false;
Lang Hames9ff69c82015-04-24 19:11:51 +0000402 OutStreamer->EmitDataRegion(MCDR_DataRegionEnd);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000403}
404
Vasileios Kalintiris42544d62015-05-08 09:10:15 +0000405void MipsAsmPrinter::EmitBasicBlockEnd(const MachineBasicBlock &MBB) {
406 MipsTargetStreamer &TS = getTargetStreamer();
407 if (MBB.size() == 0)
408 TS.emitDirectiveInsn();
409}
410
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000411/// isBlockOnlyReachableByFallthough - Return true if the basic block has
412/// exactly one predecessor and the control transfer mechanism between
413/// the predecessor and this block is a fall-through.
Akira Hatanakae2489122011-04-15 21:51:11 +0000414bool MipsAsmPrinter::isBlockOnlyReachableByFallthrough(const MachineBasicBlock*
415 MBB) const {
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000416 // The predecessor has to be immediately before this block.
417 const MachineBasicBlock *Pred = *MBB->pred_begin();
418
419 // If the predecessor is a switch statement, assume a jump table
420 // implementation, so it is not a fall through.
421 if (const BasicBlock *bb = Pred->getBasicBlock())
422 if (isa<SwitchInst>(bb->getTerminator()))
423 return false;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000424
Akira Hatanakae625ba42011-04-01 18:57:38 +0000425 // If this is a landing pad, it isn't a fall through. If it has no preds,
426 // then nothing falls through to it.
Reid Kleckner0e288232015-08-27 23:27:47 +0000427 if (MBB->isEHPad() || MBB->pred_empty())
Akira Hatanakae625ba42011-04-01 18:57:38 +0000428 return false;
429
430 // If there isn't exactly one predecessor, it can't be a fall through.
431 MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI;
432 ++PI2;
Jia Liuf54f60f2012-02-28 07:46:26 +0000433
Akira Hatanakae625ba42011-04-01 18:57:38 +0000434 if (PI2 != MBB->pred_end())
Jia Liuf54f60f2012-02-28 07:46:26 +0000435 return false;
Akira Hatanakae625ba42011-04-01 18:57:38 +0000436
437 // The predecessor has to be immediately before this block.
438 if (!Pred->isLayoutSuccessor(MBB))
439 return false;
Jia Liuf54f60f2012-02-28 07:46:26 +0000440
Akira Hatanakae625ba42011-04-01 18:57:38 +0000441 // If the block is completely empty, then it definitely does fall through.
442 if (Pred->empty())
443 return true;
Jia Liuf54f60f2012-02-28 07:46:26 +0000444
Akira Hatanakae625ba42011-04-01 18:57:38 +0000445 // Otherwise, check the last instruction.
446 // Check if the last terminator is an unconditional branch.
447 MachineBasicBlock::const_iterator I = Pred->end();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000448 while (I != Pred->begin() && !(--I)->isTerminator()) ;
Akira Hatanakae625ba42011-04-01 18:57:38 +0000449
Evan Cheng7f8e5632011-12-07 07:15:52 +0000450 return !I->isBarrier();
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000451}
452
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000453// Print out an operand for an inline asm expression.
Eric Christophered51b9e2012-05-10 21:48:22 +0000454bool MipsAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000455 unsigned AsmVariant, const char *ExtraCode,
Chris Lattner3bb09762010-04-04 05:29:35 +0000456 raw_ostream &O) {
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000457 // Does this asm operand have a single letter operand modifier?
Eric Christophered51b9e2012-05-10 21:48:22 +0000458 if (ExtraCode && ExtraCode[0]) {
459 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000460
Eric Christophered51b9e2012-05-10 21:48:22 +0000461 const MachineOperand &MO = MI->getOperand(OpNum);
462 switch (ExtraCode[0]) {
Eric Christopherbc5d2492012-05-19 00:51:56 +0000463 default:
Jack Carterb2fd5f62012-06-21 17:14:46 +0000464 // See if this is a generic print operand
465 return AsmPrinter::PrintAsmOperand(MI,OpNum,AsmVariant,ExtraCode,O);
Eric Christopherbc5d2492012-05-19 00:51:56 +0000466 case 'X': // hex const int
467 if ((MO.getType()) != MachineOperand::MO_Immediate)
468 return true;
Benjamin Kramer33b46912015-05-23 16:53:07 +0000469 O << "0x" << Twine::utohexstr(MO.getImm());
Eric Christopherbc5d2492012-05-19 00:51:56 +0000470 return false;
471 case 'x': // hex const int (low 16 bits)
472 if ((MO.getType()) != MachineOperand::MO_Immediate)
473 return true;
Benjamin Kramer33b46912015-05-23 16:53:07 +0000474 O << "0x" << Twine::utohexstr(MO.getImm() & 0xffff);
Eric Christopherbc5d2492012-05-19 00:51:56 +0000475 return false;
476 case 'd': // decimal const int
477 if ((MO.getType()) != MachineOperand::MO_Immediate)
478 return true;
479 O << MO.getImm();
480 return false;
Eric Christopherf481ab32012-05-30 19:05:19 +0000481 case 'm': // decimal const int minus 1
482 if ((MO.getType()) != MachineOperand::MO_Immediate)
483 return true;
484 O << MO.getImm() - 1;
485 return false;
Jack Carter27747b52012-06-28 20:46:26 +0000486 case 'z': {
487 // $0 if zero, regular printing otherwise
Toma Tabacu27cab752014-11-06 14:25:42 +0000488 if (MO.getType() == MachineOperand::MO_Immediate && MO.getImm() == 0) {
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000489 O << "$0";
Toma Tabacu27cab752014-11-06 14:25:42 +0000490 return false;
491 }
492 // If not, call printOperand as normal.
493 break;
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000494 }
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000495 case 'D': // Second part of a double word register operand
496 case 'L': // Low order register of a double word register operand
Jack Cartera62ba822012-07-18 06:41:36 +0000497 case 'M': // High order register of a double word register operand
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000498 {
Jack Carterb2af5122012-07-05 23:58:21 +0000499 if (OpNum == 0)
500 return true;
501 const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
502 if (!FlagsOP.isImm())
503 return true;
504 unsigned Flags = FlagsOP.getImm();
505 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Jack Carter2ab73b12012-07-06 02:44:22 +0000506 // Number of registers represented by this operand. We are looking
507 // for 2 for 32 bit mode and 1 for 64 bit mode.
Jack Carterb2af5122012-07-05 23:58:21 +0000508 if (NumVals != 2) {
Jack Carter2ab73b12012-07-06 02:44:22 +0000509 if (Subtarget->isGP64bit() && NumVals == 1 && MO.isReg()) {
Jack Carterb2af5122012-07-05 23:58:21 +0000510 unsigned Reg = MO.getReg();
511 O << '$' << MipsInstPrinter::getRegisterName(Reg);
512 return false;
513 }
514 return true;
515 }
Jack Carter42ebf982012-07-11 21:41:49 +0000516
517 unsigned RegOp = OpNum;
518 if (!Subtarget->isGP64bit()){
Simon Pilgrimdcd84332016-11-18 11:53:36 +0000519 // Endianness reverses which register holds the high or low value
Jack Cartera62ba822012-07-18 06:41:36 +0000520 // between M and L.
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000521 switch(ExtraCode[0]) {
Jack Cartera62ba822012-07-18 06:41:36 +0000522 case 'M':
523 RegOp = (Subtarget->isLittle()) ? OpNum + 1 : OpNum;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000524 break;
525 case 'L':
Jack Cartera62ba822012-07-18 06:41:36 +0000526 RegOp = (Subtarget->isLittle()) ? OpNum : OpNum + 1;
527 break;
528 case 'D': // Always the second part
529 RegOp = OpNum + 1;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000530 }
531 if (RegOp >= MI->getNumOperands())
532 return true;
533 const MachineOperand &MO = MI->getOperand(RegOp);
534 if (!MO.isReg())
535 return true;
536 unsigned Reg = MO.getReg();
537 O << '$' << MipsInstPrinter::getRegisterName(Reg);
538 return false;
Jack Carterb2af5122012-07-05 23:58:21 +0000539 }
Eric Christophered51b9e2012-05-10 21:48:22 +0000540 }
Daniel Sanders8b59af12013-11-12 12:56:01 +0000541 case 'w':
542 // Print MSA registers for the 'f' constraint
543 // In LLVM, the 'w' modifier doesn't need to do anything.
544 // We can just call printOperand as normal.
545 break;
Jack Carter2ab73b12012-07-06 02:44:22 +0000546 }
547 }
Eric Christophered51b9e2012-05-10 21:48:22 +0000548
549 printOperand(MI, OpNum, O);
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000550 return false;
551}
552
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000553bool MipsAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
554 unsigned OpNum, unsigned AsmVariant,
555 const char *ExtraCode,
556 raw_ostream &O) {
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000557 assert(OpNum + 1 < MI->getNumOperands() && "Insufficient operands");
558 const MachineOperand &BaseMO = MI->getOperand(OpNum);
559 const MachineOperand &OffsetMO = MI->getOperand(OpNum + 1);
560 assert(BaseMO.isReg() && "Unexpected base pointer for inline asm memory operand.");
561 assert(OffsetMO.isImm() && "Unexpected offset for inline asm memory operand.");
562 int Offset = OffsetMO.getImm();
563
Jack Carterb04e3572013-04-09 23:19:50 +0000564 // Currently we are expecting either no ExtraCode or 'D'
565 if (ExtraCode) {
566 if (ExtraCode[0] == 'D')
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000567 Offset += 4;
Jack Carterb04e3572013-04-09 23:19:50 +0000568 else
569 return true; // Unknown modifier.
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000570 // FIXME: M = high order bits
571 // FIXME: L = low order bits
Jack Carterb04e3572013-04-09 23:19:50 +0000572 }
Jia Liuf54f60f2012-02-28 07:46:26 +0000573
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000574 O << Offset << "($" << MipsInstPrinter::getRegisterName(BaseMO.getReg()) << ")";
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000575
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000576 return false;
577}
578
Chris Lattner76c564b2010-04-04 04:47:45 +0000579void MipsAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
580 raw_ostream &O) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000581 const MachineOperand &MO = MI->getOperand(opNum);
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +0000582 bool closeP = false;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000583
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000584 if (MO.getTargetFlags())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000585 closeP = true;
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000586
587 switch(MO.getTargetFlags()) {
588 case MipsII::MO_GPREL: O << "%gp_rel("; break;
589 case MipsII::MO_GOT_CALL: O << "%call16("; break;
Akira Hatanaka56d9ef52011-04-01 21:41:06 +0000590 case MipsII::MO_GOT: O << "%got("; break;
591 case MipsII::MO_ABS_HI: O << "%hi("; break;
592 case MipsII::MO_ABS_LO: O << "%lo("; break;
Simon Dardisca74dd72017-01-27 11:36:52 +0000593 case MipsII::MO_HIGHER: O << "%higher("; break;
594 case MipsII::MO_HIGHEST: O << "%highest(("; break;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +0000595 case MipsII::MO_TLSGD: O << "%tlsgd("; break;
596 case MipsII::MO_GOTTPREL: O << "%gottprel("; break;
597 case MipsII::MO_TPREL_HI: O << "%tprel_hi("; break;
598 case MipsII::MO_TPREL_LO: O << "%tprel_lo("; break;
Akira Hatanaka25ce3642011-09-22 03:09:07 +0000599 case MipsII::MO_GPOFF_HI: O << "%hi(%neg(%gp_rel("; break;
600 case MipsII::MO_GPOFF_LO: O << "%lo(%neg(%gp_rel("; break;
601 case MipsII::MO_GOT_DISP: O << "%got_disp("; break;
602 case MipsII::MO_GOT_PAGE: O << "%got_page("; break;
603 case MipsII::MO_GOT_OFST: O << "%got_ofst("; break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000604 }
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000605
Chris Lattnereb2cc682009-09-13 20:31:40 +0000606 switch (MO.getType()) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000607 case MachineOperand::MO_Register:
Akira Hatanaka9c6028f2011-07-07 23:56:50 +0000608 O << '$'
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000609 << StringRef(MipsInstPrinter::getRegisterName(MO.getReg())).lower();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000610 break;
611
612 case MachineOperand::MO_Immediate:
Akira Hatanaka2db176c2011-05-24 21:22:21 +0000613 O << MO.getImm();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000614 break;
615
616 case MachineOperand::MO_MachineBasicBlock:
Matt Arsenault8b643552015-06-09 00:31:39 +0000617 MO.getMBB()->getSymbol()->print(O, MAI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000618 return;
619
620 case MachineOperand::MO_GlobalAddress:
Matt Arsenault8b643552015-06-09 00:31:39 +0000621 getSymbol(MO.getGlobal())->print(O, MAI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000622 break;
623
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000624 case MachineOperand::MO_BlockAddress: {
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000625 MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000626 O << BA->getName();
627 break;
628 }
629
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000630 case MachineOperand::MO_ConstantPoolIndex:
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000631 O << getDataLayout().getPrivateGlobalPrefix() << "CPI"
Chris Lattnera5bb3702007-12-30 23:10:15 +0000632 << getFunctionNumber() << "_" << MO.getIndex();
Bruno Cardoso Lopes4713b282009-11-19 06:06:13 +0000633 if (MO.getOffset())
634 O << "+" << MO.getOffset();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000635 break;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000636
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000637 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000638 llvm_unreachable("<unknown operand type>");
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000639 }
640
641 if (closeP) O << ")";
642}
643
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000644void MipsAsmPrinter::
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000645printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O) {
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000646 // Load/Store memory operands -- imm($reg)
647 // If PIC target the target is loaded as the
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +0000648 // pattern lw $25,%call16($28)
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000649
650 // opNum can be invalid if instruction has reglist as operand.
651 // MemOperand is always last operand of instruction (base + offset).
652 switch (MI->getOpcode()) {
653 default:
654 break;
655 case Mips::SWM32_MM:
656 case Mips::LWM32_MM:
657 opNum = MI->getNumOperands() - 2;
658 break;
659 }
660
Chris Lattner76c564b2010-04-04 04:47:45 +0000661 printOperand(MI, opNum+1, O);
Akira Hatanaka2e766ed2011-07-07 18:57:00 +0000662 O << "(";
663 printOperand(MI, opNum, O);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000664 O << ")";
665}
666
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000667void MipsAsmPrinter::
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000668printMemOperandEA(const MachineInstr *MI, int opNum, raw_ostream &O) {
669 // when using stack locations for not load/store instructions
670 // print the same way as all normal 3 operand instructions.
671 printOperand(MI, opNum, O);
672 O << ", ";
673 printOperand(MI, opNum+1, O);
674 return;
675}
676
677void MipsAsmPrinter::
Simon Dardisba92b032016-09-09 11:06:01 +0000678printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
679 const char *Modifier) {
680 const MachineOperand &MO = MI->getOperand(opNum);
681 O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
682}
683
684void MipsAsmPrinter::
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000685printRegisterList(const MachineInstr *MI, int opNum, raw_ostream &O) {
686 for (int i = opNum, e = MI->getNumOperands(); i != e; ++i) {
687 if (i != opNum) O << ", ";
688 printOperand(MI, i, O);
689 }
690}
691
Bob Wilsonb633d7a2009-09-30 22:06:26 +0000692void MipsAsmPrinter::EmitStartOfAsmFile(Module &M) {
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000693 MipsTargetStreamer &TS = getTargetStreamer();
694
695 // MipsTargetStreamer has an initialization order problem when emitting an
696 // object file directly (see MipsTargetELFStreamer for full details). Work
697 // around it by re-initializing the PIC state here.
Rafael Espindola699281c2016-05-18 11:58:50 +0000698 TS.setPic(OutContext.getObjectFileInfo()->isPositionIndependent());
Eric Christopher8af49b32015-02-18 01:01:57 +0000699
700 // Compute MIPS architecture attributes based on the default subtarget
701 // that we'd have constructed. Module level directives aren't LTO
702 // clean anyhow.
703 // FIXME: For ifunc related functions we could iterate over and look
704 // for a feature string that doesn't match the default one.
Daniel Sanders50f17232015-09-15 16:17:27 +0000705 const Triple &TT = TM.getTargetTriple();
Daniel Sandersa73f1fd2015-06-10 12:11:26 +0000706 StringRef CPU = MIPS_MC::selectMipsCPU(TT, TM.getTargetCPU());
Eric Christopher8af49b32015-02-18 01:01:57 +0000707 StringRef FS = TM.getTargetFeatureString();
708 const MipsTargetMachine &MTM = static_cast<const MipsTargetMachine &>(TM);
Daniel Sanders50f17232015-09-15 16:17:27 +0000709 const MipsSubtarget STI(TT, CPU, FS, MTM.isLittleEndian(), MTM);
Eric Christopher8af49b32015-02-18 01:01:57 +0000710
711 bool IsABICalls = STI.isABICalls();
712 const MipsABIInfo &ABI = MTM.getABI();
Daniel Sanders16fa1db2014-04-16 13:58:57 +0000713 if (IsABICalls) {
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000714 TS.emitDirectiveAbiCalls();
Daniel Sanders16fa1db2014-04-16 13:58:57 +0000715 // FIXME: This condition should be a lot more complicated that it is here.
716 // Ideally it should test for properties of the ABI and not the ABI
717 // itself.
718 // For the moment, I'm only correcting enough to make MIPS-IV work.
Simon Dardisca74dd72017-01-27 11:36:52 +0000719 if (!isPositionIndependent() && STI.hasSym32())
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000720 TS.emitDirectiveOptionPic0();
Daniel Sanders16fa1db2014-04-16 13:58:57 +0000721 }
Jack Carterf9f753c2013-06-18 19:47:15 +0000722
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000723 // Tell the assembler which ABI we are using
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000724 std::string SectionName = std::string(".mdebug.") + getCurrentABIString();
Lang Hames9ff69c82015-04-24 19:11:51 +0000725 OutStreamer->SwitchSection(
Rafael Espindolaba31e272015-01-29 17:33:21 +0000726 OutContext.getELFSection(SectionName, ELF::SHT_PROGBITS, 0));
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000727
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000728 // NaN: At the moment we only support:
729 // 1. .nan legacy (default)
730 // 2. .nan 2008
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000731 STI.isNaN2008() ? TS.emitDirectiveNaN2008()
732 : TS.emitDirectiveNaNLegacy();
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000733
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000734 // TODO: handle O64 ABI
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000735
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000736 TS.updateABIInfo(STI);
Daniel Sanders7e527422014-07-10 13:38:23 +0000737
Daniel Sanderse22244b2014-07-21 15:25:24 +0000738 // We should always emit a '.module fp=...' but binutils 2.24 does not accept
739 // it. We therefore emit it when it contradicts the ABI defaults (-mfpxx or
740 // -mfp64) and omit it otherwise.
Eric Christopher8af49b32015-02-18 01:01:57 +0000741 if (ABI.IsO32() && (STI.isABI_FPXX() || STI.isFP64bit()))
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000742 TS.emitDirectiveModuleFP();
Daniel Sanderse22244b2014-07-21 15:25:24 +0000743
744 // We should always emit a '.module [no]oddspreg' but binutils 2.24 does not
745 // accept it. We therefore emit it when it contradicts the default or an
746 // option has changed the default (i.e. FPXX) and omit it otherwise.
Eric Christopher8af49b32015-02-18 01:01:57 +0000747 if (ABI.IsO32() && (!STI.useOddSPReg() || STI.isABI_FPXX()))
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000748 TS.emitDirectiveModuleOddSPReg();
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000749}
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000750
Eric Christopher64d35be2015-02-19 19:52:25 +0000751void MipsAsmPrinter::emitInlineAsmStart() const {
Toma Tabacua23f13c2014-12-17 10:56:16 +0000752 MipsTargetStreamer &TS = getTargetStreamer();
753
Toma Tabacu68e8a9c2015-01-09 15:00:30 +0000754 // GCC's choice of assembler options for inline assembly code ('at', 'macro'
755 // and 'reorder') is different from LLVM's choice for generated code ('noat',
756 // 'nomacro' and 'noreorder').
757 // In order to maintain compatibility with inline assembly code which depends
758 // on GCC's assembler options being used, we have to switch to those options
759 // for the duration of the inline assembly block and then switch back.
Toma Tabacua23f13c2014-12-17 10:56:16 +0000760 TS.emitDirectiveSetPush();
761 TS.emitDirectiveSetAt();
762 TS.emitDirectiveSetMacro();
763 TS.emitDirectiveSetReorder();
Lang Hames9ff69c82015-04-24 19:11:51 +0000764 OutStreamer->AddBlankLine();
Toma Tabacua23f13c2014-12-17 10:56:16 +0000765}
766
767void MipsAsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
768 const MCSubtargetInfo *EndInfo) const {
Lang Hames9ff69c82015-04-24 19:11:51 +0000769 OutStreamer->AddBlankLine();
Toma Tabacua23f13c2014-12-17 10:56:16 +0000770 getTargetStreamer().emitDirectiveSetPop();
771}
772
Eric Christopher327fc972015-02-21 08:48:22 +0000773void MipsAsmPrinter::EmitJal(const MCSubtargetInfo &STI, MCSymbol *Symbol) {
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000774 MCInst I;
775 I.setOpcode(Mips::JAL);
776 I.addOperand(
Jim Grosbach13760bd2015-05-30 01:25:56 +0000777 MCOperand::createExpr(MCSymbolRefExpr::create(Symbol, OutContext)));
Lang Hames9ff69c82015-04-24 19:11:51 +0000778 OutStreamer->EmitInstruction(I, STI);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000779}
780
Eric Christopher327fc972015-02-21 08:48:22 +0000781void MipsAsmPrinter::EmitInstrReg(const MCSubtargetInfo &STI, unsigned Opcode,
782 unsigned Reg) {
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000783 MCInst I;
784 I.setOpcode(Opcode);
Jim Grosbache9119e42015-05-13 18:37:00 +0000785 I.addOperand(MCOperand::createReg(Reg));
Lang Hames9ff69c82015-04-24 19:11:51 +0000786 OutStreamer->EmitInstruction(I, STI);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000787}
788
Eric Christopher327fc972015-02-21 08:48:22 +0000789void MipsAsmPrinter::EmitInstrRegReg(const MCSubtargetInfo &STI,
790 unsigned Opcode, unsigned Reg1,
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000791 unsigned Reg2) {
792 MCInst I;
793 //
794 // Because of the current td files for Mips32, the operands for MTC1
795 // appear backwards from their normal assembly order. It's not a trivial
796 // change to fix this in the td file so we adjust for it here.
797 //
798 if (Opcode == Mips::MTC1) {
799 unsigned Temp = Reg1;
800 Reg1 = Reg2;
801 Reg2 = Temp;
802 }
803 I.setOpcode(Opcode);
Jim Grosbache9119e42015-05-13 18:37:00 +0000804 I.addOperand(MCOperand::createReg(Reg1));
805 I.addOperand(MCOperand::createReg(Reg2));
Lang Hames9ff69c82015-04-24 19:11:51 +0000806 OutStreamer->EmitInstruction(I, STI);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000807}
808
Eric Christopher327fc972015-02-21 08:48:22 +0000809void MipsAsmPrinter::EmitInstrRegRegReg(const MCSubtargetInfo &STI,
810 unsigned Opcode, unsigned Reg1,
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000811 unsigned Reg2, unsigned Reg3) {
812 MCInst I;
813 I.setOpcode(Opcode);
Jim Grosbache9119e42015-05-13 18:37:00 +0000814 I.addOperand(MCOperand::createReg(Reg1));
815 I.addOperand(MCOperand::createReg(Reg2));
816 I.addOperand(MCOperand::createReg(Reg3));
Lang Hames9ff69c82015-04-24 19:11:51 +0000817 OutStreamer->EmitInstruction(I, STI);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000818}
819
Eric Christopher327fc972015-02-21 08:48:22 +0000820void MipsAsmPrinter::EmitMovFPIntPair(const MCSubtargetInfo &STI,
821 unsigned MovOpc, unsigned Reg1,
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000822 unsigned Reg2, unsigned FPReg1,
823 unsigned FPReg2, bool LE) {
824 if (!LE) {
825 unsigned temp = Reg1;
826 Reg1 = Reg2;
827 Reg2 = temp;
828 }
Eric Christopher327fc972015-02-21 08:48:22 +0000829 EmitInstrRegReg(STI, MovOpc, Reg1, FPReg1);
830 EmitInstrRegReg(STI, MovOpc, Reg2, FPReg2);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000831}
832
Eric Christopher327fc972015-02-21 08:48:22 +0000833void MipsAsmPrinter::EmitSwapFPIntParams(const MCSubtargetInfo &STI,
834 Mips16HardFloatInfo::FPParamVariant PV,
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000835 bool LE, bool ToFP) {
836 using namespace Mips16HardFloatInfo;
837 unsigned MovOpc = ToFP ? Mips::MTC1 : Mips::MFC1;
838 switch (PV) {
839 case FSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000840 EmitInstrRegReg(STI, MovOpc, Mips::A0, Mips::F12);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000841 break;
842 case FFSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000843 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F14, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000844 break;
845 case FDSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000846 EmitInstrRegReg(STI, MovOpc, Mips::A0, Mips::F12);
847 EmitMovFPIntPair(STI, MovOpc, Mips::A2, Mips::A3, Mips::F14, Mips::F15, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000848 break;
849 case DSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000850 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000851 break;
852 case DDSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000853 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
854 EmitMovFPIntPair(STI, MovOpc, Mips::A2, Mips::A3, Mips::F14, Mips::F15, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000855 break;
856 case DFSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000857 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
858 EmitInstrRegReg(STI, MovOpc, Mips::A2, Mips::F14);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000859 break;
860 case NoSig:
861 return;
862 }
863}
864
Eric Christopher327fc972015-02-21 08:48:22 +0000865void MipsAsmPrinter::EmitSwapFPIntRetval(
866 const MCSubtargetInfo &STI, Mips16HardFloatInfo::FPReturnVariant RV,
867 bool LE) {
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000868 using namespace Mips16HardFloatInfo;
869 unsigned MovOpc = Mips::MFC1;
870 switch (RV) {
871 case FRet:
Eric Christopher327fc972015-02-21 08:48:22 +0000872 EmitInstrRegReg(STI, MovOpc, Mips::V0, Mips::F0);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000873 break;
874 case DRet:
Eric Christopher327fc972015-02-21 08:48:22 +0000875 EmitMovFPIntPair(STI, MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000876 break;
877 case CFRet:
Eric Christopher327fc972015-02-21 08:48:22 +0000878 EmitMovFPIntPair(STI, MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000879 break;
880 case CDRet:
Eric Christopher327fc972015-02-21 08:48:22 +0000881 EmitMovFPIntPair(STI, MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
882 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F2, Mips::F3, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000883 break;
884 case NoFPRet:
885 break;
886 }
887}
888
889void MipsAsmPrinter::EmitFPCallStub(
890 const char *Symbol, const Mips16HardFloatInfo::FuncSignature *Signature) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000891 MCSymbol *MSymbol = OutContext.getOrCreateSymbol(StringRef(Symbol));
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000892 using namespace Mips16HardFloatInfo;
Eric Christopherbb401642015-02-21 08:32:22 +0000893 bool LE = getDataLayout().isLittleEndian();
Eric Christopher327fc972015-02-21 08:48:22 +0000894 // Construct a local MCSubtargetInfo here.
895 // This is because the MachineFunction won't exist (but have not yet been
896 // freed) and since we're at the global level we can use the default
897 // constructed subtarget.
898 std::unique_ptr<MCSubtargetInfo> STI(TM.getTarget().createMCSubtargetInfo(
Daniel Sanders335487a2015-06-16 13:15:50 +0000899 TM.getTargetTriple().str(), TM.getTargetCPU(),
900 TM.getTargetFeatureString()));
Eric Christopher327fc972015-02-21 08:48:22 +0000901
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000902 //
903 // .global xxxx
904 //
Lang Hames9ff69c82015-04-24 19:11:51 +0000905 OutStreamer->EmitSymbolAttribute(MSymbol, MCSA_Global);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000906 const char *RetType;
907 //
908 // make the comment field identifying the return and parameter
909 // types of the floating point stub
910 // # Stub function to call rettype xxxx (params)
911 //
912 switch (Signature->RetSig) {
913 case FRet:
914 RetType = "float";
915 break;
916 case DRet:
917 RetType = "double";
918 break;
919 case CFRet:
920 RetType = "complex";
921 break;
922 case CDRet:
923 RetType = "double complex";
924 break;
925 case NoFPRet:
926 RetType = "";
927 break;
928 }
929 const char *Parms;
930 switch (Signature->ParamSig) {
931 case FSig:
932 Parms = "float";
933 break;
934 case FFSig:
935 Parms = "float, float";
936 break;
937 case FDSig:
938 Parms = "float, double";
939 break;
940 case DSig:
941 Parms = "double";
942 break;
943 case DDSig:
944 Parms = "double, double";
945 break;
946 case DFSig:
947 Parms = "double, float";
948 break;
949 case NoSig:
950 Parms = "";
951 break;
952 }
Lang Hames9ff69c82015-04-24 19:11:51 +0000953 OutStreamer->AddComment("\t# Stub function to call " + Twine(RetType) + " " +
954 Twine(Symbol) + " (" + Twine(Parms) + ")");
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000955 //
956 // probably not necessary but we save and restore the current section state
957 //
Lang Hames9ff69c82015-04-24 19:11:51 +0000958 OutStreamer->PushSection();
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000959 //
960 // .section mips16.call.fpxxxx,"ax",@progbits
961 //
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000962 MCSectionELF *M = OutContext.getELFSection(
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000963 ".mips16.call.fp." + std::string(Symbol), ELF::SHT_PROGBITS,
Rafael Espindolaba31e272015-01-29 17:33:21 +0000964 ELF::SHF_ALLOC | ELF::SHF_EXECINSTR);
Lang Hames9ff69c82015-04-24 19:11:51 +0000965 OutStreamer->SwitchSection(M, nullptr);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000966 //
967 // .align 2
968 //
Lang Hames9ff69c82015-04-24 19:11:51 +0000969 OutStreamer->EmitValueToAlignment(4);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000970 MipsTargetStreamer &TS = getTargetStreamer();
971 //
972 // .set nomips16
973 // .set nomicromips
974 //
975 TS.emitDirectiveSetNoMips16();
976 TS.emitDirectiveSetNoMicroMips();
977 //
978 // .ent __call_stub_fp_xxxx
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000979 // .type __call_stub_fp_xxxx,@function
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000980 // __call_stub_fp_xxxx:
981 //
982 std::string x = "__call_stub_fp_" + std::string(Symbol);
Rafael Espindolaa8695762015-06-02 00:25:12 +0000983 MCSymbolELF *Stub =
984 cast<MCSymbolELF>(OutContext.getOrCreateSymbol(StringRef(x)));
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000985 TS.emitDirectiveEnt(*Stub);
986 MCSymbol *MType =
Jim Grosbach6f482002015-05-18 18:43:14 +0000987 OutContext.getOrCreateSymbol("__call_stub_fp_" + Twine(Symbol));
Lang Hames9ff69c82015-04-24 19:11:51 +0000988 OutStreamer->EmitSymbolAttribute(MType, MCSA_ELF_TypeFunction);
989 OutStreamer->EmitLabel(Stub);
Eric Christopherd5bc07e2015-02-21 08:32:38 +0000990
991 // Only handle non-pic for now.
Rafael Espindolab0f59cb2016-06-27 17:21:46 +0000992 assert(!isPositionIndependent() &&
Eric Christopherd5bc07e2015-02-21 08:32:38 +0000993 "should not be here if we are compiling pic");
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000994 TS.emitDirectiveSetReorder();
995 //
996 // We need to add a MipsMCExpr class to MCTargetDesc to fully implement
997 // stubs without raw text but this current patch is for compiler generated
998 // functions and they all return some value.
999 // The calling sequence for non pic is different in that case and we need
1000 // to implement %lo and %hi in order to handle the case of no return value
1001 // See the corresponding method in Mips16HardFloat for details.
1002 //
1003 // mov the return address to S2.
1004 // we have no stack space to store it and we are about to make another call.
1005 // We need to make sure that the enclosing function knows to save S2
1006 // This should have already been handled.
1007 //
1008 // Mov $18, $31
1009
Vasileios Kalintiris1c78ca62015-08-11 08:56:25 +00001010 EmitInstrRegRegReg(*STI, Mips::OR, Mips::S2, Mips::RA, Mips::ZERO);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001011
Eric Christopher327fc972015-02-21 08:48:22 +00001012 EmitSwapFPIntParams(*STI, Signature->ParamSig, LE, true);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001013
1014 // Jal xxxx
1015 //
Eric Christopher327fc972015-02-21 08:48:22 +00001016 EmitJal(*STI, MSymbol);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001017
1018 // fix return values
Eric Christopher327fc972015-02-21 08:48:22 +00001019 EmitSwapFPIntRetval(*STI, Signature->RetSig, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001020 //
1021 // do the return
1022 // if (Signature->RetSig == NoFPRet)
1023 // llvm_unreachable("should not be any stubs here with no return value");
1024 // else
Eric Christopher327fc972015-02-21 08:48:22 +00001025 EmitInstrReg(*STI, Mips::JR, Mips::S2);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001026
Jim Grosbach6f482002015-05-18 18:43:14 +00001027 MCSymbol *Tmp = OutContext.createTempSymbol();
Lang Hames9ff69c82015-04-24 19:11:51 +00001028 OutStreamer->EmitLabel(Tmp);
Jim Grosbach13760bd2015-05-30 01:25:56 +00001029 const MCSymbolRefExpr *E = MCSymbolRefExpr::create(Stub, OutContext);
1030 const MCSymbolRefExpr *T = MCSymbolRefExpr::create(Tmp, OutContext);
1031 const MCExpr *T_min_E = MCBinaryExpr::createSub(T, E, OutContext);
Rafael Espindolaa8695762015-06-02 00:25:12 +00001032 OutStreamer->emitELFSize(Stub, T_min_E);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001033 TS.emitDirectiveEnd(x);
Lang Hames9ff69c82015-04-24 19:11:51 +00001034 OutStreamer->PopSection();
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001035}
1036
1037void MipsAsmPrinter::EmitEndOfAsmFile(Module &M) {
1038 // Emit needed stubs
1039 //
1040 for (std::map<
1041 const char *,
1042 const llvm::Mips16HardFloatInfo::FuncSignature *>::const_iterator
1043 it = StubsNeeded.begin();
1044 it != StubsNeeded.end(); ++it) {
1045 const char *Symbol = it->first;
1046 const llvm::Mips16HardFloatInfo::FuncSignature *Signature = it->second;
1047 EmitFPCallStub(Symbol, Signature);
1048 }
Rafael Espindola2ab7ea72014-01-27 01:33:33 +00001049 // return to the text section
Lang Hames9ff69c82015-04-24 19:11:51 +00001050 OutStreamer->SwitchSection(OutContext.getObjectFileInfo()->getTextSection());
Jack Carterc1b17ed2013-01-18 21:20:38 +00001051}
1052
Sagar Thakurec657922017-02-15 10:48:11 +00001053void MipsAsmPrinter::EmitSled(const MachineInstr &MI, SledKind Kind) {
1054 const uint8_t NoopsInSledCount = Subtarget->isGP64bit() ? 15 : 11;
1055 // For mips32 we want to emit the following pattern:
1056 //
1057 // .Lxray_sled_N:
1058 // ALIGN
1059 // B .tmpN
1060 // 11 NOP instructions (44 bytes)
1061 // ADDIU T9, T9, 52
1062 // .tmpN
1063 //
1064 // We need the 44 bytes (11 instructions) because at runtime, we'd
1065 // be patching over the full 48 bytes (12 instructions) with the following
1066 // pattern:
1067 //
1068 // ADDIU SP, SP, -8
1069 // NOP
1070 // SW RA, 4(SP)
1071 // SW T9, 0(SP)
1072 // LUI T9, %hi(__xray_FunctionEntry/Exit)
1073 // ORI T9, T9, %lo(__xray_FunctionEntry/Exit)
1074 // LUI T0, %hi(function_id)
1075 // JALR T9
1076 // ORI T0, T0, %lo(function_id)
1077 // LW T9, 0(SP)
1078 // LW RA, 4(SP)
1079 // ADDIU SP, SP, 8
1080 //
1081 // We add 52 bytes to t9 because we want to adjust the function pointer to
1082 // the actual start of function i.e. the address just after the noop sled.
1083 // We do this because gp displacement relocation is emitted at the start of
1084 // of the function i.e after the nop sled and to correctly calculate the
1085 // global offset table address, t9 must hold the address of the instruction
1086 // containing the gp displacement relocation.
1087 // FIXME: Is this correct for the static relocation model?
1088 //
1089 // For mips64 we want to emit the following pattern:
1090 //
1091 // .Lxray_sled_N:
1092 // ALIGN
1093 // B .tmpN
1094 // 15 NOP instructions (60 bytes)
1095 // .tmpN
1096 //
1097 // We need the 60 bytes (15 instructions) because at runtime, we'd
1098 // be patching over the full 64 bytes (16 instructions) with the following
1099 // pattern:
1100 //
1101 // DADDIU SP, SP, -16
1102 // NOP
1103 // SD RA, 8(SP)
1104 // SD T9, 0(SP)
1105 // LUI T9, %highest(__xray_FunctionEntry/Exit)
1106 // ORI T9, T9, %higher(__xray_FunctionEntry/Exit)
1107 // DSLL T9, T9, 16
1108 // ORI T9, T9, %hi(__xray_FunctionEntry/Exit)
1109 // DSLL T9, T9, 16
1110 // ORI T9, T9, %lo(__xray_FunctionEntry/Exit)
1111 // LUI T0, %hi(function_id)
1112 // JALR T9
1113 // ADDIU T0, T0, %lo(function_id)
1114 // LD T9, 0(SP)
1115 // LD RA, 8(SP)
1116 // DADDIU SP, SP, 16
1117 //
1118 OutStreamer->EmitCodeAlignment(4);
1119 auto CurSled = OutContext.createTempSymbol("xray_sled_", true);
1120 OutStreamer->EmitLabel(CurSled);
1121 auto Target = OutContext.createTempSymbol();
1122
1123 // Emit "B .tmpN" instruction, which jumps over the nop sled to the actual
1124 // start of function
1125 const MCExpr *TargetExpr = MCSymbolRefExpr::create(
1126 Target, MCSymbolRefExpr::VariantKind::VK_None, OutContext);
1127 EmitToStreamer(*OutStreamer, MCInstBuilder(Mips::BEQ)
1128 .addReg(Mips::ZERO)
1129 .addReg(Mips::ZERO)
1130 .addExpr(TargetExpr));
1131
1132 for (int8_t I = 0; I < NoopsInSledCount; I++)
1133 EmitToStreamer(*OutStreamer, MCInstBuilder(Mips::SLL)
1134 .addReg(Mips::ZERO)
1135 .addReg(Mips::ZERO)
1136 .addImm(0));
1137
1138 OutStreamer->EmitLabel(Target);
1139
1140 if (!Subtarget->isGP64bit()) {
1141 EmitToStreamer(*OutStreamer,
1142 MCInstBuilder(Mips::ADDiu)
1143 .addReg(Mips::T9)
1144 .addReg(Mips::T9)
1145 .addImm(0x34));
1146 }
1147
1148 recordSled(CurSled, MI, Kind);
1149}
1150
1151void MipsAsmPrinter::EmitXRayTable() {
1152 if (Sleds.empty())
1153 return;
1154 if (Subtarget->isTargetELF()) {
1155 auto PrevSection = OutStreamer->getCurrentSectionOnly();
1156 auto Fn = MF->getFunction();
1157 MCSection *Section;
1158
1159 if (Fn->hasComdat())
1160 Section = OutContext.getELFSection("xray_instr_map", ELF::SHT_PROGBITS,
1161 ELF::SHF_ALLOC | ELF::SHF_GROUP, 0,
1162 Fn->getComdat()->getName());
1163 else
1164 Section =
1165 OutContext.getELFSection("xray_instr_map", ELF::SHT_PROGBITS,
1166 ELF::SHF_ALLOC, 0, CurrentFnSym->getName());
1167
1168 OutStreamer->SwitchSection(Section);
1169 for (const auto &Sled : Sleds) {
1170 OutStreamer->EmitSymbolValue(Sled.Sled, Subtarget->isGP64bit() ? 8 : 4);
1171 OutStreamer->EmitSymbolValue(CurrentFnSym, Subtarget->isGP64bit() ? 8 : 4);
1172 auto Kind = static_cast<uint8_t>(Sled.Kind);
1173 OutStreamer->EmitBytes(
1174 StringRef(reinterpret_cast<const char *>(&Kind), 1));
1175 OutStreamer->EmitBytes(
1176 StringRef(reinterpret_cast<const char *>(&Sled.AlwaysInstrument), 1));
1177 OutStreamer->EmitZeros(Subtarget->isGP64bit() ? 14 : 6);
1178 }
1179 OutStreamer->SwitchSection(PrevSection);
1180 }
1181 Sleds.clear();
1182}
1183
1184void MipsAsmPrinter::LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr &MI) {
1185 EmitSled(MI, SledKind::FUNCTION_ENTER);
1186}
1187
1188void MipsAsmPrinter::LowerPATCHABLE_FUNCTION_EXIT(const MachineInstr &MI) {
1189 EmitSled(MI, SledKind::FUNCTION_EXIT);
1190}
1191
1192void MipsAsmPrinter::LowerPATCHABLE_TAIL_CALL(const MachineInstr &MI) {
1193 EmitSled(MI, SledKind::TAIL_CALL);
1194}
1195
Akira Hatanakaf2bcad92011-07-01 01:04:43 +00001196void MipsAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
1197 raw_ostream &OS) {
1198 // TODO: implement
1199}
1200
Petar Jovanovicdbb39352017-01-20 17:53:30 +00001201// Emit .dtprelword or .dtpreldword directive
1202// and value for debug thread local expression.
Simon Dardis2e8cdbd2017-02-08 19:03:46 +00001203void MipsAsmPrinter::EmitDebugThreadLocal(const MCExpr *Value,
Petar Jovanovicdbb39352017-01-20 17:53:30 +00001204 unsigned Size) const {
1205 switch (Size) {
1206 case 4:
1207 OutStreamer->EmitDTPRel32Value(Value);
1208 break;
1209 case 8:
1210 OutStreamer->EmitDTPRel64Value(Value);
1211 break;
1212 default:
1213 llvm_unreachable("Unexpected size of expression value.");
1214 }
1215}
1216
Sasa Stankovic8c5736b2014-02-28 10:00:38 +00001217// Align all targets of indirect branches on bundle size. Used only if target
1218// is NaCl.
1219void MipsAsmPrinter::NaClAlignIndirectJumpTargets(MachineFunction &MF) {
1220 // Align all blocks that are jumped to through jump table.
1221 if (MachineJumpTableInfo *JtInfo = MF.getJumpTableInfo()) {
1222 const std::vector<MachineJumpTableEntry> &JT = JtInfo->getJumpTables();
1223 for (unsigned I = 0; I < JT.size(); ++I) {
1224 const std::vector<MachineBasicBlock*> &MBBs = JT[I].MBBs;
1225
1226 for (unsigned J = 0; J < MBBs.size(); ++J)
1227 MBBs[J]->setAlignment(MIPS_NACL_BUNDLE_ALIGN);
1228 }
1229 }
1230
1231 // If basic block address is taken, block can be target of indirect branch.
Vasileios Kalintiris5a971a42016-04-15 20:43:17 +00001232 for (auto &MBB : MF) {
1233 if (MBB.hasAddressTaken())
1234 MBB.setAlignment(MIPS_NACL_BUNDLE_ALIGN);
Sasa Stankovic8c5736b2014-02-28 10:00:38 +00001235 }
1236}
1237
Sasa Stankovic7b061a42014-04-30 15:06:25 +00001238bool MipsAsmPrinter::isLongBranchPseudo(int Opcode) const {
1239 return (Opcode == Mips::LONG_BRANCH_LUi
1240 || Opcode == Mips::LONG_BRANCH_ADDiu
Sasa Stankovic7b061a42014-04-30 15:06:25 +00001241 || Opcode == Mips::LONG_BRANCH_DADDiu);
1242}
1243
Bob Wilson5a495fe2009-06-23 23:59:40 +00001244// Force static initialization.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +00001245extern "C" void LLVMInitializeMipsAsmPrinter() {
Mehdi Aminif42454b2016-10-09 23:00:34 +00001246 RegisterAsmPrinter<MipsAsmPrinter> X(getTheMipsTarget());
1247 RegisterAsmPrinter<MipsAsmPrinter> Y(getTheMipselTarget());
1248 RegisterAsmPrinter<MipsAsmPrinter> A(getTheMips64Target());
1249 RegisterAsmPrinter<MipsAsmPrinter> B(getTheMips64elTarget());
Daniel Dunbare8338102009-07-15 20:24:03 +00001250}