blob: 0c603f5686875d269003ad8553637b8d9673aabf [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"
Chris Lattner4cd44982009-09-13 17:14:04 +000044#include "llvm/MC/MCSymbol.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;
101 MCInst TmpInst0;
102
103 if (Subtarget->hasMips64r6()) {
104 // MIPS64r6 should use (JALR64 ZERO_64, $rs)
105 TmpInst0.setOpcode(Mips::JALR64);
106 HasLinkReg = true;
107 } else if (Subtarget->hasMips32r6()) {
108 // MIPS32r6 should use (JALR ZERO, $rs)
109 TmpInst0.setOpcode(Mips::JALR);
110 HasLinkReg = true;
111 } else if (Subtarget->inMicroMipsMode())
112 // microMIPS should use (JR_MM $rs)
113 TmpInst0.setOpcode(Mips::JR_MM);
114 else {
115 // Everything else should use (JR $rs)
116 TmpInst0.setOpcode(Mips::JR);
117 }
118
119 MCOperand MCOp;
120
121 if (HasLinkReg) {
122 unsigned ZeroReg = Subtarget->isGP64bit() ? Mips::ZERO_64 : Mips::ZERO;
Jim Grosbache9119e42015-05-13 18:37:00 +0000123 TmpInst0.addOperand(MCOperand::createReg(ZeroReg));
Daniel Sanders338513b2014-07-09 10:16:07 +0000124 }
125
126 lowerOperand(MI->getOperand(0), MCOp);
127 TmpInst0.addOperand(MCOp);
128
129 EmitToStreamer(OutStreamer, TmpInst0);
130}
131
Akira Hatanakaddd12652011-07-07 20:10:52 +0000132void MipsAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000133 MipsTargetStreamer &TS = getTargetStreamer();
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000134 TS.forbidModuleDirective();
Daniel Sandersc7dbc632014-07-08 10:11:38 +0000135
Akira Hatanakaddd12652011-07-07 20:10:52 +0000136 if (MI->isDebugValue()) {
Bruno Cardoso Lopescd1d4472011-12-30 21:09:41 +0000137 SmallString<128> Str;
138 raw_svector_ostream OS(Str);
139
Akira Hatanakaddd12652011-07-07 20:10:52 +0000140 PrintDebugValueComment(MI, OS);
141 return;
142 }
143
Reed Kotler91ae9822013-10-27 21:57:36 +0000144 // If we just ended a constant pool, mark it as such.
145 if (InConstantPool && MI->getOpcode() != Mips::CONSTPOOL_ENTRY) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000146 OutStreamer->EmitDataRegion(MCDR_DataRegionEnd);
Reed Kotler91ae9822013-10-27 21:57:36 +0000147 InConstantPool = false;
148 }
149 if (MI->getOpcode() == Mips::CONSTPOOL_ENTRY) {
150 // CONSTPOOL_ENTRY - This instruction represents a floating
151 //constant pool in the function. The first operand is the ID#
152 // for this instruction, the second is the index into the
153 // MachineConstantPool that this is, the third is the size in
154 // bytes of this constant pool entry.
155 // The required alignment is specified on the basic block holding this MI.
156 //
157 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
158 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
159
160 // If this is the first entry of the pool, mark it.
161 if (!InConstantPool) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000162 OutStreamer->EmitDataRegion(MCDR_DataRegion);
Reed Kotler91ae9822013-10-27 21:57:36 +0000163 InConstantPool = true;
164 }
165
Lang Hames9ff69c82015-04-24 19:11:51 +0000166 OutStreamer->EmitLabel(GetCPISymbol(LabelId));
Reed Kotler91ae9822013-10-27 21:57:36 +0000167
168 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
169 if (MCPE.isMachineConstantPoolEntry())
170 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
171 else
172 EmitGlobalConstant(MCPE.Val.ConstVal);
173 return;
174 }
175
Rafael Espindola14d02fe2014-01-25 15:06:56 +0000176
Akira Hatanaka5ac78682012-06-13 23:25:52 +0000177 MachineBasicBlock::const_instr_iterator I = MI;
178 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
179
180 do {
Akira Hatanaka556135d2013-02-06 21:50:15 +0000181 // Do any auto-generated pseudo lowerings.
Lang Hames9ff69c82015-04-24 19:11:51 +0000182 if (emitPseudoExpansionLowering(*OutStreamer, &*I))
Akira Hatanaka556135d2013-02-06 21:50:15 +0000183 continue;
Jack Carterc20a21b2012-08-28 19:07:39 +0000184
Daniel Sanders338513b2014-07-09 10:16:07 +0000185 if (I->getOpcode() == Mips::PseudoReturn ||
Daniel Sandersf5a5fbd2014-07-09 10:21:59 +0000186 I->getOpcode() == Mips::PseudoReturn64 ||
187 I->getOpcode() == Mips::PseudoIndirectBranch ||
188 I->getOpcode() == Mips::PseudoIndirectBranch64) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000189 emitPseudoIndirectBranch(*OutStreamer, &*I);
Daniel Sanders338513b2014-07-09 10:16:07 +0000190 continue;
191 }
192
Reed Kotler76c9bcd2013-02-15 21:05:58 +0000193 // The inMips16Mode() test is not permanent.
194 // Some instructions are marked as pseudo right now which
195 // would make the test fail for the wrong reason but
196 // that will be fixed soon. We need this here because we are
197 // removing another test for this situation downstream in the
198 // callchain.
199 //
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000200 if (I->isPseudo() && !Subtarget->inMips16Mode()
201 && !isLongBranchPseudo(I->getOpcode()))
Reed Kotler76c9bcd2013-02-15 21:05:58 +0000202 llvm_unreachable("Pseudo opcode found in EmitInstruction()");
203
Akira Hatanaka556135d2013-02-06 21:50:15 +0000204 MCInst TmpInst0;
205 MCInstLowering.Lower(I, TmpInst0);
Lang Hames9ff69c82015-04-24 19:11:51 +0000206 EmitToStreamer(*OutStreamer, TmpInst0);
Akira Hatanaka556135d2013-02-06 21:50:15 +0000207 } while ((++I != E) && I->isInsideBundle()); // Delay slot check
Akira Hatanakaddd12652011-07-07 20:10:52 +0000208}
209
Akira Hatanakae2489122011-04-15 21:51:11 +0000210//===----------------------------------------------------------------------===//
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000211//
212// Mips Asm Directives
213//
214// -- Frame directive "frame Stackpointer, Stacksize, RARegister"
215// Describe the stack frame.
216//
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000217// -- Mask directives "(f)mask bitmask, offset"
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000218// Tells the assembler which registers are saved and where.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000219// bitmask - contain a little endian bitset indicating which registers are
220// saved on function prologue (e.g. with a 0x80000000 mask, the
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000221// assembler knows the register 31 (RA) is saved at prologue.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000222// offset - the position before stack pointer subtraction indicating where
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000223// the first saved register on prologue is located. (e.g. with a
224//
225// Consider the following function prologue:
226//
Bill Wendling97925ec2008-02-27 06:33:05 +0000227// .frame $fp,48,$ra
228// .mask 0xc0000000,-8
229// addiu $sp, $sp, -48
230// sw $ra, 40($sp)
231// sw $fp, 36($sp)
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000232//
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000233// With a 0xc0000000 mask, the assembler knows the register 31 (RA) and
234// 30 (FP) are saved at prologue. As the save order on prologue is from
235// left to right, RA is saved first. A -8 offset means that after the
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000236// stack pointer subtration, the first register in the mask (RA) will be
237// saved at address 48-8=40.
238//
Akira Hatanakae2489122011-04-15 21:51:11 +0000239//===----------------------------------------------------------------------===//
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000240
Akira Hatanakae2489122011-04-15 21:51:11 +0000241//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000242// Mask directives
Akira Hatanakae2489122011-04-15 21:51:11 +0000243//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000244
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000245// Create a bitmask with all callee saved registers for CPU or Floating Point
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000246// registers. For CPU registers consider RA, GP and FP for saving if necessary.
Rafael Espindola25fa2912014-01-27 04:33:11 +0000247void MipsAsmPrinter::printSavedRegsBitmask() {
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000248 // CPU and FPU Saved Registers Bitmasks
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000249 unsigned CPUBitmask = 0, FPUBitmask = 0;
250 int CPUTopSavedRegOff, FPUTopSavedRegOff;
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000251
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000252 // Set the CPU and FPU Bitmasks
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000253 const MachineFrameInfo *MFI = MF->getFrameInfo();
Eric Christophercba722f2015-03-21 03:13:07 +0000254 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000255 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000256 // size of stack area to which FP callee-saved regs are saved.
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000257 unsigned CPURegSize = Mips::GPR32RegClass.getSize();
Craig Topperc7242e02012-04-20 07:30:17 +0000258 unsigned FGR32RegSize = Mips::FGR32RegClass.getSize();
259 unsigned AFGR64RegSize = Mips::AFGR64RegClass.getSize();
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000260 bool HasAFGR64Reg = false;
261 unsigned CSFPRegsSize = 0;
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000262
Toma Tabacube218922015-04-09 10:54:16 +0000263 for (const auto &I : CSI) {
264 unsigned Reg = I.getReg();
Eric Christophercba722f2015-03-21 03:13:07 +0000265 unsigned RegNum = TRI->getEncodingValue(Reg);
Toma Tabacube218922015-04-09 10:54:16 +0000266
267 // If it's a floating point register, set the FPU Bitmask.
268 // If it's a general purpose register, set the CPU Bitmask.
269 if (Mips::FGR32RegClass.contains(Reg)) {
270 FPUBitmask |= (1 << RegNum);
271 CSFPRegsSize += FGR32RegSize;
272 } else if (Mips::AFGR64RegClass.contains(Reg)) {
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000273 FPUBitmask |= (3 << RegNum);
274 CSFPRegsSize += AFGR64RegSize;
275 HasAFGR64Reg = true;
Toma Tabacube218922015-04-09 10:54:16 +0000276 } else if (Mips::GPR32RegClass.contains(Reg))
277 CPUBitmask |= (1 << RegNum);
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000278 }
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000279
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000280 // FP Regs are saved right below where the virtual frame pointer points to.
281 FPUTopSavedRegOff = FPUBitmask ?
282 (HasAFGR64Reg ? -AFGR64RegSize : -FGR32RegSize) : 0;
283
284 // CPU Regs are saved below FP Regs.
285 CPUTopSavedRegOff = CPUBitmask ? -CSFPRegsSize - CPURegSize : 0;
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000286
Rafael Espindola25fa2912014-01-27 04:33:11 +0000287 MipsTargetStreamer &TS = getTargetStreamer();
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000288 // Print CPUBitmask
Rafael Espindola25fa2912014-01-27 04:33:11 +0000289 TS.emitMask(CPUBitmask, CPUTopSavedRegOff);
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000290
291 // Print FPUBitmask
Rafael Espindola25fa2912014-01-27 04:33:11 +0000292 TS.emitFMask(FPUBitmask, FPUTopSavedRegOff);
Bruno Cardoso Lopesbcda5e22007-07-11 23:24:41 +0000293}
294
Akira Hatanakae2489122011-04-15 21:51:11 +0000295//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000296// Frame and Set directives
Akira Hatanakae2489122011-04-15 21:51:11 +0000297//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000298
299/// Frame Directive
Chris Lattner5e596182010-04-04 07:05:53 +0000300void MipsAsmPrinter::emitFrameDirective() {
Eric Christophercba722f2015-03-21 03:13:07 +0000301 const TargetRegisterInfo &RI = *MF->getSubtarget().getRegisterInfo();
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000302
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000303 unsigned stackReg = RI.getFrameRegister(*MF);
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000304 unsigned returnReg = RI.getRARegister();
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000305 unsigned stackSize = MF->getFrameInfo()->getStackSize();
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000306
Rafael Espindola054234f2014-01-27 03:53:56 +0000307 getTargetStreamer().emitFrame(stackReg, stackSize, returnReg);
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000308}
309
310/// Emit Set directives.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000311const char *MipsAsmPrinter::getCurrentABIString() const {
Eric Christophera5762812015-01-26 17:33:46 +0000312 switch (static_cast<MipsTargetMachine &>(TM).getABI().GetEnumValue()) {
Daniel Sanderse2e25da2014-10-24 16:15:27 +0000313 case MipsABIInfo::ABI::O32: return "abi32";
314 case MipsABIInfo::ABI::N32: return "abiN32";
315 case MipsABIInfo::ABI::N64: return "abi64";
316 case MipsABIInfo::ABI::EABI: return "eabi32"; // TODO: handle eabi64
Dmitri Gribenkoca1e27b2012-09-10 21:26:47 +0000317 default: llvm_unreachable("Unknown Mips ABI");
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000318 }
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000319}
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000320
Chris Lattner5d9fb4b2010-01-27 23:23:58 +0000321void MipsAsmPrinter::EmitFunctionEntryLabel() {
Rafael Espindola6633d572014-01-14 18:57:12 +0000322 MipsTargetStreamer &TS = getTargetStreamer();
Sasa Stankovic8c5736b2014-02-28 10:00:38 +0000323
324 // NaCl sandboxing requires that indirect call instructions are masked.
325 // This means that function entry points should be bundle-aligned.
326 if (Subtarget->isTargetNaCl())
327 EmitAlignment(std::max(MF->getAlignment(), MIPS_NACL_BUNDLE_ALIGN));
328
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000329 if (Subtarget->inMicroMipsMode())
Rafael Espindola6633d572014-01-14 18:57:12 +0000330 TS.emitDirectiveSetMicroMips();
Matheus Almeidadc7e48e2014-04-16 11:46:59 +0000331 else
332 TS.emitDirectiveSetNoMicroMips();
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000333
Rafael Espindola6633d572014-01-14 18:57:12 +0000334 if (Subtarget->inMips16Mode())
335 TS.emitDirectiveSetMips16();
336 else
337 TS.emitDirectiveSetNoMips16();
Jack Carterab3cb422013-02-19 22:04:37 +0000338
Rafael Espindola6633d572014-01-14 18:57:12 +0000339 TS.emitDirectiveEnt(*CurrentFnSym);
Lang Hames9ff69c82015-04-24 19:11:51 +0000340 OutStreamer->EmitLabel(CurrentFnSym);
Chris Lattner5d9fb4b2010-01-27 23:23:58 +0000341}
342
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000343/// EmitFunctionBodyStart - Targets can override this to emit stuff before
344/// the first basic block in the function.
345void MipsAsmPrinter::EmitFunctionBodyStart() {
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000346 MipsTargetStreamer &TS = getTargetStreamer();
347
Rafael Espindola7d78b2a2013-10-29 16:24:21 +0000348 MCInstLowering.Initialize(&MF->getContext());
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +0000349
Duncan P. N. Exon Smith2e753142015-02-14 02:37:48 +0000350 bool IsNakedFunction = MF->getFunction()->hasFnAttribute(Attribute::Naked);
Reed Kotler0f2b10e2013-05-03 23:17:24 +0000351 if (!IsNakedFunction)
352 emitFrameDirective();
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000353
Rafael Espindola25fa2912014-01-27 04:33:11 +0000354 if (!IsNakedFunction)
355 printSavedRegsBitmask();
356
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000357 if (!Subtarget->inMips16Mode()) {
358 TS.emitDirectiveSetNoReorder();
359 TS.emitDirectiveSetNoMacro();
360 TS.emitDirectiveSetNoAt();
Akira Hatanaka8f3573032012-05-12 00:48:43 +0000361 }
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000362}
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000363
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000364/// EmitFunctionBodyEnd - Targets can override this to emit stuff after
365/// the last basic block in the function.
366void MipsAsmPrinter::EmitFunctionBodyEnd() {
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000367 MipsTargetStreamer &TS = getTargetStreamer();
368
Chris Lattnerfd97a332010-01-28 01:48:52 +0000369 // There are instruction for this macros, but they must
370 // always be at the function end, and we can't emit and
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000371 // break with BB logic.
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000372 if (!Subtarget->inMips16Mode()) {
373 TS.emitDirectiveSetAt();
374 TS.emitDirectiveSetMacro();
375 TS.emitDirectiveSetReorder();
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000376 }
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000377 TS.emitDirectiveEnd(CurrentFnSym->getName());
Reed Kotler91ae9822013-10-27 21:57:36 +0000378 // Make sure to terminate any constant pools that were at the end
379 // of the function.
380 if (!InConstantPool)
381 return;
382 InConstantPool = false;
Lang Hames9ff69c82015-04-24 19:11:51 +0000383 OutStreamer->EmitDataRegion(MCDR_DataRegionEnd);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000384}
385
Vasileios Kalintiris42544d62015-05-08 09:10:15 +0000386void MipsAsmPrinter::EmitBasicBlockEnd(const MachineBasicBlock &MBB) {
387 MipsTargetStreamer &TS = getTargetStreamer();
388 if (MBB.size() == 0)
389 TS.emitDirectiveInsn();
390}
391
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000392/// isBlockOnlyReachableByFallthough - Return true if the basic block has
393/// exactly one predecessor and the control transfer mechanism between
394/// the predecessor and this block is a fall-through.
Akira Hatanakae2489122011-04-15 21:51:11 +0000395bool MipsAsmPrinter::isBlockOnlyReachableByFallthrough(const MachineBasicBlock*
396 MBB) const {
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000397 // The predecessor has to be immediately before this block.
398 const MachineBasicBlock *Pred = *MBB->pred_begin();
399
400 // If the predecessor is a switch statement, assume a jump table
401 // implementation, so it is not a fall through.
402 if (const BasicBlock *bb = Pred->getBasicBlock())
403 if (isa<SwitchInst>(bb->getTerminator()))
404 return false;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000405
Akira Hatanakae625ba42011-04-01 18:57:38 +0000406 // If this is a landing pad, it isn't a fall through. If it has no preds,
407 // then nothing falls through to it.
408 if (MBB->isLandingPad() || MBB->pred_empty())
409 return false;
410
411 // If there isn't exactly one predecessor, it can't be a fall through.
412 MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI;
413 ++PI2;
Jia Liuf54f60f2012-02-28 07:46:26 +0000414
Akira Hatanakae625ba42011-04-01 18:57:38 +0000415 if (PI2 != MBB->pred_end())
Jia Liuf54f60f2012-02-28 07:46:26 +0000416 return false;
Akira Hatanakae625ba42011-04-01 18:57:38 +0000417
418 // The predecessor has to be immediately before this block.
419 if (!Pred->isLayoutSuccessor(MBB))
420 return false;
Jia Liuf54f60f2012-02-28 07:46:26 +0000421
Akira Hatanakae625ba42011-04-01 18:57:38 +0000422 // If the block is completely empty, then it definitely does fall through.
423 if (Pred->empty())
424 return true;
Jia Liuf54f60f2012-02-28 07:46:26 +0000425
Akira Hatanakae625ba42011-04-01 18:57:38 +0000426 // Otherwise, check the last instruction.
427 // Check if the last terminator is an unconditional branch.
428 MachineBasicBlock::const_iterator I = Pred->end();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000429 while (I != Pred->begin() && !(--I)->isTerminator()) ;
Akira Hatanakae625ba42011-04-01 18:57:38 +0000430
Evan Cheng7f8e5632011-12-07 07:15:52 +0000431 return !I->isBarrier();
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000432}
433
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000434// Print out an operand for an inline asm expression.
Eric Christophered51b9e2012-05-10 21:48:22 +0000435bool MipsAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000436 unsigned AsmVariant, const char *ExtraCode,
Chris Lattner3bb09762010-04-04 05:29:35 +0000437 raw_ostream &O) {
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000438 // Does this asm operand have a single letter operand modifier?
Eric Christophered51b9e2012-05-10 21:48:22 +0000439 if (ExtraCode && ExtraCode[0]) {
440 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000441
Eric Christophered51b9e2012-05-10 21:48:22 +0000442 const MachineOperand &MO = MI->getOperand(OpNum);
443 switch (ExtraCode[0]) {
Eric Christopherbc5d2492012-05-19 00:51:56 +0000444 default:
Jack Carterb2fd5f62012-06-21 17:14:46 +0000445 // See if this is a generic print operand
446 return AsmPrinter::PrintAsmOperand(MI,OpNum,AsmVariant,ExtraCode,O);
Eric Christopherbc5d2492012-05-19 00:51:56 +0000447 case 'X': // hex const int
448 if ((MO.getType()) != MachineOperand::MO_Immediate)
449 return true;
Benjamin Kramer33b46912015-05-23 16:53:07 +0000450 O << "0x" << Twine::utohexstr(MO.getImm());
Eric Christopherbc5d2492012-05-19 00:51:56 +0000451 return false;
452 case 'x': // hex const int (low 16 bits)
453 if ((MO.getType()) != MachineOperand::MO_Immediate)
454 return true;
Benjamin Kramer33b46912015-05-23 16:53:07 +0000455 O << "0x" << Twine::utohexstr(MO.getImm() & 0xffff);
Eric Christopherbc5d2492012-05-19 00:51:56 +0000456 return false;
457 case 'd': // decimal const int
458 if ((MO.getType()) != MachineOperand::MO_Immediate)
459 return true;
460 O << MO.getImm();
461 return false;
Eric Christopherf481ab32012-05-30 19:05:19 +0000462 case 'm': // decimal const int minus 1
463 if ((MO.getType()) != MachineOperand::MO_Immediate)
464 return true;
465 O << MO.getImm() - 1;
466 return false;
Jack Carter27747b52012-06-28 20:46:26 +0000467 case 'z': {
468 // $0 if zero, regular printing otherwise
Toma Tabacu27cab752014-11-06 14:25:42 +0000469 if (MO.getType() == MachineOperand::MO_Immediate && MO.getImm() == 0) {
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000470 O << "$0";
Toma Tabacu27cab752014-11-06 14:25:42 +0000471 return false;
472 }
473 // If not, call printOperand as normal.
474 break;
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000475 }
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000476 case 'D': // Second part of a double word register operand
477 case 'L': // Low order register of a double word register operand
Jack Cartera62ba822012-07-18 06:41:36 +0000478 case 'M': // High order register of a double word register operand
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000479 {
Jack Carterb2af5122012-07-05 23:58:21 +0000480 if (OpNum == 0)
481 return true;
482 const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
483 if (!FlagsOP.isImm())
484 return true;
485 unsigned Flags = FlagsOP.getImm();
486 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Jack Carter2ab73b12012-07-06 02:44:22 +0000487 // Number of registers represented by this operand. We are looking
488 // for 2 for 32 bit mode and 1 for 64 bit mode.
Jack Carterb2af5122012-07-05 23:58:21 +0000489 if (NumVals != 2) {
Jack Carter2ab73b12012-07-06 02:44:22 +0000490 if (Subtarget->isGP64bit() && NumVals == 1 && MO.isReg()) {
Jack Carterb2af5122012-07-05 23:58:21 +0000491 unsigned Reg = MO.getReg();
492 O << '$' << MipsInstPrinter::getRegisterName(Reg);
493 return false;
494 }
495 return true;
496 }
Jack Carter42ebf982012-07-11 21:41:49 +0000497
498 unsigned RegOp = OpNum;
499 if (!Subtarget->isGP64bit()){
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000500 // Endianess reverses which register holds the high or low value
Jack Cartera62ba822012-07-18 06:41:36 +0000501 // between M and L.
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000502 switch(ExtraCode[0]) {
Jack Cartera62ba822012-07-18 06:41:36 +0000503 case 'M':
504 RegOp = (Subtarget->isLittle()) ? OpNum + 1 : OpNum;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000505 break;
506 case 'L':
Jack Cartera62ba822012-07-18 06:41:36 +0000507 RegOp = (Subtarget->isLittle()) ? OpNum : OpNum + 1;
508 break;
509 case 'D': // Always the second part
510 RegOp = OpNum + 1;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000511 }
512 if (RegOp >= MI->getNumOperands())
513 return true;
514 const MachineOperand &MO = MI->getOperand(RegOp);
515 if (!MO.isReg())
516 return true;
517 unsigned Reg = MO.getReg();
518 O << '$' << MipsInstPrinter::getRegisterName(Reg);
519 return false;
Jack Carterb2af5122012-07-05 23:58:21 +0000520 }
Eric Christophered51b9e2012-05-10 21:48:22 +0000521 }
Daniel Sanders8b59af12013-11-12 12:56:01 +0000522 case 'w':
523 // Print MSA registers for the 'f' constraint
524 // In LLVM, the 'w' modifier doesn't need to do anything.
525 // We can just call printOperand as normal.
526 break;
Jack Carter2ab73b12012-07-06 02:44:22 +0000527 }
528 }
Eric Christophered51b9e2012-05-10 21:48:22 +0000529
530 printOperand(MI, OpNum, O);
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000531 return false;
532}
533
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000534bool MipsAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
535 unsigned OpNum, unsigned AsmVariant,
536 const char *ExtraCode,
537 raw_ostream &O) {
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000538 assert(OpNum + 1 < MI->getNumOperands() && "Insufficient operands");
539 const MachineOperand &BaseMO = MI->getOperand(OpNum);
540 const MachineOperand &OffsetMO = MI->getOperand(OpNum + 1);
541 assert(BaseMO.isReg() && "Unexpected base pointer for inline asm memory operand.");
542 assert(OffsetMO.isImm() && "Unexpected offset for inline asm memory operand.");
543 int Offset = OffsetMO.getImm();
544
Jack Carterb04e3572013-04-09 23:19:50 +0000545 // Currently we are expecting either no ExtraCode or 'D'
546 if (ExtraCode) {
547 if (ExtraCode[0] == 'D')
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000548 Offset += 4;
Jack Carterb04e3572013-04-09 23:19:50 +0000549 else
550 return true; // Unknown modifier.
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000551 // FIXME: M = high order bits
552 // FIXME: L = low order bits
Jack Carterb04e3572013-04-09 23:19:50 +0000553 }
Jia Liuf54f60f2012-02-28 07:46:26 +0000554
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000555 O << Offset << "($" << MipsInstPrinter::getRegisterName(BaseMO.getReg()) << ")";
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000556
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000557 return false;
558}
559
Chris Lattner76c564b2010-04-04 04:47:45 +0000560void MipsAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
561 raw_ostream &O) {
Eric Christopher8b770652015-01-26 19:03:15 +0000562 const DataLayout *DL = TM.getDataLayout();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000563 const MachineOperand &MO = MI->getOperand(opNum);
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +0000564 bool closeP = false;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000565
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000566 if (MO.getTargetFlags())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000567 closeP = true;
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000568
569 switch(MO.getTargetFlags()) {
570 case MipsII::MO_GPREL: O << "%gp_rel("; break;
571 case MipsII::MO_GOT_CALL: O << "%call16("; break;
Akira Hatanaka56d9ef52011-04-01 21:41:06 +0000572 case MipsII::MO_GOT: O << "%got("; break;
573 case MipsII::MO_ABS_HI: O << "%hi("; break;
574 case MipsII::MO_ABS_LO: O << "%lo("; break;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +0000575 case MipsII::MO_TLSGD: O << "%tlsgd("; break;
576 case MipsII::MO_GOTTPREL: O << "%gottprel("; break;
577 case MipsII::MO_TPREL_HI: O << "%tprel_hi("; break;
578 case MipsII::MO_TPREL_LO: O << "%tprel_lo("; break;
Akira Hatanaka25ce3642011-09-22 03:09:07 +0000579 case MipsII::MO_GPOFF_HI: O << "%hi(%neg(%gp_rel("; break;
580 case MipsII::MO_GPOFF_LO: O << "%lo(%neg(%gp_rel("; break;
581 case MipsII::MO_GOT_DISP: O << "%got_disp("; break;
582 case MipsII::MO_GOT_PAGE: O << "%got_page("; break;
583 case MipsII::MO_GOT_OFST: O << "%got_ofst("; break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000584 }
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000585
Chris Lattnereb2cc682009-09-13 20:31:40 +0000586 switch (MO.getType()) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000587 case MachineOperand::MO_Register:
Akira Hatanaka9c6028f2011-07-07 23:56:50 +0000588 O << '$'
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000589 << StringRef(MipsInstPrinter::getRegisterName(MO.getReg())).lower();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000590 break;
591
592 case MachineOperand::MO_Immediate:
Akira Hatanaka2db176c2011-05-24 21:22:21 +0000593 O << MO.getImm();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000594 break;
595
596 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner29bdac42010-03-13 21:04:28 +0000597 O << *MO.getMBB()->getSymbol();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000598 return;
599
600 case MachineOperand::MO_GlobalAddress:
Rafael Espindola79858aa2013-10-29 17:07:16 +0000601 O << *getSymbol(MO.getGlobal());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000602 break;
603
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000604 case MachineOperand::MO_BlockAddress: {
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000605 MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000606 O << BA->getName();
607 break;
608 }
609
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000610 case MachineOperand::MO_ConstantPoolIndex:
Rafael Espindola58873562014-01-03 19:21:54 +0000611 O << DL->getPrivateGlobalPrefix() << "CPI"
Chris Lattnera5bb3702007-12-30 23:10:15 +0000612 << getFunctionNumber() << "_" << MO.getIndex();
Bruno Cardoso Lopes4713b282009-11-19 06:06:13 +0000613 if (MO.getOffset())
614 O << "+" << MO.getOffset();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000615 break;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000616
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000617 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000618 llvm_unreachable("<unknown operand type>");
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000619 }
620
621 if (closeP) O << ")";
622}
623
Chris Lattner76c564b2010-04-04 04:47:45 +0000624void MipsAsmPrinter::printUnsignedImm(const MachineInstr *MI, int opNum,
625 raw_ostream &O) {
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000626 const MachineOperand &MO = MI->getOperand(opNum);
Devang Patel12f68552010-04-27 22:24:37 +0000627 if (MO.isImm())
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000628 O << (unsigned short int)MO.getImm();
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000629 else
Chris Lattner76c564b2010-04-04 04:47:45 +0000630 printOperand(MI, opNum, O);
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000631}
632
Daniel Sanders3f6eb542013-11-12 10:45:18 +0000633void MipsAsmPrinter::printUnsignedImm8(const MachineInstr *MI, int opNum,
634 raw_ostream &O) {
635 const MachineOperand &MO = MI->getOperand(opNum);
636 if (MO.isImm())
637 O << (unsigned short int)(unsigned char)MO.getImm();
638 else
639 printOperand(MI, opNum, O);
640}
641
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000642void MipsAsmPrinter::
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000643printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O) {
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000644 // Load/Store memory operands -- imm($reg)
645 // If PIC target the target is loaded as the
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +0000646 // pattern lw $25,%call16($28)
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000647
648 // opNum can be invalid if instruction has reglist as operand.
649 // MemOperand is always last operand of instruction (base + offset).
650 switch (MI->getOpcode()) {
651 default:
652 break;
653 case Mips::SWM32_MM:
654 case Mips::LWM32_MM:
655 opNum = MI->getNumOperands() - 2;
656 break;
657 }
658
Chris Lattner76c564b2010-04-04 04:47:45 +0000659 printOperand(MI, opNum+1, O);
Akira Hatanaka2e766ed2011-07-07 18:57:00 +0000660 O << "(";
661 printOperand(MI, opNum, O);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000662 O << ")";
663}
664
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000665void MipsAsmPrinter::
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000666printMemOperandEA(const MachineInstr *MI, int opNum, raw_ostream &O) {
667 // when using stack locations for not load/store instructions
668 // print the same way as all normal 3 operand instructions.
669 printOperand(MI, opNum, O);
670 O << ", ";
671 printOperand(MI, opNum+1, O);
672 return;
673}
674
675void MipsAsmPrinter::
Chris Lattner76c564b2010-04-04 04:47:45 +0000676printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
677 const char *Modifier) {
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000678 const MachineOperand &MO = MI->getOperand(opNum);
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000679 O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000680}
681
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000682void MipsAsmPrinter::
683printRegisterList(const MachineInstr *MI, int opNum, raw_ostream &O) {
684 for (int i = opNum, e = MI->getNumOperands(); i != e; ++i) {
685 if (i != opNum) O << ", ";
686 printOperand(MI, i, O);
687 }
688}
689
Bob Wilsonb633d7a2009-09-30 22:06:26 +0000690void MipsAsmPrinter::EmitStartOfAsmFile(Module &M) {
Eric Christopher8af49b32015-02-18 01:01:57 +0000691
692 // Compute MIPS architecture attributes based on the default subtarget
693 // that we'd have constructed. Module level directives aren't LTO
694 // clean anyhow.
695 // FIXME: For ifunc related functions we could iterate over and look
696 // for a feature string that doesn't match the default one.
697 StringRef TT = TM.getTargetTriple();
698 StringRef CPU =
699 MIPS_MC::selectMipsCPU(TM.getTargetTriple(), TM.getTargetCPU());
700 StringRef FS = TM.getTargetFeatureString();
701 const MipsTargetMachine &MTM = static_cast<const MipsTargetMachine &>(TM);
702 const MipsSubtarget STI(TT, CPU, FS, MTM.isLittleEndian(), MTM);
703
704 bool IsABICalls = STI.isABICalls();
705 const MipsABIInfo &ABI = MTM.getABI();
Daniel Sanders16fa1db2014-04-16 13:58:57 +0000706 if (IsABICalls) {
707 getTargetStreamer().emitDirectiveAbiCalls();
Eric Christopher8ef7a6a2014-07-18 00:08:53 +0000708 Reloc::Model RM = TM.getRelocationModel();
Daniel Sanders16fa1db2014-04-16 13:58:57 +0000709 // FIXME: This condition should be a lot more complicated that it is here.
710 // Ideally it should test for properties of the ABI and not the ABI
711 // itself.
712 // For the moment, I'm only correcting enough to make MIPS-IV work.
Eric Christopherd86af632015-01-29 23:27:45 +0000713 if (RM == Reloc::Static && !ABI.IsN64())
Daniel Sanders16fa1db2014-04-16 13:58:57 +0000714 getTargetStreamer().emitDirectiveOptionPic0();
715 }
Jack Carterf9f753c2013-06-18 19:47:15 +0000716
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000717 // Tell the assembler which ABI we are using
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000718 std::string SectionName = std::string(".mdebug.") + getCurrentABIString();
Lang Hames9ff69c82015-04-24 19:11:51 +0000719 OutStreamer->SwitchSection(
Rafael Espindolaba31e272015-01-29 17:33:21 +0000720 OutContext.getELFSection(SectionName, ELF::SHT_PROGBITS, 0));
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000721
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000722 // NaN: At the moment we only support:
723 // 1. .nan legacy (default)
724 // 2. .nan 2008
Eric Christopher8af49b32015-02-18 01:01:57 +0000725 STI.isNaN2008() ? getTargetStreamer().emitDirectiveNaN2008()
726 : getTargetStreamer().emitDirectiveNaNLegacy();
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000727
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000728 // TODO: handle O64 ABI
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000729
Eric Christopherd86af632015-01-29 23:27:45 +0000730 if (ABI.IsEABI()) {
Eric Christopher8af49b32015-02-18 01:01:57 +0000731 if (STI.isGP32bit())
Lang Hames9ff69c82015-04-24 19:11:51 +0000732 OutStreamer->SwitchSection(OutContext.getELFSection(".gcc_compiled_long32",
733 ELF::SHT_PROGBITS, 0));
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000734 else
Lang Hames9ff69c82015-04-24 19:11:51 +0000735 OutStreamer->SwitchSection(OutContext.getELFSection(".gcc_compiled_long64",
736 ELF::SHT_PROGBITS, 0));
Benjamin Kramer0151d7b2010-04-05 10:17:15 +0000737 }
Daniel Sanders7e527422014-07-10 13:38:23 +0000738
Eric Christopher8af49b32015-02-18 01:01:57 +0000739 getTargetStreamer().updateABIInfo(STI);
Daniel Sanders7e527422014-07-10 13:38:23 +0000740
Daniel Sanderse22244b2014-07-21 15:25:24 +0000741 // We should always emit a '.module fp=...' but binutils 2.24 does not accept
742 // it. We therefore emit it when it contradicts the ABI defaults (-mfpxx or
743 // -mfp64) and omit it otherwise.
Eric Christopher8af49b32015-02-18 01:01:57 +0000744 if (ABI.IsO32() && (STI.isABI_FPXX() || STI.isFP64bit()))
Daniel Sanderse22244b2014-07-21 15:25:24 +0000745 getTargetStreamer().emitDirectiveModuleFP();
746
747 // We should always emit a '.module [no]oddspreg' but binutils 2.24 does not
748 // accept it. We therefore emit it when it contradicts the default or an
749 // option has changed the default (i.e. FPXX) and omit it otherwise.
Eric Christopher8af49b32015-02-18 01:01:57 +0000750 if (ABI.IsO32() && (!STI.useOddSPReg() || STI.isABI_FPXX()))
751 getTargetStreamer().emitDirectiveModuleOddSPReg(STI.useOddSPReg(),
Eric Christopherd86af632015-01-29 23:27:45 +0000752 ABI.IsO32());
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000753}
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000754
Eric Christopher64d35be2015-02-19 19:52:25 +0000755void MipsAsmPrinter::emitInlineAsmStart() const {
Toma Tabacua23f13c2014-12-17 10:56:16 +0000756 MipsTargetStreamer &TS = getTargetStreamer();
757
Toma Tabacu68e8a9c2015-01-09 15:00:30 +0000758 // GCC's choice of assembler options for inline assembly code ('at', 'macro'
759 // and 'reorder') is different from LLVM's choice for generated code ('noat',
760 // 'nomacro' and 'noreorder').
761 // In order to maintain compatibility with inline assembly code which depends
762 // on GCC's assembler options being used, we have to switch to those options
763 // for the duration of the inline assembly block and then switch back.
Toma Tabacua23f13c2014-12-17 10:56:16 +0000764 TS.emitDirectiveSetPush();
765 TS.emitDirectiveSetAt();
766 TS.emitDirectiveSetMacro();
767 TS.emitDirectiveSetReorder();
Lang Hames9ff69c82015-04-24 19:11:51 +0000768 OutStreamer->AddBlankLine();
Toma Tabacua23f13c2014-12-17 10:56:16 +0000769}
770
771void MipsAsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
772 const MCSubtargetInfo *EndInfo) const {
Lang Hames9ff69c82015-04-24 19:11:51 +0000773 OutStreamer->AddBlankLine();
Toma Tabacua23f13c2014-12-17 10:56:16 +0000774 getTargetStreamer().emitDirectiveSetPop();
775}
776
Eric Christopher327fc972015-02-21 08:48:22 +0000777void MipsAsmPrinter::EmitJal(const MCSubtargetInfo &STI, MCSymbol *Symbol) {
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000778 MCInst I;
779 I.setOpcode(Mips::JAL);
780 I.addOperand(
Jim Grosbach13760bd2015-05-30 01:25:56 +0000781 MCOperand::createExpr(MCSymbolRefExpr::create(Symbol, OutContext)));
Lang Hames9ff69c82015-04-24 19:11:51 +0000782 OutStreamer->EmitInstruction(I, STI);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000783}
784
Eric Christopher327fc972015-02-21 08:48:22 +0000785void MipsAsmPrinter::EmitInstrReg(const MCSubtargetInfo &STI, unsigned Opcode,
786 unsigned Reg) {
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000787 MCInst I;
788 I.setOpcode(Opcode);
Jim Grosbache9119e42015-05-13 18:37:00 +0000789 I.addOperand(MCOperand::createReg(Reg));
Lang Hames9ff69c82015-04-24 19:11:51 +0000790 OutStreamer->EmitInstruction(I, STI);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000791}
792
Eric Christopher327fc972015-02-21 08:48:22 +0000793void MipsAsmPrinter::EmitInstrRegReg(const MCSubtargetInfo &STI,
794 unsigned Opcode, unsigned Reg1,
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000795 unsigned Reg2) {
796 MCInst I;
797 //
798 // Because of the current td files for Mips32, the operands for MTC1
799 // appear backwards from their normal assembly order. It's not a trivial
800 // change to fix this in the td file so we adjust for it here.
801 //
802 if (Opcode == Mips::MTC1) {
803 unsigned Temp = Reg1;
804 Reg1 = Reg2;
805 Reg2 = Temp;
806 }
807 I.setOpcode(Opcode);
Jim Grosbache9119e42015-05-13 18:37:00 +0000808 I.addOperand(MCOperand::createReg(Reg1));
809 I.addOperand(MCOperand::createReg(Reg2));
Lang Hames9ff69c82015-04-24 19:11:51 +0000810 OutStreamer->EmitInstruction(I, STI);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000811}
812
Eric Christopher327fc972015-02-21 08:48:22 +0000813void MipsAsmPrinter::EmitInstrRegRegReg(const MCSubtargetInfo &STI,
814 unsigned Opcode, unsigned Reg1,
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000815 unsigned Reg2, unsigned Reg3) {
816 MCInst I;
817 I.setOpcode(Opcode);
Jim Grosbache9119e42015-05-13 18:37:00 +0000818 I.addOperand(MCOperand::createReg(Reg1));
819 I.addOperand(MCOperand::createReg(Reg2));
820 I.addOperand(MCOperand::createReg(Reg3));
Lang Hames9ff69c82015-04-24 19:11:51 +0000821 OutStreamer->EmitInstruction(I, STI);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000822}
823
Eric Christopher327fc972015-02-21 08:48:22 +0000824void MipsAsmPrinter::EmitMovFPIntPair(const MCSubtargetInfo &STI,
825 unsigned MovOpc, unsigned Reg1,
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000826 unsigned Reg2, unsigned FPReg1,
827 unsigned FPReg2, bool LE) {
828 if (!LE) {
829 unsigned temp = Reg1;
830 Reg1 = Reg2;
831 Reg2 = temp;
832 }
Eric Christopher327fc972015-02-21 08:48:22 +0000833 EmitInstrRegReg(STI, MovOpc, Reg1, FPReg1);
834 EmitInstrRegReg(STI, MovOpc, Reg2, FPReg2);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000835}
836
Eric Christopher327fc972015-02-21 08:48:22 +0000837void MipsAsmPrinter::EmitSwapFPIntParams(const MCSubtargetInfo &STI,
838 Mips16HardFloatInfo::FPParamVariant PV,
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000839 bool LE, bool ToFP) {
840 using namespace Mips16HardFloatInfo;
841 unsigned MovOpc = ToFP ? Mips::MTC1 : Mips::MFC1;
842 switch (PV) {
843 case FSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000844 EmitInstrRegReg(STI, MovOpc, Mips::A0, Mips::F12);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000845 break;
846 case FFSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000847 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F14, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000848 break;
849 case FDSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000850 EmitInstrRegReg(STI, MovOpc, Mips::A0, Mips::F12);
851 EmitMovFPIntPair(STI, MovOpc, Mips::A2, Mips::A3, Mips::F14, Mips::F15, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000852 break;
853 case DSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000854 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000855 break;
856 case DDSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000857 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
858 EmitMovFPIntPair(STI, MovOpc, Mips::A2, Mips::A3, Mips::F14, Mips::F15, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000859 break;
860 case DFSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000861 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
862 EmitInstrRegReg(STI, MovOpc, Mips::A2, Mips::F14);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000863 break;
864 case NoSig:
865 return;
866 }
867}
868
Eric Christopher327fc972015-02-21 08:48:22 +0000869void MipsAsmPrinter::EmitSwapFPIntRetval(
870 const MCSubtargetInfo &STI, Mips16HardFloatInfo::FPReturnVariant RV,
871 bool LE) {
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000872 using namespace Mips16HardFloatInfo;
873 unsigned MovOpc = Mips::MFC1;
874 switch (RV) {
875 case FRet:
Eric Christopher327fc972015-02-21 08:48:22 +0000876 EmitInstrRegReg(STI, MovOpc, Mips::V0, Mips::F0);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000877 break;
878 case DRet:
Eric Christopher327fc972015-02-21 08:48:22 +0000879 EmitMovFPIntPair(STI, MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000880 break;
881 case CFRet:
Eric Christopher327fc972015-02-21 08:48:22 +0000882 EmitMovFPIntPair(STI, MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000883 break;
884 case CDRet:
Eric Christopher327fc972015-02-21 08:48:22 +0000885 EmitMovFPIntPair(STI, MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
886 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F2, Mips::F3, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000887 break;
888 case NoFPRet:
889 break;
890 }
891}
892
893void MipsAsmPrinter::EmitFPCallStub(
894 const char *Symbol, const Mips16HardFloatInfo::FuncSignature *Signature) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000895 MCSymbol *MSymbol = OutContext.getOrCreateSymbol(StringRef(Symbol));
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000896 using namespace Mips16HardFloatInfo;
Eric Christopherbb401642015-02-21 08:32:22 +0000897 bool LE = getDataLayout().isLittleEndian();
Eric Christopher327fc972015-02-21 08:48:22 +0000898 // Construct a local MCSubtargetInfo here.
899 // This is because the MachineFunction won't exist (but have not yet been
900 // freed) and since we're at the global level we can use the default
901 // constructed subtarget.
902 std::unique_ptr<MCSubtargetInfo> STI(TM.getTarget().createMCSubtargetInfo(
903 TM.getTargetTriple(), TM.getTargetCPU(), TM.getTargetFeatureString()));
904
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000905 //
906 // .global xxxx
907 //
Lang Hames9ff69c82015-04-24 19:11:51 +0000908 OutStreamer->EmitSymbolAttribute(MSymbol, MCSA_Global);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000909 const char *RetType;
910 //
911 // make the comment field identifying the return and parameter
912 // types of the floating point stub
913 // # Stub function to call rettype xxxx (params)
914 //
915 switch (Signature->RetSig) {
916 case FRet:
917 RetType = "float";
918 break;
919 case DRet:
920 RetType = "double";
921 break;
922 case CFRet:
923 RetType = "complex";
924 break;
925 case CDRet:
926 RetType = "double complex";
927 break;
928 case NoFPRet:
929 RetType = "";
930 break;
931 }
932 const char *Parms;
933 switch (Signature->ParamSig) {
934 case FSig:
935 Parms = "float";
936 break;
937 case FFSig:
938 Parms = "float, float";
939 break;
940 case FDSig:
941 Parms = "float, double";
942 break;
943 case DSig:
944 Parms = "double";
945 break;
946 case DDSig:
947 Parms = "double, double";
948 break;
949 case DFSig:
950 Parms = "double, float";
951 break;
952 case NoSig:
953 Parms = "";
954 break;
955 }
Lang Hames9ff69c82015-04-24 19:11:51 +0000956 OutStreamer->AddComment("\t# Stub function to call " + Twine(RetType) + " " +
957 Twine(Symbol) + " (" + Twine(Parms) + ")");
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000958 //
959 // probably not necessary but we save and restore the current section state
960 //
Lang Hames9ff69c82015-04-24 19:11:51 +0000961 OutStreamer->PushSection();
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000962 //
963 // .section mips16.call.fpxxxx,"ax",@progbits
964 //
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000965 MCSectionELF *M = OutContext.getELFSection(
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000966 ".mips16.call.fp." + std::string(Symbol), ELF::SHT_PROGBITS,
Rafael Espindolaba31e272015-01-29 17:33:21 +0000967 ELF::SHF_ALLOC | ELF::SHF_EXECINSTR);
Lang Hames9ff69c82015-04-24 19:11:51 +0000968 OutStreamer->SwitchSection(M, nullptr);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000969 //
970 // .align 2
971 //
Lang Hames9ff69c82015-04-24 19:11:51 +0000972 OutStreamer->EmitValueToAlignment(4);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000973 MipsTargetStreamer &TS = getTargetStreamer();
974 //
975 // .set nomips16
976 // .set nomicromips
977 //
978 TS.emitDirectiveSetNoMips16();
979 TS.emitDirectiveSetNoMicroMips();
980 //
981 // .ent __call_stub_fp_xxxx
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000982 // .type __call_stub_fp_xxxx,@function
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000983 // __call_stub_fp_xxxx:
984 //
985 std::string x = "__call_stub_fp_" + std::string(Symbol);
Jim Grosbach6f482002015-05-18 18:43:14 +0000986 MCSymbol *Stub = OutContext.getOrCreateSymbol(StringRef(x));
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000987 TS.emitDirectiveEnt(*Stub);
988 MCSymbol *MType =
Jim Grosbach6f482002015-05-18 18:43:14 +0000989 OutContext.getOrCreateSymbol("__call_stub_fp_" + Twine(Symbol));
Lang Hames9ff69c82015-04-24 19:11:51 +0000990 OutStreamer->EmitSymbolAttribute(MType, MCSA_ELF_TypeFunction);
991 OutStreamer->EmitLabel(Stub);
Eric Christopherd5bc07e2015-02-21 08:32:38 +0000992
993 // Only handle non-pic for now.
994 assert(TM.getRelocationModel() != Reloc::PIC_ &&
995 "should not be here if we are compiling pic");
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000996 TS.emitDirectiveSetReorder();
997 //
998 // We need to add a MipsMCExpr class to MCTargetDesc to fully implement
999 // stubs without raw text but this current patch is for compiler generated
1000 // functions and they all return some value.
1001 // The calling sequence for non pic is different in that case and we need
1002 // to implement %lo and %hi in order to handle the case of no return value
1003 // See the corresponding method in Mips16HardFloat for details.
1004 //
1005 // mov the return address to S2.
1006 // we have no stack space to store it and we are about to make another call.
1007 // We need to make sure that the enclosing function knows to save S2
1008 // This should have already been handled.
1009 //
1010 // Mov $18, $31
1011
Eric Christopher327fc972015-02-21 08:48:22 +00001012 EmitInstrRegRegReg(*STI, Mips::ADDu, Mips::S2, Mips::RA, Mips::ZERO);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001013
Eric Christopher327fc972015-02-21 08:48:22 +00001014 EmitSwapFPIntParams(*STI, Signature->ParamSig, LE, true);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001015
1016 // Jal xxxx
1017 //
Eric Christopher327fc972015-02-21 08:48:22 +00001018 EmitJal(*STI, MSymbol);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001019
1020 // fix return values
Eric Christopher327fc972015-02-21 08:48:22 +00001021 EmitSwapFPIntRetval(*STI, Signature->RetSig, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001022 //
1023 // do the return
1024 // if (Signature->RetSig == NoFPRet)
1025 // llvm_unreachable("should not be any stubs here with no return value");
1026 // else
Eric Christopher327fc972015-02-21 08:48:22 +00001027 EmitInstrReg(*STI, Mips::JR, Mips::S2);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001028
Jim Grosbach6f482002015-05-18 18:43:14 +00001029 MCSymbol *Tmp = OutContext.createTempSymbol();
Lang Hames9ff69c82015-04-24 19:11:51 +00001030 OutStreamer->EmitLabel(Tmp);
Jim Grosbach13760bd2015-05-30 01:25:56 +00001031 const MCSymbolRefExpr *E = MCSymbolRefExpr::create(Stub, OutContext);
1032 const MCSymbolRefExpr *T = MCSymbolRefExpr::create(Tmp, OutContext);
1033 const MCExpr *T_min_E = MCBinaryExpr::createSub(T, E, OutContext);
Lang Hames9ff69c82015-04-24 19:11:51 +00001034 OutStreamer->EmitELFSize(Stub, T_min_E);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001035 TS.emitDirectiveEnd(x);
Lang Hames9ff69c82015-04-24 19:11:51 +00001036 OutStreamer->PopSection();
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001037}
1038
1039void MipsAsmPrinter::EmitEndOfAsmFile(Module &M) {
1040 // Emit needed stubs
1041 //
1042 for (std::map<
1043 const char *,
1044 const llvm::Mips16HardFloatInfo::FuncSignature *>::const_iterator
1045 it = StubsNeeded.begin();
1046 it != StubsNeeded.end(); ++it) {
1047 const char *Symbol = it->first;
1048 const llvm::Mips16HardFloatInfo::FuncSignature *Signature = it->second;
1049 EmitFPCallStub(Symbol, Signature);
1050 }
Rafael Espindola2ab7ea72014-01-27 01:33:33 +00001051 // return to the text section
Lang Hames9ff69c82015-04-24 19:11:51 +00001052 OutStreamer->SwitchSection(OutContext.getObjectFileInfo()->getTextSection());
Jack Carterc1b17ed2013-01-18 21:20:38 +00001053}
1054
Akira Hatanakaf2bcad92011-07-01 01:04:43 +00001055void MipsAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
1056 raw_ostream &OS) {
1057 // TODO: implement
1058}
1059
Sasa Stankovic8c5736b2014-02-28 10:00:38 +00001060// Align all targets of indirect branches on bundle size. Used only if target
1061// is NaCl.
1062void MipsAsmPrinter::NaClAlignIndirectJumpTargets(MachineFunction &MF) {
1063 // Align all blocks that are jumped to through jump table.
1064 if (MachineJumpTableInfo *JtInfo = MF.getJumpTableInfo()) {
1065 const std::vector<MachineJumpTableEntry> &JT = JtInfo->getJumpTables();
1066 for (unsigned I = 0; I < JT.size(); ++I) {
1067 const std::vector<MachineBasicBlock*> &MBBs = JT[I].MBBs;
1068
1069 for (unsigned J = 0; J < MBBs.size(); ++J)
1070 MBBs[J]->setAlignment(MIPS_NACL_BUNDLE_ALIGN);
1071 }
1072 }
1073
1074 // If basic block address is taken, block can be target of indirect branch.
1075 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
1076 MBB != E; ++MBB) {
1077 if (MBB->hasAddressTaken())
1078 MBB->setAlignment(MIPS_NACL_BUNDLE_ALIGN);
1079 }
1080}
1081
Sasa Stankovic7b061a42014-04-30 15:06:25 +00001082bool MipsAsmPrinter::isLongBranchPseudo(int Opcode) const {
1083 return (Opcode == Mips::LONG_BRANCH_LUi
1084 || Opcode == Mips::LONG_BRANCH_ADDiu
Sasa Stankovic7b061a42014-04-30 15:06:25 +00001085 || Opcode == Mips::LONG_BRANCH_DADDiu);
1086}
1087
Bob Wilson5a495fe2009-06-23 23:59:40 +00001088// Force static initialization.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +00001089extern "C" void LLVMInitializeMipsAsmPrinter() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +00001090 RegisterAsmPrinter<MipsAsmPrinter> X(TheMipsTarget);
1091 RegisterAsmPrinter<MipsAsmPrinter> Y(TheMipselTarget);
Akira Hatanaka3d673cc2011-09-21 03:00:58 +00001092 RegisterAsmPrinter<MipsAsmPrinter> A(TheMips64Target);
1093 RegisterAsmPrinter<MipsAsmPrinter> B(TheMips64elTarget);
Daniel Dunbare8338102009-07-15 20:24:03 +00001094}