blob: 7519c309e44a3904a330954c822ba040f08c3c11 [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"
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000042#include "llvm/MC/MCSection.h"
Rafael Espindola2ab7ea72014-01-27 01:33:33 +000043#include "llvm/MC/MCSectionELF.h"
Rafael Espindolaa8695762015-06-02 00:25:12 +000044#include "llvm/MC/MCSymbolELF.h"
Jack Carterab3cb422013-02-19 22:04:37 +000045#include "llvm/Support/ELF.h"
Jack Carterb2af5122012-07-05 23:58:21 +000046#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000047#include "llvm/Support/raw_ostream.h"
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +000048#include "llvm/Target/TargetLoweringObjectFile.h"
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +000049#include "llvm/Target/TargetOptions.h"
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000050#include <string>
Akira Hatanakaf2bcad92011-07-01 01:04:43 +000051
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000052using namespace llvm;
53
Chandler Carruth84e68b22014-04-22 02:41:26 +000054#define DEBUG_TYPE "mips-asm-printer"
55
Toma Tabacua23f13c2014-12-17 10:56:16 +000056MipsTargetStreamer &MipsAsmPrinter::getTargetStreamer() const {
Lang Hames9ff69c82015-04-24 19:11:51 +000057 return static_cast<MipsTargetStreamer &>(*OutStreamer->getTargetStreamer());
Rafael Espindolaa17151a2013-10-08 13:08:17 +000058}
59
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000060bool MipsAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Eric Christopher3ee30d02015-02-20 08:39:06 +000061 Subtarget = &MF.getSubtarget<MipsSubtarget>();
Eric Christopher8ef7a6a2014-07-18 00:08:53 +000062
Reed Kotler1595f362013-04-09 19:46:01 +000063 // Initialize TargetLoweringObjectFile.
Eric Christopher4e7d1e72014-07-18 23:41:32 +000064 const_cast<TargetLoweringObjectFile &>(getObjFileLowering())
Reed Kotler1595f362013-04-09 19:46:01 +000065 .Initialize(OutContext, TM);
Eric Christopher4e7d1e72014-07-18 23:41:32 +000066
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000067 MipsFI = MF.getInfo<MipsFunctionInfo>();
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000068 if (Subtarget->inMips16Mode())
69 for (std::map<
70 const char *,
71 const llvm::Mips16HardFloatInfo::FuncSignature *>::const_iterator
72 it = MipsFI->StubsNeeded.begin();
73 it != MipsFI->StubsNeeded.end(); ++it) {
74 const char *Symbol = it->first;
75 const llvm::Mips16HardFloatInfo::FuncSignature *Signature = it->second;
76 if (StubsNeeded.find(Symbol) == StubsNeeded.end())
77 StubsNeeded[Symbol] = Signature;
78 }
Reed Kotler91ae9822013-10-27 21:57:36 +000079 MCP = MF.getConstantPool();
Sasa Stankovic8c5736b2014-02-28 10:00:38 +000080
81 // In NaCl, all indirect jump targets must be aligned to bundle size.
82 if (Subtarget->isTargetNaCl())
83 NaClAlignIndirectJumpTargets(MF);
84
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000085 AsmPrinter::runOnMachineFunction(MF);
86 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();
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000139 TS.forbidModuleDirective();
Daniel Sandersc7dbc632014-07-08 10:11:38 +0000140
Akira Hatanakaddd12652011-07-07 20:10:52 +0000141 if (MI->isDebugValue()) {
Bruno Cardoso Lopescd1d4472011-12-30 21:09:41 +0000142 SmallString<128> Str;
143 raw_svector_ostream OS(Str);
144
Akira Hatanakaddd12652011-07-07 20:10:52 +0000145 PrintDebugValueComment(MI, OS);
146 return;
147 }
148
Reed Kotler91ae9822013-10-27 21:57:36 +0000149 // If we just ended a constant pool, mark it as such.
150 if (InConstantPool && MI->getOpcode() != Mips::CONSTPOOL_ENTRY) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000151 OutStreamer->EmitDataRegion(MCDR_DataRegionEnd);
Reed Kotler91ae9822013-10-27 21:57:36 +0000152 InConstantPool = false;
153 }
154 if (MI->getOpcode() == Mips::CONSTPOOL_ENTRY) {
155 // CONSTPOOL_ENTRY - This instruction represents a floating
156 //constant pool in the function. The first operand is the ID#
157 // for this instruction, the second is the index into the
158 // MachineConstantPool that this is, the third is the size in
159 // bytes of this constant pool entry.
160 // The required alignment is specified on the basic block holding this MI.
161 //
162 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
163 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
164
165 // If this is the first entry of the pool, mark it.
166 if (!InConstantPool) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000167 OutStreamer->EmitDataRegion(MCDR_DataRegion);
Reed Kotler91ae9822013-10-27 21:57:36 +0000168 InConstantPool = true;
169 }
170
Lang Hames9ff69c82015-04-24 19:11:51 +0000171 OutStreamer->EmitLabel(GetCPISymbol(LabelId));
Reed Kotler91ae9822013-10-27 21:57:36 +0000172
173 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
174 if (MCPE.isMachineConstantPoolEntry())
175 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
176 else
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000177 EmitGlobalConstant(MF->getDataLayout(), MCPE.Val.ConstVal);
Reed Kotler91ae9822013-10-27 21:57:36 +0000178 return;
179 }
180
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000181
182 MachineBasicBlock::const_instr_iterator I = MI->getIterator();
183 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
Akira Hatanaka5ac78682012-06-13 23:25:52 +0000184
185 do {
Akira Hatanaka556135d2013-02-06 21:50:15 +0000186 // Do any auto-generated pseudo lowerings.
Lang Hames9ff69c82015-04-24 19:11:51 +0000187 if (emitPseudoExpansionLowering(*OutStreamer, &*I))
Akira Hatanaka556135d2013-02-06 21:50:15 +0000188 continue;
Jack Carterc20a21b2012-08-28 19:07:39 +0000189
Daniel Sanders338513b2014-07-09 10:16:07 +0000190 if (I->getOpcode() == Mips::PseudoReturn ||
Daniel Sandersf5a5fbd2014-07-09 10:21:59 +0000191 I->getOpcode() == Mips::PseudoReturn64 ||
192 I->getOpcode() == Mips::PseudoIndirectBranch ||
Simon Dardisea343152016-08-18 13:22:43 +0000193 I->getOpcode() == Mips::PseudoIndirectBranch64 ||
194 I->getOpcode() == Mips::TAILCALLREG ||
195 I->getOpcode() == Mips::TAILCALLREG64) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000196 emitPseudoIndirectBranch(*OutStreamer, &*I);
Daniel Sanders338513b2014-07-09 10:16:07 +0000197 continue;
198 }
199
Reed Kotler76c9bcd2013-02-15 21:05:58 +0000200 // The inMips16Mode() test is not permanent.
201 // Some instructions are marked as pseudo right now which
202 // would make the test fail for the wrong reason but
203 // that will be fixed soon. We need this here because we are
204 // removing another test for this situation downstream in the
205 // callchain.
206 //
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000207 if (I->isPseudo() && !Subtarget->inMips16Mode()
208 && !isLongBranchPseudo(I->getOpcode()))
Reed Kotler76c9bcd2013-02-15 21:05:58 +0000209 llvm_unreachable("Pseudo opcode found in EmitInstruction()");
210
Akira Hatanaka556135d2013-02-06 21:50:15 +0000211 MCInst TmpInst0;
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +0000212 MCInstLowering.Lower(&*I, TmpInst0);
Lang Hames9ff69c82015-04-24 19:11:51 +0000213 EmitToStreamer(*OutStreamer, TmpInst0);
Akira Hatanaka556135d2013-02-06 21:50:15 +0000214 } while ((++I != E) && I->isInsideBundle()); // Delay slot check
Akira Hatanakaddd12652011-07-07 20:10:52 +0000215}
216
Akira Hatanakae2489122011-04-15 21:51:11 +0000217//===----------------------------------------------------------------------===//
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000218//
219// Mips Asm Directives
220//
221// -- Frame directive "frame Stackpointer, Stacksize, RARegister"
222// Describe the stack frame.
223//
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000224// -- Mask directives "(f)mask bitmask, offset"
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000225// Tells the assembler which registers are saved and where.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000226// bitmask - contain a little endian bitset indicating which registers are
227// saved on function prologue (e.g. with a 0x80000000 mask, the
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000228// assembler knows the register 31 (RA) is saved at prologue.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000229// offset - the position before stack pointer subtraction indicating where
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000230// the first saved register on prologue is located. (e.g. with a
231//
232// Consider the following function prologue:
233//
Bill Wendling97925ec2008-02-27 06:33:05 +0000234// .frame $fp,48,$ra
235// .mask 0xc0000000,-8
236// addiu $sp, $sp, -48
237// sw $ra, 40($sp)
238// sw $fp, 36($sp)
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000239//
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000240// With a 0xc0000000 mask, the assembler knows the register 31 (RA) and
241// 30 (FP) are saved at prologue. As the save order on prologue is from
242// left to right, RA is saved first. A -8 offset means that after the
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000243// stack pointer subtration, the first register in the mask (RA) will be
244// saved at address 48-8=40.
245//
Akira Hatanakae2489122011-04-15 21:51:11 +0000246//===----------------------------------------------------------------------===//
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000247
Akira Hatanakae2489122011-04-15 21:51:11 +0000248//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000249// Mask directives
Akira Hatanakae2489122011-04-15 21:51:11 +0000250//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000251
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000252// Create a bitmask with all callee saved registers for CPU or Floating Point
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000253// registers. For CPU registers consider RA, GP and FP for saving if necessary.
Rafael Espindola25fa2912014-01-27 04:33:11 +0000254void MipsAsmPrinter::printSavedRegsBitmask() {
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000255 // CPU and FPU Saved Registers Bitmasks
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000256 unsigned CPUBitmask = 0, FPUBitmask = 0;
257 int CPUTopSavedRegOff, FPUTopSavedRegOff;
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000258
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000259 // Set the CPU and FPU Bitmasks
Matthias Braun941a7052016-07-28 18:40:00 +0000260 const MachineFrameInfo &MFI = MF->getFrameInfo();
Eric Christophercba722f2015-03-21 03:13:07 +0000261 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
Matthias Braun941a7052016-07-28 18:40:00 +0000262 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000263 // size of stack area to which FP callee-saved regs are saved.
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000264 unsigned CPURegSize = Mips::GPR32RegClass.getSize();
Craig Topperc7242e02012-04-20 07:30:17 +0000265 unsigned FGR32RegSize = Mips::FGR32RegClass.getSize();
266 unsigned AFGR64RegSize = Mips::AFGR64RegClass.getSize();
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000267 bool HasAFGR64Reg = false;
268 unsigned CSFPRegsSize = 0;
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000269
Toma Tabacube218922015-04-09 10:54:16 +0000270 for (const auto &I : CSI) {
271 unsigned Reg = I.getReg();
Eric Christophercba722f2015-03-21 03:13:07 +0000272 unsigned RegNum = TRI->getEncodingValue(Reg);
Toma Tabacube218922015-04-09 10:54:16 +0000273
274 // If it's a floating point register, set the FPU Bitmask.
275 // If it's a general purpose register, set the CPU Bitmask.
276 if (Mips::FGR32RegClass.contains(Reg)) {
277 FPUBitmask |= (1 << RegNum);
278 CSFPRegsSize += FGR32RegSize;
279 } else if (Mips::AFGR64RegClass.contains(Reg)) {
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000280 FPUBitmask |= (3 << RegNum);
281 CSFPRegsSize += AFGR64RegSize;
282 HasAFGR64Reg = true;
Toma Tabacube218922015-04-09 10:54:16 +0000283 } else if (Mips::GPR32RegClass.contains(Reg))
284 CPUBitmask |= (1 << RegNum);
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000285 }
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000286
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000287 // FP Regs are saved right below where the virtual frame pointer points to.
288 FPUTopSavedRegOff = FPUBitmask ?
289 (HasAFGR64Reg ? -AFGR64RegSize : -FGR32RegSize) : 0;
290
291 // CPU Regs are saved below FP Regs.
292 CPUTopSavedRegOff = CPUBitmask ? -CSFPRegsSize - CPURegSize : 0;
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000293
Rafael Espindola25fa2912014-01-27 04:33:11 +0000294 MipsTargetStreamer &TS = getTargetStreamer();
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000295 // Print CPUBitmask
Rafael Espindola25fa2912014-01-27 04:33:11 +0000296 TS.emitMask(CPUBitmask, CPUTopSavedRegOff);
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000297
298 // Print FPUBitmask
Rafael Espindola25fa2912014-01-27 04:33:11 +0000299 TS.emitFMask(FPUBitmask, FPUTopSavedRegOff);
Bruno Cardoso Lopesbcda5e22007-07-11 23:24:41 +0000300}
301
Akira Hatanakae2489122011-04-15 21:51:11 +0000302//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000303// Frame and Set directives
Akira Hatanakae2489122011-04-15 21:51:11 +0000304//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000305
306/// Frame Directive
Chris Lattner5e596182010-04-04 07:05:53 +0000307void MipsAsmPrinter::emitFrameDirective() {
Eric Christophercba722f2015-03-21 03:13:07 +0000308 const TargetRegisterInfo &RI = *MF->getSubtarget().getRegisterInfo();
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000309
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000310 unsigned stackReg = RI.getFrameRegister(*MF);
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000311 unsigned returnReg = RI.getRARegister();
Matthias Braun941a7052016-07-28 18:40:00 +0000312 unsigned stackSize = MF->getFrameInfo().getStackSize();
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000313
Rafael Espindola054234f2014-01-27 03:53:56 +0000314 getTargetStreamer().emitFrame(stackReg, stackSize, returnReg);
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000315}
316
317/// Emit Set directives.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000318const char *MipsAsmPrinter::getCurrentABIString() const {
Eric Christophera5762812015-01-26 17:33:46 +0000319 switch (static_cast<MipsTargetMachine &>(TM).getABI().GetEnumValue()) {
Daniel Sanderse2e25da2014-10-24 16:15:27 +0000320 case MipsABIInfo::ABI::O32: return "abi32";
321 case MipsABIInfo::ABI::N32: return "abiN32";
322 case MipsABIInfo::ABI::N64: return "abi64";
Dmitri Gribenkoca1e27b2012-09-10 21:26:47 +0000323 default: llvm_unreachable("Unknown Mips ABI");
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000324 }
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000325}
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000326
Chris Lattner5d9fb4b2010-01-27 23:23:58 +0000327void MipsAsmPrinter::EmitFunctionEntryLabel() {
Rafael Espindola6633d572014-01-14 18:57:12 +0000328 MipsTargetStreamer &TS = getTargetStreamer();
Sasa Stankovic8c5736b2014-02-28 10:00:38 +0000329
330 // NaCl sandboxing requires that indirect call instructions are masked.
331 // This means that function entry points should be bundle-aligned.
332 if (Subtarget->isTargetNaCl())
333 EmitAlignment(std::max(MF->getAlignment(), MIPS_NACL_BUNDLE_ALIGN));
334
Daniel Sanders1d148642016-06-16 09:17:03 +0000335 if (Subtarget->inMicroMipsMode()) {
Rafael Espindola6633d572014-01-14 18:57:12 +0000336 TS.emitDirectiveSetMicroMips();
Daniel Sanders1d148642016-06-16 09:17:03 +0000337 TS.setUsesMicroMips();
338 } else
Matheus Almeidadc7e48e2014-04-16 11:46:59 +0000339 TS.emitDirectiveSetNoMicroMips();
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000340
Rafael Espindola6633d572014-01-14 18:57:12 +0000341 if (Subtarget->inMips16Mode())
342 TS.emitDirectiveSetMips16();
343 else
344 TS.emitDirectiveSetNoMips16();
Jack Carterab3cb422013-02-19 22:04:37 +0000345
Rafael Espindola6633d572014-01-14 18:57:12 +0000346 TS.emitDirectiveEnt(*CurrentFnSym);
Lang Hames9ff69c82015-04-24 19:11:51 +0000347 OutStreamer->EmitLabel(CurrentFnSym);
Chris Lattner5d9fb4b2010-01-27 23:23:58 +0000348}
349
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000350/// EmitFunctionBodyStart - Targets can override this to emit stuff before
351/// the first basic block in the function.
352void MipsAsmPrinter::EmitFunctionBodyStart() {
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000353 MipsTargetStreamer &TS = getTargetStreamer();
354
Rafael Espindola7d78b2a2013-10-29 16:24:21 +0000355 MCInstLowering.Initialize(&MF->getContext());
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +0000356
Duncan P. N. Exon Smith2e753142015-02-14 02:37:48 +0000357 bool IsNakedFunction = MF->getFunction()->hasFnAttribute(Attribute::Naked);
Reed Kotler0f2b10e2013-05-03 23:17:24 +0000358 if (!IsNakedFunction)
359 emitFrameDirective();
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000360
Rafael Espindola25fa2912014-01-27 04:33:11 +0000361 if (!IsNakedFunction)
362 printSavedRegsBitmask();
363
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000364 if (!Subtarget->inMips16Mode()) {
365 TS.emitDirectiveSetNoReorder();
366 TS.emitDirectiveSetNoMacro();
367 TS.emitDirectiveSetNoAt();
Akira Hatanaka8f3573032012-05-12 00:48:43 +0000368 }
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000369}
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000370
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000371/// EmitFunctionBodyEnd - Targets can override this to emit stuff after
372/// the last basic block in the function.
373void MipsAsmPrinter::EmitFunctionBodyEnd() {
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000374 MipsTargetStreamer &TS = getTargetStreamer();
375
Chris Lattnerfd97a332010-01-28 01:48:52 +0000376 // There are instruction for this macros, but they must
377 // always be at the function end, and we can't emit and
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000378 // break with BB logic.
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000379 if (!Subtarget->inMips16Mode()) {
380 TS.emitDirectiveSetAt();
381 TS.emitDirectiveSetMacro();
382 TS.emitDirectiveSetReorder();
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000383 }
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000384 TS.emitDirectiveEnd(CurrentFnSym->getName());
Reed Kotler91ae9822013-10-27 21:57:36 +0000385 // Make sure to terminate any constant pools that were at the end
386 // of the function.
387 if (!InConstantPool)
388 return;
389 InConstantPool = false;
Lang Hames9ff69c82015-04-24 19:11:51 +0000390 OutStreamer->EmitDataRegion(MCDR_DataRegionEnd);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000391}
392
Vasileios Kalintiris42544d62015-05-08 09:10:15 +0000393void MipsAsmPrinter::EmitBasicBlockEnd(const MachineBasicBlock &MBB) {
394 MipsTargetStreamer &TS = getTargetStreamer();
395 if (MBB.size() == 0)
396 TS.emitDirectiveInsn();
397}
398
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000399/// isBlockOnlyReachableByFallthough - Return true if the basic block has
400/// exactly one predecessor and the control transfer mechanism between
401/// the predecessor and this block is a fall-through.
Akira Hatanakae2489122011-04-15 21:51:11 +0000402bool MipsAsmPrinter::isBlockOnlyReachableByFallthrough(const MachineBasicBlock*
403 MBB) const {
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000404 // The predecessor has to be immediately before this block.
405 const MachineBasicBlock *Pred = *MBB->pred_begin();
406
407 // If the predecessor is a switch statement, assume a jump table
408 // implementation, so it is not a fall through.
409 if (const BasicBlock *bb = Pred->getBasicBlock())
410 if (isa<SwitchInst>(bb->getTerminator()))
411 return false;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000412
Akira Hatanakae625ba42011-04-01 18:57:38 +0000413 // If this is a landing pad, it isn't a fall through. If it has no preds,
414 // then nothing falls through to it.
Reid Kleckner0e288232015-08-27 23:27:47 +0000415 if (MBB->isEHPad() || MBB->pred_empty())
Akira Hatanakae625ba42011-04-01 18:57:38 +0000416 return false;
417
418 // If there isn't exactly one predecessor, it can't be a fall through.
419 MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI;
420 ++PI2;
Jia Liuf54f60f2012-02-28 07:46:26 +0000421
Akira Hatanakae625ba42011-04-01 18:57:38 +0000422 if (PI2 != MBB->pred_end())
Jia Liuf54f60f2012-02-28 07:46:26 +0000423 return false;
Akira Hatanakae625ba42011-04-01 18:57:38 +0000424
425 // The predecessor has to be immediately before this block.
426 if (!Pred->isLayoutSuccessor(MBB))
427 return false;
Jia Liuf54f60f2012-02-28 07:46:26 +0000428
Akira Hatanakae625ba42011-04-01 18:57:38 +0000429 // If the block is completely empty, then it definitely does fall through.
430 if (Pred->empty())
431 return true;
Jia Liuf54f60f2012-02-28 07:46:26 +0000432
Akira Hatanakae625ba42011-04-01 18:57:38 +0000433 // Otherwise, check the last instruction.
434 // Check if the last terminator is an unconditional branch.
435 MachineBasicBlock::const_iterator I = Pred->end();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000436 while (I != Pred->begin() && !(--I)->isTerminator()) ;
Akira Hatanakae625ba42011-04-01 18:57:38 +0000437
Evan Cheng7f8e5632011-12-07 07:15:52 +0000438 return !I->isBarrier();
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000439}
440
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000441// Print out an operand for an inline asm expression.
Eric Christophered51b9e2012-05-10 21:48:22 +0000442bool MipsAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000443 unsigned AsmVariant, const char *ExtraCode,
Chris Lattner3bb09762010-04-04 05:29:35 +0000444 raw_ostream &O) {
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000445 // Does this asm operand have a single letter operand modifier?
Eric Christophered51b9e2012-05-10 21:48:22 +0000446 if (ExtraCode && ExtraCode[0]) {
447 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000448
Eric Christophered51b9e2012-05-10 21:48:22 +0000449 const MachineOperand &MO = MI->getOperand(OpNum);
450 switch (ExtraCode[0]) {
Eric Christopherbc5d2492012-05-19 00:51:56 +0000451 default:
Jack Carterb2fd5f62012-06-21 17:14:46 +0000452 // See if this is a generic print operand
453 return AsmPrinter::PrintAsmOperand(MI,OpNum,AsmVariant,ExtraCode,O);
Eric Christopherbc5d2492012-05-19 00:51:56 +0000454 case 'X': // hex const int
455 if ((MO.getType()) != MachineOperand::MO_Immediate)
456 return true;
Benjamin Kramer33b46912015-05-23 16:53:07 +0000457 O << "0x" << Twine::utohexstr(MO.getImm());
Eric Christopherbc5d2492012-05-19 00:51:56 +0000458 return false;
459 case 'x': // hex const int (low 16 bits)
460 if ((MO.getType()) != MachineOperand::MO_Immediate)
461 return true;
Benjamin Kramer33b46912015-05-23 16:53:07 +0000462 O << "0x" << Twine::utohexstr(MO.getImm() & 0xffff);
Eric Christopherbc5d2492012-05-19 00:51:56 +0000463 return false;
464 case 'd': // decimal const int
465 if ((MO.getType()) != MachineOperand::MO_Immediate)
466 return true;
467 O << MO.getImm();
468 return false;
Eric Christopherf481ab32012-05-30 19:05:19 +0000469 case 'm': // decimal const int minus 1
470 if ((MO.getType()) != MachineOperand::MO_Immediate)
471 return true;
472 O << MO.getImm() - 1;
473 return false;
Jack Carter27747b52012-06-28 20:46:26 +0000474 case 'z': {
475 // $0 if zero, regular printing otherwise
Toma Tabacu27cab752014-11-06 14:25:42 +0000476 if (MO.getType() == MachineOperand::MO_Immediate && MO.getImm() == 0) {
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000477 O << "$0";
Toma Tabacu27cab752014-11-06 14:25:42 +0000478 return false;
479 }
480 // If not, call printOperand as normal.
481 break;
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000482 }
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000483 case 'D': // Second part of a double word register operand
484 case 'L': // Low order register of a double word register operand
Jack Cartera62ba822012-07-18 06:41:36 +0000485 case 'M': // High order register of a double word register operand
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000486 {
Jack Carterb2af5122012-07-05 23:58:21 +0000487 if (OpNum == 0)
488 return true;
489 const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
490 if (!FlagsOP.isImm())
491 return true;
492 unsigned Flags = FlagsOP.getImm();
493 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Jack Carter2ab73b12012-07-06 02:44:22 +0000494 // Number of registers represented by this operand. We are looking
495 // for 2 for 32 bit mode and 1 for 64 bit mode.
Jack Carterb2af5122012-07-05 23:58:21 +0000496 if (NumVals != 2) {
Jack Carter2ab73b12012-07-06 02:44:22 +0000497 if (Subtarget->isGP64bit() && NumVals == 1 && MO.isReg()) {
Jack Carterb2af5122012-07-05 23:58:21 +0000498 unsigned Reg = MO.getReg();
499 O << '$' << MipsInstPrinter::getRegisterName(Reg);
500 return false;
501 }
502 return true;
503 }
Jack Carter42ebf982012-07-11 21:41:49 +0000504
505 unsigned RegOp = OpNum;
506 if (!Subtarget->isGP64bit()){
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000507 // Endianess reverses which register holds the high or low value
Jack Cartera62ba822012-07-18 06:41:36 +0000508 // between M and L.
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000509 switch(ExtraCode[0]) {
Jack Cartera62ba822012-07-18 06:41:36 +0000510 case 'M':
511 RegOp = (Subtarget->isLittle()) ? OpNum + 1 : OpNum;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000512 break;
513 case 'L':
Jack Cartera62ba822012-07-18 06:41:36 +0000514 RegOp = (Subtarget->isLittle()) ? OpNum : OpNum + 1;
515 break;
516 case 'D': // Always the second part
517 RegOp = OpNum + 1;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000518 }
519 if (RegOp >= MI->getNumOperands())
520 return true;
521 const MachineOperand &MO = MI->getOperand(RegOp);
522 if (!MO.isReg())
523 return true;
524 unsigned Reg = MO.getReg();
525 O << '$' << MipsInstPrinter::getRegisterName(Reg);
526 return false;
Jack Carterb2af5122012-07-05 23:58:21 +0000527 }
Eric Christophered51b9e2012-05-10 21:48:22 +0000528 }
Daniel Sanders8b59af12013-11-12 12:56:01 +0000529 case 'w':
530 // Print MSA registers for the 'f' constraint
531 // In LLVM, the 'w' modifier doesn't need to do anything.
532 // We can just call printOperand as normal.
533 break;
Jack Carter2ab73b12012-07-06 02:44:22 +0000534 }
535 }
Eric Christophered51b9e2012-05-10 21:48:22 +0000536
537 printOperand(MI, OpNum, O);
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000538 return false;
539}
540
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000541bool MipsAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
542 unsigned OpNum, unsigned AsmVariant,
543 const char *ExtraCode,
544 raw_ostream &O) {
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000545 assert(OpNum + 1 < MI->getNumOperands() && "Insufficient operands");
546 const MachineOperand &BaseMO = MI->getOperand(OpNum);
547 const MachineOperand &OffsetMO = MI->getOperand(OpNum + 1);
548 assert(BaseMO.isReg() && "Unexpected base pointer for inline asm memory operand.");
549 assert(OffsetMO.isImm() && "Unexpected offset for inline asm memory operand.");
550 int Offset = OffsetMO.getImm();
551
Jack Carterb04e3572013-04-09 23:19:50 +0000552 // Currently we are expecting either no ExtraCode or 'D'
553 if (ExtraCode) {
554 if (ExtraCode[0] == 'D')
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000555 Offset += 4;
Jack Carterb04e3572013-04-09 23:19:50 +0000556 else
557 return true; // Unknown modifier.
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000558 // FIXME: M = high order bits
559 // FIXME: L = low order bits
Jack Carterb04e3572013-04-09 23:19:50 +0000560 }
Jia Liuf54f60f2012-02-28 07:46:26 +0000561
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000562 O << Offset << "($" << MipsInstPrinter::getRegisterName(BaseMO.getReg()) << ")";
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000563
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000564 return false;
565}
566
Chris Lattner76c564b2010-04-04 04:47:45 +0000567void MipsAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
568 raw_ostream &O) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000569 const MachineOperand &MO = MI->getOperand(opNum);
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +0000570 bool closeP = false;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000571
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000572 if (MO.getTargetFlags())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000573 closeP = true;
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000574
575 switch(MO.getTargetFlags()) {
576 case MipsII::MO_GPREL: O << "%gp_rel("; break;
577 case MipsII::MO_GOT_CALL: O << "%call16("; break;
Akira Hatanaka56d9ef52011-04-01 21:41:06 +0000578 case MipsII::MO_GOT: O << "%got("; break;
579 case MipsII::MO_ABS_HI: O << "%hi("; break;
580 case MipsII::MO_ABS_LO: O << "%lo("; break;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +0000581 case MipsII::MO_TLSGD: O << "%tlsgd("; break;
582 case MipsII::MO_GOTTPREL: O << "%gottprel("; break;
583 case MipsII::MO_TPREL_HI: O << "%tprel_hi("; break;
584 case MipsII::MO_TPREL_LO: O << "%tprel_lo("; break;
Akira Hatanaka25ce3642011-09-22 03:09:07 +0000585 case MipsII::MO_GPOFF_HI: O << "%hi(%neg(%gp_rel("; break;
586 case MipsII::MO_GPOFF_LO: O << "%lo(%neg(%gp_rel("; break;
587 case MipsII::MO_GOT_DISP: O << "%got_disp("; break;
588 case MipsII::MO_GOT_PAGE: O << "%got_page("; break;
589 case MipsII::MO_GOT_OFST: O << "%got_ofst("; break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000590 }
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000591
Chris Lattnereb2cc682009-09-13 20:31:40 +0000592 switch (MO.getType()) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000593 case MachineOperand::MO_Register:
Akira Hatanaka9c6028f2011-07-07 23:56:50 +0000594 O << '$'
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000595 << StringRef(MipsInstPrinter::getRegisterName(MO.getReg())).lower();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000596 break;
597
598 case MachineOperand::MO_Immediate:
Akira Hatanaka2db176c2011-05-24 21:22:21 +0000599 O << MO.getImm();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000600 break;
601
602 case MachineOperand::MO_MachineBasicBlock:
Matt Arsenault8b643552015-06-09 00:31:39 +0000603 MO.getMBB()->getSymbol()->print(O, MAI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000604 return;
605
606 case MachineOperand::MO_GlobalAddress:
Matt Arsenault8b643552015-06-09 00:31:39 +0000607 getSymbol(MO.getGlobal())->print(O, MAI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000608 break;
609
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000610 case MachineOperand::MO_BlockAddress: {
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000611 MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000612 O << BA->getName();
613 break;
614 }
615
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000616 case MachineOperand::MO_ConstantPoolIndex:
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000617 O << getDataLayout().getPrivateGlobalPrefix() << "CPI"
Chris Lattnera5bb3702007-12-30 23:10:15 +0000618 << getFunctionNumber() << "_" << MO.getIndex();
Bruno Cardoso Lopes4713b282009-11-19 06:06:13 +0000619 if (MO.getOffset())
620 O << "+" << MO.getOffset();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000621 break;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000622
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000623 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000624 llvm_unreachable("<unknown operand type>");
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000625 }
626
627 if (closeP) O << ")";
628}
629
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000630void MipsAsmPrinter::
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000631printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O) {
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000632 // Load/Store memory operands -- imm($reg)
633 // If PIC target the target is loaded as the
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +0000634 // pattern lw $25,%call16($28)
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000635
636 // opNum can be invalid if instruction has reglist as operand.
637 // MemOperand is always last operand of instruction (base + offset).
638 switch (MI->getOpcode()) {
639 default:
640 break;
641 case Mips::SWM32_MM:
642 case Mips::LWM32_MM:
643 opNum = MI->getNumOperands() - 2;
644 break;
645 }
646
Chris Lattner76c564b2010-04-04 04:47:45 +0000647 printOperand(MI, opNum+1, O);
Akira Hatanaka2e766ed2011-07-07 18:57:00 +0000648 O << "(";
649 printOperand(MI, opNum, O);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000650 O << ")";
651}
652
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000653void MipsAsmPrinter::
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000654printMemOperandEA(const MachineInstr *MI, int opNum, raw_ostream &O) {
655 // when using stack locations for not load/store instructions
656 // print the same way as all normal 3 operand instructions.
657 printOperand(MI, opNum, O);
658 O << ", ";
659 printOperand(MI, opNum+1, O);
660 return;
661}
662
663void MipsAsmPrinter::
Simon Dardisba92b032016-09-09 11:06:01 +0000664printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
665 const char *Modifier) {
666 const MachineOperand &MO = MI->getOperand(opNum);
667 O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
668}
669
670void MipsAsmPrinter::
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000671printRegisterList(const MachineInstr *MI, int opNum, raw_ostream &O) {
672 for (int i = opNum, e = MI->getNumOperands(); i != e; ++i) {
673 if (i != opNum) O << ", ";
674 printOperand(MI, i, O);
675 }
676}
677
Bob Wilsonb633d7a2009-09-30 22:06:26 +0000678void MipsAsmPrinter::EmitStartOfAsmFile(Module &M) {
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000679 MipsTargetStreamer &TS = getTargetStreamer();
680
681 // MipsTargetStreamer has an initialization order problem when emitting an
682 // object file directly (see MipsTargetELFStreamer for full details). Work
683 // around it by re-initializing the PIC state here.
Rafael Espindola699281c2016-05-18 11:58:50 +0000684 TS.setPic(OutContext.getObjectFileInfo()->isPositionIndependent());
Eric Christopher8af49b32015-02-18 01:01:57 +0000685
686 // Compute MIPS architecture attributes based on the default subtarget
687 // that we'd have constructed. Module level directives aren't LTO
688 // clean anyhow.
689 // FIXME: For ifunc related functions we could iterate over and look
690 // for a feature string that doesn't match the default one.
Daniel Sanders50f17232015-09-15 16:17:27 +0000691 const Triple &TT = TM.getTargetTriple();
Daniel Sandersa73f1fd2015-06-10 12:11:26 +0000692 StringRef CPU = MIPS_MC::selectMipsCPU(TT, TM.getTargetCPU());
Eric Christopher8af49b32015-02-18 01:01:57 +0000693 StringRef FS = TM.getTargetFeatureString();
694 const MipsTargetMachine &MTM = static_cast<const MipsTargetMachine &>(TM);
Daniel Sanders50f17232015-09-15 16:17:27 +0000695 const MipsSubtarget STI(TT, CPU, FS, MTM.isLittleEndian(), MTM);
Eric Christopher8af49b32015-02-18 01:01:57 +0000696
697 bool IsABICalls = STI.isABICalls();
698 const MipsABIInfo &ABI = MTM.getABI();
Daniel Sanders16fa1db2014-04-16 13:58:57 +0000699 if (IsABICalls) {
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000700 TS.emitDirectiveAbiCalls();
Daniel Sanders16fa1db2014-04-16 13:58:57 +0000701 // FIXME: This condition should be a lot more complicated that it is here.
702 // Ideally it should test for properties of the ABI and not the ABI
703 // itself.
704 // For the moment, I'm only correcting enough to make MIPS-IV work.
Rafael Espindolab0f59cb2016-06-27 17:21:46 +0000705 if (!isPositionIndependent() && !ABI.IsN64())
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000706 TS.emitDirectiveOptionPic0();
Daniel Sanders16fa1db2014-04-16 13:58:57 +0000707 }
Jack Carterf9f753c2013-06-18 19:47:15 +0000708
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000709 // Tell the assembler which ABI we are using
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000710 std::string SectionName = std::string(".mdebug.") + getCurrentABIString();
Lang Hames9ff69c82015-04-24 19:11:51 +0000711 OutStreamer->SwitchSection(
Rafael Espindolaba31e272015-01-29 17:33:21 +0000712 OutContext.getELFSection(SectionName, ELF::SHT_PROGBITS, 0));
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000713
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000714 // NaN: At the moment we only support:
715 // 1. .nan legacy (default)
716 // 2. .nan 2008
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000717 STI.isNaN2008() ? TS.emitDirectiveNaN2008()
718 : TS.emitDirectiveNaNLegacy();
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000719
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000720 // TODO: handle O64 ABI
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000721
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000722 TS.updateABIInfo(STI);
Daniel Sanders7e527422014-07-10 13:38:23 +0000723
Daniel Sanderse22244b2014-07-21 15:25:24 +0000724 // We should always emit a '.module fp=...' but binutils 2.24 does not accept
725 // it. We therefore emit it when it contradicts the ABI defaults (-mfpxx or
726 // -mfp64) and omit it otherwise.
Eric Christopher8af49b32015-02-18 01:01:57 +0000727 if (ABI.IsO32() && (STI.isABI_FPXX() || STI.isFP64bit()))
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000728 TS.emitDirectiveModuleFP();
Daniel Sanderse22244b2014-07-21 15:25:24 +0000729
730 // We should always emit a '.module [no]oddspreg' but binutils 2.24 does not
731 // accept it. We therefore emit it when it contradicts the default or an
732 // option has changed the default (i.e. FPXX) and omit it otherwise.
Eric Christopher8af49b32015-02-18 01:01:57 +0000733 if (ABI.IsO32() && (!STI.useOddSPReg() || STI.isABI_FPXX()))
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000734 TS.emitDirectiveModuleOddSPReg();
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000735}
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000736
Eric Christopher64d35be2015-02-19 19:52:25 +0000737void MipsAsmPrinter::emitInlineAsmStart() const {
Toma Tabacua23f13c2014-12-17 10:56:16 +0000738 MipsTargetStreamer &TS = getTargetStreamer();
739
Toma Tabacu68e8a9c2015-01-09 15:00:30 +0000740 // GCC's choice of assembler options for inline assembly code ('at', 'macro'
741 // and 'reorder') is different from LLVM's choice for generated code ('noat',
742 // 'nomacro' and 'noreorder').
743 // In order to maintain compatibility with inline assembly code which depends
744 // on GCC's assembler options being used, we have to switch to those options
745 // for the duration of the inline assembly block and then switch back.
Toma Tabacua23f13c2014-12-17 10:56:16 +0000746 TS.emitDirectiveSetPush();
747 TS.emitDirectiveSetAt();
748 TS.emitDirectiveSetMacro();
749 TS.emitDirectiveSetReorder();
Lang Hames9ff69c82015-04-24 19:11:51 +0000750 OutStreamer->AddBlankLine();
Toma Tabacua23f13c2014-12-17 10:56:16 +0000751}
752
753void MipsAsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
754 const MCSubtargetInfo *EndInfo) const {
Lang Hames9ff69c82015-04-24 19:11:51 +0000755 OutStreamer->AddBlankLine();
Toma Tabacua23f13c2014-12-17 10:56:16 +0000756 getTargetStreamer().emitDirectiveSetPop();
757}
758
Eric Christopher327fc972015-02-21 08:48:22 +0000759void MipsAsmPrinter::EmitJal(const MCSubtargetInfo &STI, MCSymbol *Symbol) {
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000760 MCInst I;
761 I.setOpcode(Mips::JAL);
762 I.addOperand(
Jim Grosbach13760bd2015-05-30 01:25:56 +0000763 MCOperand::createExpr(MCSymbolRefExpr::create(Symbol, OutContext)));
Lang Hames9ff69c82015-04-24 19:11:51 +0000764 OutStreamer->EmitInstruction(I, STI);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000765}
766
Eric Christopher327fc972015-02-21 08:48:22 +0000767void MipsAsmPrinter::EmitInstrReg(const MCSubtargetInfo &STI, unsigned Opcode,
768 unsigned Reg) {
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000769 MCInst I;
770 I.setOpcode(Opcode);
Jim Grosbache9119e42015-05-13 18:37:00 +0000771 I.addOperand(MCOperand::createReg(Reg));
Lang Hames9ff69c82015-04-24 19:11:51 +0000772 OutStreamer->EmitInstruction(I, STI);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000773}
774
Eric Christopher327fc972015-02-21 08:48:22 +0000775void MipsAsmPrinter::EmitInstrRegReg(const MCSubtargetInfo &STI,
776 unsigned Opcode, unsigned Reg1,
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000777 unsigned Reg2) {
778 MCInst I;
779 //
780 // Because of the current td files for Mips32, the operands for MTC1
781 // appear backwards from their normal assembly order. It's not a trivial
782 // change to fix this in the td file so we adjust for it here.
783 //
784 if (Opcode == Mips::MTC1) {
785 unsigned Temp = Reg1;
786 Reg1 = Reg2;
787 Reg2 = Temp;
788 }
789 I.setOpcode(Opcode);
Jim Grosbache9119e42015-05-13 18:37:00 +0000790 I.addOperand(MCOperand::createReg(Reg1));
791 I.addOperand(MCOperand::createReg(Reg2));
Lang Hames9ff69c82015-04-24 19:11:51 +0000792 OutStreamer->EmitInstruction(I, STI);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000793}
794
Eric Christopher327fc972015-02-21 08:48:22 +0000795void MipsAsmPrinter::EmitInstrRegRegReg(const MCSubtargetInfo &STI,
796 unsigned Opcode, unsigned Reg1,
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000797 unsigned Reg2, unsigned Reg3) {
798 MCInst I;
799 I.setOpcode(Opcode);
Jim Grosbache9119e42015-05-13 18:37:00 +0000800 I.addOperand(MCOperand::createReg(Reg1));
801 I.addOperand(MCOperand::createReg(Reg2));
802 I.addOperand(MCOperand::createReg(Reg3));
Lang Hames9ff69c82015-04-24 19:11:51 +0000803 OutStreamer->EmitInstruction(I, STI);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000804}
805
Eric Christopher327fc972015-02-21 08:48:22 +0000806void MipsAsmPrinter::EmitMovFPIntPair(const MCSubtargetInfo &STI,
807 unsigned MovOpc, unsigned Reg1,
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000808 unsigned Reg2, unsigned FPReg1,
809 unsigned FPReg2, bool LE) {
810 if (!LE) {
811 unsigned temp = Reg1;
812 Reg1 = Reg2;
813 Reg2 = temp;
814 }
Eric Christopher327fc972015-02-21 08:48:22 +0000815 EmitInstrRegReg(STI, MovOpc, Reg1, FPReg1);
816 EmitInstrRegReg(STI, MovOpc, Reg2, FPReg2);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000817}
818
Eric Christopher327fc972015-02-21 08:48:22 +0000819void MipsAsmPrinter::EmitSwapFPIntParams(const MCSubtargetInfo &STI,
820 Mips16HardFloatInfo::FPParamVariant PV,
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000821 bool LE, bool ToFP) {
822 using namespace Mips16HardFloatInfo;
823 unsigned MovOpc = ToFP ? Mips::MTC1 : Mips::MFC1;
824 switch (PV) {
825 case FSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000826 EmitInstrRegReg(STI, MovOpc, Mips::A0, Mips::F12);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000827 break;
828 case FFSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000829 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F14, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000830 break;
831 case FDSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000832 EmitInstrRegReg(STI, MovOpc, Mips::A0, Mips::F12);
833 EmitMovFPIntPair(STI, MovOpc, Mips::A2, Mips::A3, Mips::F14, Mips::F15, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000834 break;
835 case DSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000836 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000837 break;
838 case DDSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000839 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
840 EmitMovFPIntPair(STI, MovOpc, Mips::A2, Mips::A3, Mips::F14, Mips::F15, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000841 break;
842 case DFSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000843 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
844 EmitInstrRegReg(STI, MovOpc, Mips::A2, Mips::F14);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000845 break;
846 case NoSig:
847 return;
848 }
849}
850
Eric Christopher327fc972015-02-21 08:48:22 +0000851void MipsAsmPrinter::EmitSwapFPIntRetval(
852 const MCSubtargetInfo &STI, Mips16HardFloatInfo::FPReturnVariant RV,
853 bool LE) {
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000854 using namespace Mips16HardFloatInfo;
855 unsigned MovOpc = Mips::MFC1;
856 switch (RV) {
857 case FRet:
Eric Christopher327fc972015-02-21 08:48:22 +0000858 EmitInstrRegReg(STI, MovOpc, Mips::V0, Mips::F0);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000859 break;
860 case DRet:
Eric Christopher327fc972015-02-21 08:48:22 +0000861 EmitMovFPIntPair(STI, MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000862 break;
863 case CFRet:
Eric Christopher327fc972015-02-21 08:48:22 +0000864 EmitMovFPIntPair(STI, MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000865 break;
866 case CDRet:
Eric Christopher327fc972015-02-21 08:48:22 +0000867 EmitMovFPIntPair(STI, MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
868 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F2, Mips::F3, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000869 break;
870 case NoFPRet:
871 break;
872 }
873}
874
875void MipsAsmPrinter::EmitFPCallStub(
876 const char *Symbol, const Mips16HardFloatInfo::FuncSignature *Signature) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000877 MCSymbol *MSymbol = OutContext.getOrCreateSymbol(StringRef(Symbol));
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000878 using namespace Mips16HardFloatInfo;
Eric Christopherbb401642015-02-21 08:32:22 +0000879 bool LE = getDataLayout().isLittleEndian();
Eric Christopher327fc972015-02-21 08:48:22 +0000880 // Construct a local MCSubtargetInfo here.
881 // This is because the MachineFunction won't exist (but have not yet been
882 // freed) and since we're at the global level we can use the default
883 // constructed subtarget.
884 std::unique_ptr<MCSubtargetInfo> STI(TM.getTarget().createMCSubtargetInfo(
Daniel Sanders335487a2015-06-16 13:15:50 +0000885 TM.getTargetTriple().str(), TM.getTargetCPU(),
886 TM.getTargetFeatureString()));
Eric Christopher327fc972015-02-21 08:48:22 +0000887
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000888 //
889 // .global xxxx
890 //
Lang Hames9ff69c82015-04-24 19:11:51 +0000891 OutStreamer->EmitSymbolAttribute(MSymbol, MCSA_Global);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000892 const char *RetType;
893 //
894 // make the comment field identifying the return and parameter
895 // types of the floating point stub
896 // # Stub function to call rettype xxxx (params)
897 //
898 switch (Signature->RetSig) {
899 case FRet:
900 RetType = "float";
901 break;
902 case DRet:
903 RetType = "double";
904 break;
905 case CFRet:
906 RetType = "complex";
907 break;
908 case CDRet:
909 RetType = "double complex";
910 break;
911 case NoFPRet:
912 RetType = "";
913 break;
914 }
915 const char *Parms;
916 switch (Signature->ParamSig) {
917 case FSig:
918 Parms = "float";
919 break;
920 case FFSig:
921 Parms = "float, float";
922 break;
923 case FDSig:
924 Parms = "float, double";
925 break;
926 case DSig:
927 Parms = "double";
928 break;
929 case DDSig:
930 Parms = "double, double";
931 break;
932 case DFSig:
933 Parms = "double, float";
934 break;
935 case NoSig:
936 Parms = "";
937 break;
938 }
Lang Hames9ff69c82015-04-24 19:11:51 +0000939 OutStreamer->AddComment("\t# Stub function to call " + Twine(RetType) + " " +
940 Twine(Symbol) + " (" + Twine(Parms) + ")");
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000941 //
942 // probably not necessary but we save and restore the current section state
943 //
Lang Hames9ff69c82015-04-24 19:11:51 +0000944 OutStreamer->PushSection();
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000945 //
946 // .section mips16.call.fpxxxx,"ax",@progbits
947 //
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000948 MCSectionELF *M = OutContext.getELFSection(
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000949 ".mips16.call.fp." + std::string(Symbol), ELF::SHT_PROGBITS,
Rafael Espindolaba31e272015-01-29 17:33:21 +0000950 ELF::SHF_ALLOC | ELF::SHF_EXECINSTR);
Lang Hames9ff69c82015-04-24 19:11:51 +0000951 OutStreamer->SwitchSection(M, nullptr);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000952 //
953 // .align 2
954 //
Lang Hames9ff69c82015-04-24 19:11:51 +0000955 OutStreamer->EmitValueToAlignment(4);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000956 MipsTargetStreamer &TS = getTargetStreamer();
957 //
958 // .set nomips16
959 // .set nomicromips
960 //
961 TS.emitDirectiveSetNoMips16();
962 TS.emitDirectiveSetNoMicroMips();
963 //
964 // .ent __call_stub_fp_xxxx
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000965 // .type __call_stub_fp_xxxx,@function
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000966 // __call_stub_fp_xxxx:
967 //
968 std::string x = "__call_stub_fp_" + std::string(Symbol);
Rafael Espindolaa8695762015-06-02 00:25:12 +0000969 MCSymbolELF *Stub =
970 cast<MCSymbolELF>(OutContext.getOrCreateSymbol(StringRef(x)));
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000971 TS.emitDirectiveEnt(*Stub);
972 MCSymbol *MType =
Jim Grosbach6f482002015-05-18 18:43:14 +0000973 OutContext.getOrCreateSymbol("__call_stub_fp_" + Twine(Symbol));
Lang Hames9ff69c82015-04-24 19:11:51 +0000974 OutStreamer->EmitSymbolAttribute(MType, MCSA_ELF_TypeFunction);
975 OutStreamer->EmitLabel(Stub);
Eric Christopherd5bc07e2015-02-21 08:32:38 +0000976
977 // Only handle non-pic for now.
Rafael Espindolab0f59cb2016-06-27 17:21:46 +0000978 assert(!isPositionIndependent() &&
Eric Christopherd5bc07e2015-02-21 08:32:38 +0000979 "should not be here if we are compiling pic");
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000980 TS.emitDirectiveSetReorder();
981 //
982 // We need to add a MipsMCExpr class to MCTargetDesc to fully implement
983 // stubs without raw text but this current patch is for compiler generated
984 // functions and they all return some value.
985 // The calling sequence for non pic is different in that case and we need
986 // to implement %lo and %hi in order to handle the case of no return value
987 // See the corresponding method in Mips16HardFloat for details.
988 //
989 // mov the return address to S2.
990 // we have no stack space to store it and we are about to make another call.
991 // We need to make sure that the enclosing function knows to save S2
992 // This should have already been handled.
993 //
994 // Mov $18, $31
995
Vasileios Kalintiris1c78ca62015-08-11 08:56:25 +0000996 EmitInstrRegRegReg(*STI, Mips::OR, Mips::S2, Mips::RA, Mips::ZERO);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000997
Eric Christopher327fc972015-02-21 08:48:22 +0000998 EmitSwapFPIntParams(*STI, Signature->ParamSig, LE, true);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000999
1000 // Jal xxxx
1001 //
Eric Christopher327fc972015-02-21 08:48:22 +00001002 EmitJal(*STI, MSymbol);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001003
1004 // fix return values
Eric Christopher327fc972015-02-21 08:48:22 +00001005 EmitSwapFPIntRetval(*STI, Signature->RetSig, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001006 //
1007 // do the return
1008 // if (Signature->RetSig == NoFPRet)
1009 // llvm_unreachable("should not be any stubs here with no return value");
1010 // else
Eric Christopher327fc972015-02-21 08:48:22 +00001011 EmitInstrReg(*STI, Mips::JR, Mips::S2);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001012
Jim Grosbach6f482002015-05-18 18:43:14 +00001013 MCSymbol *Tmp = OutContext.createTempSymbol();
Lang Hames9ff69c82015-04-24 19:11:51 +00001014 OutStreamer->EmitLabel(Tmp);
Jim Grosbach13760bd2015-05-30 01:25:56 +00001015 const MCSymbolRefExpr *E = MCSymbolRefExpr::create(Stub, OutContext);
1016 const MCSymbolRefExpr *T = MCSymbolRefExpr::create(Tmp, OutContext);
1017 const MCExpr *T_min_E = MCBinaryExpr::createSub(T, E, OutContext);
Rafael Espindolaa8695762015-06-02 00:25:12 +00001018 OutStreamer->emitELFSize(Stub, T_min_E);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001019 TS.emitDirectiveEnd(x);
Lang Hames9ff69c82015-04-24 19:11:51 +00001020 OutStreamer->PopSection();
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001021}
1022
1023void MipsAsmPrinter::EmitEndOfAsmFile(Module &M) {
1024 // Emit needed stubs
1025 //
1026 for (std::map<
1027 const char *,
1028 const llvm::Mips16HardFloatInfo::FuncSignature *>::const_iterator
1029 it = StubsNeeded.begin();
1030 it != StubsNeeded.end(); ++it) {
1031 const char *Symbol = it->first;
1032 const llvm::Mips16HardFloatInfo::FuncSignature *Signature = it->second;
1033 EmitFPCallStub(Symbol, Signature);
1034 }
Rafael Espindola2ab7ea72014-01-27 01:33:33 +00001035 // return to the text section
Lang Hames9ff69c82015-04-24 19:11:51 +00001036 OutStreamer->SwitchSection(OutContext.getObjectFileInfo()->getTextSection());
Jack Carterc1b17ed2013-01-18 21:20:38 +00001037}
1038
Akira Hatanakaf2bcad92011-07-01 01:04:43 +00001039void MipsAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
1040 raw_ostream &OS) {
1041 // TODO: implement
1042}
1043
Sasa Stankovic8c5736b2014-02-28 10:00:38 +00001044// Align all targets of indirect branches on bundle size. Used only if target
1045// is NaCl.
1046void MipsAsmPrinter::NaClAlignIndirectJumpTargets(MachineFunction &MF) {
1047 // Align all blocks that are jumped to through jump table.
1048 if (MachineJumpTableInfo *JtInfo = MF.getJumpTableInfo()) {
1049 const std::vector<MachineJumpTableEntry> &JT = JtInfo->getJumpTables();
1050 for (unsigned I = 0; I < JT.size(); ++I) {
1051 const std::vector<MachineBasicBlock*> &MBBs = JT[I].MBBs;
1052
1053 for (unsigned J = 0; J < MBBs.size(); ++J)
1054 MBBs[J]->setAlignment(MIPS_NACL_BUNDLE_ALIGN);
1055 }
1056 }
1057
1058 // If basic block address is taken, block can be target of indirect branch.
Vasileios Kalintiris5a971a42016-04-15 20:43:17 +00001059 for (auto &MBB : MF) {
1060 if (MBB.hasAddressTaken())
1061 MBB.setAlignment(MIPS_NACL_BUNDLE_ALIGN);
Sasa Stankovic8c5736b2014-02-28 10:00:38 +00001062 }
1063}
1064
Sasa Stankovic7b061a42014-04-30 15:06:25 +00001065bool MipsAsmPrinter::isLongBranchPseudo(int Opcode) const {
1066 return (Opcode == Mips::LONG_BRANCH_LUi
1067 || Opcode == Mips::LONG_BRANCH_ADDiu
Sasa Stankovic7b061a42014-04-30 15:06:25 +00001068 || Opcode == Mips::LONG_BRANCH_DADDiu);
1069}
1070
Bob Wilson5a495fe2009-06-23 23:59:40 +00001071// Force static initialization.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +00001072extern "C" void LLVMInitializeMipsAsmPrinter() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +00001073 RegisterAsmPrinter<MipsAsmPrinter> X(TheMipsTarget);
1074 RegisterAsmPrinter<MipsAsmPrinter> Y(TheMipselTarget);
Akira Hatanaka3d673cc2011-09-21 03:00:58 +00001075 RegisterAsmPrinter<MipsAsmPrinter> A(TheMips64Target);
1076 RegisterAsmPrinter<MipsAsmPrinter> B(TheMips64elTarget);
Daniel Dunbare8338102009-07-15 20:24:03 +00001077}