blob: 112508bccb6d5e7e28c1cab60270857ed9d2f9a6 [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"
25#include "llvm/ADT/StringExtras.h"
26#include "llvm/ADT/Twine.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000027#include "llvm/CodeGen/MachineConstantPool.h"
Bruno Cardoso Lopesbcda5e22007-07-11 23:24:41 +000028#include "llvm/CodeGen/MachineFrameInfo.h"
Jack Carterb2af5122012-07-05 23:58:21 +000029#include "llvm/CodeGen/MachineFunctionPass.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000030#include "llvm/CodeGen/MachineInstr.h"
Sasa Stankovic8c5736b2014-02-28 10:00:38 +000031#include "llvm/CodeGen/MachineJumpTableInfo.h"
Akira Hatanaka2fcc1cf2011-08-12 21:30:06 +000032#include "llvm/CodeGen/MachineMemOperand.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000033#include "llvm/IR/BasicBlock.h"
34#include "llvm/IR/DataLayout.h"
35#include "llvm/IR/InlineAsm.h"
36#include "llvm/IR/Instructions.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000037#include "llvm/IR/Mangler.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000038#include "llvm/MC/MCAsmInfo.h"
Rafael Espindola2ab7ea72014-01-27 01:33:33 +000039#include "llvm/MC/MCContext.h"
Rafael Espindolaac4ad252013-10-05 16:42:21 +000040#include "llvm/MC/MCELFStreamer.h"
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000041#include "llvm/MC/MCExpr.h"
Akira Hatanaka9c6028f2011-07-07 23:56:50 +000042#include "llvm/MC/MCInst.h"
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000043#include "llvm/MC/MCSection.h"
Rafael Espindola2ab7ea72014-01-27 01:33:33 +000044#include "llvm/MC/MCSectionELF.h"
Chris Lattner4cd44982009-09-13 17:14:04 +000045#include "llvm/MC/MCSymbol.h"
Jack Carterab3cb422013-02-19 22:04:37 +000046#include "llvm/Support/ELF.h"
Jack Carterb2af5122012-07-05 23:58:21 +000047#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000048#include "llvm/Support/raw_ostream.h"
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +000049#include "llvm/Target/TargetLoweringObjectFile.h"
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +000050#include "llvm/Target/TargetOptions.h"
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000051#include <string>
Akira Hatanakaf2bcad92011-07-01 01:04:43 +000052
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000053using namespace llvm;
54
Chandler Carruth84e68b22014-04-22 02:41:26 +000055#define DEBUG_TYPE "mips-asm-printer"
56
Toma Tabacua23f13c2014-12-17 10:56:16 +000057MipsTargetStreamer &MipsAsmPrinter::getTargetStreamer() const {
Lang Hames9ff69c82015-04-24 19:11:51 +000058 return static_cast<MipsTargetStreamer &>(*OutStreamer->getTargetStreamer());
Rafael Espindolaa17151a2013-10-08 13:08:17 +000059}
60
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000061bool MipsAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Eric Christopher3ee30d02015-02-20 08:39:06 +000062 Subtarget = &MF.getSubtarget<MipsSubtarget>();
Eric Christopher8ef7a6a2014-07-18 00:08:53 +000063
Reed Kotler1595f362013-04-09 19:46:01 +000064 // Initialize TargetLoweringObjectFile.
Eric Christopher4e7d1e72014-07-18 23:41:32 +000065 const_cast<TargetLoweringObjectFile &>(getObjFileLowering())
Reed Kotler1595f362013-04-09 19:46:01 +000066 .Initialize(OutContext, TM);
Eric Christopher4e7d1e72014-07-18 23:41:32 +000067
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000068 MipsFI = MF.getInfo<MipsFunctionInfo>();
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000069 if (Subtarget->inMips16Mode())
70 for (std::map<
71 const char *,
72 const llvm::Mips16HardFloatInfo::FuncSignature *>::const_iterator
73 it = MipsFI->StubsNeeded.begin();
74 it != MipsFI->StubsNeeded.end(); ++it) {
75 const char *Symbol = it->first;
76 const llvm::Mips16HardFloatInfo::FuncSignature *Signature = it->second;
77 if (StubsNeeded.find(Symbol) == StubsNeeded.end())
78 StubsNeeded[Symbol] = Signature;
79 }
Reed Kotler91ae9822013-10-27 21:57:36 +000080 MCP = MF.getConstantPool();
Sasa Stankovic8c5736b2014-02-28 10:00:38 +000081
82 // In NaCl, all indirect jump targets must be aligned to bundle size.
83 if (Subtarget->isTargetNaCl())
84 NaClAlignIndirectJumpTargets(MF);
85
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000086 AsmPrinter::runOnMachineFunction(MF);
87 return true;
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +000088}
89
Akira Hatanaka42a35242012-09-27 01:59:07 +000090bool MipsAsmPrinter::lowerOperand(const MachineOperand &MO, MCOperand &MCOp) {
91 MCOp = MCInstLowering.LowerOperand(MO);
92 return MCOp.isValid();
93}
94
95#include "MipsGenMCPseudoLowering.inc"
96
Daniel Sandersf5a5fbd2014-07-09 10:21:59 +000097// Lower PseudoReturn/PseudoIndirectBranch/PseudoIndirectBranch64 to JR, JR_MM,
98// JALR, or JALR64 as appropriate for the target
99void MipsAsmPrinter::emitPseudoIndirectBranch(MCStreamer &OutStreamer,
100 const MachineInstr *MI) {
Daniel Sanders338513b2014-07-09 10:16:07 +0000101 bool HasLinkReg = false;
102 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)
110 TmpInst0.setOpcode(Mips::JALR);
111 HasLinkReg = true;
112 } else if (Subtarget->inMicroMipsMode())
113 // microMIPS should use (JR_MM $rs)
114 TmpInst0.setOpcode(Mips::JR_MM);
115 else {
116 // Everything else should use (JR $rs)
117 TmpInst0.setOpcode(Mips::JR);
118 }
119
120 MCOperand MCOp;
121
122 if (HasLinkReg) {
123 unsigned ZeroReg = Subtarget->isGP64bit() ? Mips::ZERO_64 : Mips::ZERO;
Jim Grosbache9119e42015-05-13 18:37:00 +0000124 TmpInst0.addOperand(MCOperand::createReg(ZeroReg));
Daniel Sanders338513b2014-07-09 10:16:07 +0000125 }
126
127 lowerOperand(MI->getOperand(0), MCOp);
128 TmpInst0.addOperand(MCOp);
129
130 EmitToStreamer(OutStreamer, TmpInst0);
131}
132
Akira Hatanakaddd12652011-07-07 20:10:52 +0000133void MipsAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000134 MipsTargetStreamer &TS = getTargetStreamer();
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000135 TS.forbidModuleDirective();
Daniel Sandersc7dbc632014-07-08 10:11:38 +0000136
Akira Hatanakaddd12652011-07-07 20:10:52 +0000137 if (MI->isDebugValue()) {
Bruno Cardoso Lopescd1d4472011-12-30 21:09:41 +0000138 SmallString<128> Str;
139 raw_svector_ostream OS(Str);
140
Akira Hatanakaddd12652011-07-07 20:10:52 +0000141 PrintDebugValueComment(MI, OS);
142 return;
143 }
144
Reed Kotler91ae9822013-10-27 21:57:36 +0000145 // If we just ended a constant pool, mark it as such.
146 if (InConstantPool && MI->getOpcode() != Mips::CONSTPOOL_ENTRY) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000147 OutStreamer->EmitDataRegion(MCDR_DataRegionEnd);
Reed Kotler91ae9822013-10-27 21:57:36 +0000148 InConstantPool = false;
149 }
150 if (MI->getOpcode() == Mips::CONSTPOOL_ENTRY) {
151 // CONSTPOOL_ENTRY - This instruction represents a floating
152 //constant pool in the function. The first operand is the ID#
153 // for this instruction, the second is the index into the
154 // MachineConstantPool that this is, the third is the size in
155 // bytes of this constant pool entry.
156 // The required alignment is specified on the basic block holding this MI.
157 //
158 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
159 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
160
161 // If this is the first entry of the pool, mark it.
162 if (!InConstantPool) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000163 OutStreamer->EmitDataRegion(MCDR_DataRegion);
Reed Kotler91ae9822013-10-27 21:57:36 +0000164 InConstantPool = true;
165 }
166
Lang Hames9ff69c82015-04-24 19:11:51 +0000167 OutStreamer->EmitLabel(GetCPISymbol(LabelId));
Reed Kotler91ae9822013-10-27 21:57:36 +0000168
169 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
170 if (MCPE.isMachineConstantPoolEntry())
171 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
172 else
173 EmitGlobalConstant(MCPE.Val.ConstVal);
174 return;
175 }
176
Rafael Espindola14d02fe2014-01-25 15:06:56 +0000177
Akira Hatanaka5ac78682012-06-13 23:25:52 +0000178 MachineBasicBlock::const_instr_iterator I = MI;
179 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
180
181 do {
Akira Hatanaka556135d2013-02-06 21:50:15 +0000182 // Do any auto-generated pseudo lowerings.
Lang Hames9ff69c82015-04-24 19:11:51 +0000183 if (emitPseudoExpansionLowering(*OutStreamer, &*I))
Akira Hatanaka556135d2013-02-06 21:50:15 +0000184 continue;
Jack Carterc20a21b2012-08-28 19:07:39 +0000185
Daniel Sanders338513b2014-07-09 10:16:07 +0000186 if (I->getOpcode() == Mips::PseudoReturn ||
Daniel Sandersf5a5fbd2014-07-09 10:21:59 +0000187 I->getOpcode() == Mips::PseudoReturn64 ||
188 I->getOpcode() == Mips::PseudoIndirectBranch ||
189 I->getOpcode() == Mips::PseudoIndirectBranch64) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000190 emitPseudoIndirectBranch(*OutStreamer, &*I);
Daniel Sanders338513b2014-07-09 10:16:07 +0000191 continue;
192 }
193
Reed Kotler76c9bcd2013-02-15 21:05:58 +0000194 // The inMips16Mode() test is not permanent.
195 // Some instructions are marked as pseudo right now which
196 // would make the test fail for the wrong reason but
197 // that will be fixed soon. We need this here because we are
198 // removing another test for this situation downstream in the
199 // callchain.
200 //
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000201 if (I->isPseudo() && !Subtarget->inMips16Mode()
202 && !isLongBranchPseudo(I->getOpcode()))
Reed Kotler76c9bcd2013-02-15 21:05:58 +0000203 llvm_unreachable("Pseudo opcode found in EmitInstruction()");
204
Akira Hatanaka556135d2013-02-06 21:50:15 +0000205 MCInst TmpInst0;
206 MCInstLowering.Lower(I, TmpInst0);
Lang Hames9ff69c82015-04-24 19:11:51 +0000207 EmitToStreamer(*OutStreamer, TmpInst0);
Akira Hatanaka556135d2013-02-06 21:50:15 +0000208 } while ((++I != E) && I->isInsideBundle()); // Delay slot check
Akira Hatanakaddd12652011-07-07 20:10:52 +0000209}
210
Akira Hatanakae2489122011-04-15 21:51:11 +0000211//===----------------------------------------------------------------------===//
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000212//
213// Mips Asm Directives
214//
215// -- Frame directive "frame Stackpointer, Stacksize, RARegister"
216// Describe the stack frame.
217//
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000218// -- Mask directives "(f)mask bitmask, offset"
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000219// Tells the assembler which registers are saved and where.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000220// bitmask - contain a little endian bitset indicating which registers are
221// saved on function prologue (e.g. with a 0x80000000 mask, the
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000222// assembler knows the register 31 (RA) is saved at prologue.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000223// offset - the position before stack pointer subtraction indicating where
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000224// the first saved register on prologue is located. (e.g. with a
225//
226// Consider the following function prologue:
227//
Bill Wendling97925ec2008-02-27 06:33:05 +0000228// .frame $fp,48,$ra
229// .mask 0xc0000000,-8
230// addiu $sp, $sp, -48
231// sw $ra, 40($sp)
232// sw $fp, 36($sp)
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000233//
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000234// With a 0xc0000000 mask, the assembler knows the register 31 (RA) and
235// 30 (FP) are saved at prologue. As the save order on prologue is from
236// left to right, RA is saved first. A -8 offset means that after the
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000237// stack pointer subtration, the first register in the mask (RA) will be
238// saved at address 48-8=40.
239//
Akira Hatanakae2489122011-04-15 21:51:11 +0000240//===----------------------------------------------------------------------===//
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000241
Akira Hatanakae2489122011-04-15 21:51:11 +0000242//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000243// Mask directives
Akira Hatanakae2489122011-04-15 21:51:11 +0000244//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000245
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000246// Create a bitmask with all callee saved registers for CPU or Floating Point
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000247// registers. For CPU registers consider RA, GP and FP for saving if necessary.
Rafael Espindola25fa2912014-01-27 04:33:11 +0000248void MipsAsmPrinter::printSavedRegsBitmask() {
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000249 // CPU and FPU Saved Registers Bitmasks
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000250 unsigned CPUBitmask = 0, FPUBitmask = 0;
251 int CPUTopSavedRegOff, FPUTopSavedRegOff;
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000252
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000253 // Set the CPU and FPU Bitmasks
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000254 const MachineFrameInfo *MFI = MF->getFrameInfo();
Eric Christophercba722f2015-03-21 03:13:07 +0000255 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000256 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000257 // size of stack area to which FP callee-saved regs are saved.
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000258 unsigned CPURegSize = Mips::GPR32RegClass.getSize();
Craig Topperc7242e02012-04-20 07:30:17 +0000259 unsigned FGR32RegSize = Mips::FGR32RegClass.getSize();
260 unsigned AFGR64RegSize = Mips::AFGR64RegClass.getSize();
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000261 bool HasAFGR64Reg = false;
262 unsigned CSFPRegsSize = 0;
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000263
Toma Tabacube218922015-04-09 10:54:16 +0000264 for (const auto &I : CSI) {
265 unsigned Reg = I.getReg();
Eric Christophercba722f2015-03-21 03:13:07 +0000266 unsigned RegNum = TRI->getEncodingValue(Reg);
Toma Tabacube218922015-04-09 10:54:16 +0000267
268 // If it's a floating point register, set the FPU Bitmask.
269 // If it's a general purpose register, set the CPU Bitmask.
270 if (Mips::FGR32RegClass.contains(Reg)) {
271 FPUBitmask |= (1 << RegNum);
272 CSFPRegsSize += FGR32RegSize;
273 } else if (Mips::AFGR64RegClass.contains(Reg)) {
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000274 FPUBitmask |= (3 << RegNum);
275 CSFPRegsSize += AFGR64RegSize;
276 HasAFGR64Reg = true;
Toma Tabacube218922015-04-09 10:54:16 +0000277 } else if (Mips::GPR32RegClass.contains(Reg))
278 CPUBitmask |= (1 << RegNum);
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000279 }
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000280
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000281 // FP Regs are saved right below where the virtual frame pointer points to.
282 FPUTopSavedRegOff = FPUBitmask ?
283 (HasAFGR64Reg ? -AFGR64RegSize : -FGR32RegSize) : 0;
284
285 // CPU Regs are saved below FP Regs.
286 CPUTopSavedRegOff = CPUBitmask ? -CSFPRegsSize - CPURegSize : 0;
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000287
Rafael Espindola25fa2912014-01-27 04:33:11 +0000288 MipsTargetStreamer &TS = getTargetStreamer();
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000289 // Print CPUBitmask
Rafael Espindola25fa2912014-01-27 04:33:11 +0000290 TS.emitMask(CPUBitmask, CPUTopSavedRegOff);
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000291
292 // Print FPUBitmask
Rafael Espindola25fa2912014-01-27 04:33:11 +0000293 TS.emitFMask(FPUBitmask, FPUTopSavedRegOff);
Bruno Cardoso Lopesbcda5e22007-07-11 23:24:41 +0000294}
295
Akira Hatanakae2489122011-04-15 21:51:11 +0000296//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000297// Frame and Set directives
Akira Hatanakae2489122011-04-15 21:51:11 +0000298//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000299
300/// Frame Directive
Chris Lattner5e596182010-04-04 07:05:53 +0000301void MipsAsmPrinter::emitFrameDirective() {
Eric Christophercba722f2015-03-21 03:13:07 +0000302 const TargetRegisterInfo &RI = *MF->getSubtarget().getRegisterInfo();
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000303
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000304 unsigned stackReg = RI.getFrameRegister(*MF);
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000305 unsigned returnReg = RI.getRARegister();
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000306 unsigned stackSize = MF->getFrameInfo()->getStackSize();
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000307
Rafael Espindola054234f2014-01-27 03:53:56 +0000308 getTargetStreamer().emitFrame(stackReg, stackSize, returnReg);
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000309}
310
311/// Emit Set directives.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000312const char *MipsAsmPrinter::getCurrentABIString() const {
Eric Christophera5762812015-01-26 17:33:46 +0000313 switch (static_cast<MipsTargetMachine &>(TM).getABI().GetEnumValue()) {
Daniel Sanderse2e25da2014-10-24 16:15:27 +0000314 case MipsABIInfo::ABI::O32: return "abi32";
315 case MipsABIInfo::ABI::N32: return "abiN32";
316 case MipsABIInfo::ABI::N64: return "abi64";
317 case MipsABIInfo::ABI::EABI: return "eabi32"; // TODO: handle eabi64
Dmitri Gribenkoca1e27b2012-09-10 21:26:47 +0000318 default: llvm_unreachable("Unknown Mips ABI");
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000319 }
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000320}
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000321
Chris Lattner5d9fb4b2010-01-27 23:23:58 +0000322void MipsAsmPrinter::EmitFunctionEntryLabel() {
Rafael Espindola6633d572014-01-14 18:57:12 +0000323 MipsTargetStreamer &TS = getTargetStreamer();
Sasa Stankovic8c5736b2014-02-28 10:00:38 +0000324
325 // NaCl sandboxing requires that indirect call instructions are masked.
326 // This means that function entry points should be bundle-aligned.
327 if (Subtarget->isTargetNaCl())
328 EmitAlignment(std::max(MF->getAlignment(), MIPS_NACL_BUNDLE_ALIGN));
329
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000330 if (Subtarget->inMicroMipsMode())
Rafael Espindola6633d572014-01-14 18:57:12 +0000331 TS.emitDirectiveSetMicroMips();
Matheus Almeidadc7e48e2014-04-16 11:46:59 +0000332 else
333 TS.emitDirectiveSetNoMicroMips();
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000334
Rafael Espindola6633d572014-01-14 18:57:12 +0000335 if (Subtarget->inMips16Mode())
336 TS.emitDirectiveSetMips16();
337 else
338 TS.emitDirectiveSetNoMips16();
Jack Carterab3cb422013-02-19 22:04:37 +0000339
Rafael Espindola6633d572014-01-14 18:57:12 +0000340 TS.emitDirectiveEnt(*CurrentFnSym);
Lang Hames9ff69c82015-04-24 19:11:51 +0000341 OutStreamer->EmitLabel(CurrentFnSym);
Chris Lattner5d9fb4b2010-01-27 23:23:58 +0000342}
343
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000344/// EmitFunctionBodyStart - Targets can override this to emit stuff before
345/// the first basic block in the function.
346void MipsAsmPrinter::EmitFunctionBodyStart() {
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000347 MipsTargetStreamer &TS = getTargetStreamer();
348
Rafael Espindola7d78b2a2013-10-29 16:24:21 +0000349 MCInstLowering.Initialize(&MF->getContext());
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +0000350
Duncan P. N. Exon Smith2e753142015-02-14 02:37:48 +0000351 bool IsNakedFunction = MF->getFunction()->hasFnAttribute(Attribute::Naked);
Reed Kotler0f2b10e2013-05-03 23:17:24 +0000352 if (!IsNakedFunction)
353 emitFrameDirective();
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000354
Rafael Espindola25fa2912014-01-27 04:33:11 +0000355 if (!IsNakedFunction)
356 printSavedRegsBitmask();
357
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000358 if (!Subtarget->inMips16Mode()) {
359 TS.emitDirectiveSetNoReorder();
360 TS.emitDirectiveSetNoMacro();
361 TS.emitDirectiveSetNoAt();
Akira Hatanaka8f3573032012-05-12 00:48:43 +0000362 }
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000363}
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000364
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000365/// EmitFunctionBodyEnd - Targets can override this to emit stuff after
366/// the last basic block in the function.
367void MipsAsmPrinter::EmitFunctionBodyEnd() {
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000368 MipsTargetStreamer &TS = getTargetStreamer();
369
Chris Lattnerfd97a332010-01-28 01:48:52 +0000370 // There are instruction for this macros, but they must
371 // always be at the function end, and we can't emit and
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000372 // break with BB logic.
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000373 if (!Subtarget->inMips16Mode()) {
374 TS.emitDirectiveSetAt();
375 TS.emitDirectiveSetMacro();
376 TS.emitDirectiveSetReorder();
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000377 }
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000378 TS.emitDirectiveEnd(CurrentFnSym->getName());
Reed Kotler91ae9822013-10-27 21:57:36 +0000379 // Make sure to terminate any constant pools that were at the end
380 // of the function.
381 if (!InConstantPool)
382 return;
383 InConstantPool = false;
Lang Hames9ff69c82015-04-24 19:11:51 +0000384 OutStreamer->EmitDataRegion(MCDR_DataRegionEnd);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000385}
386
Vasileios Kalintiris42544d62015-05-08 09:10:15 +0000387void MipsAsmPrinter::EmitBasicBlockEnd(const MachineBasicBlock &MBB) {
388 MipsTargetStreamer &TS = getTargetStreamer();
389 if (MBB.size() == 0)
390 TS.emitDirectiveInsn();
391}
392
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000393/// isBlockOnlyReachableByFallthough - Return true if the basic block has
394/// exactly one predecessor and the control transfer mechanism between
395/// the predecessor and this block is a fall-through.
Akira Hatanakae2489122011-04-15 21:51:11 +0000396bool MipsAsmPrinter::isBlockOnlyReachableByFallthrough(const MachineBasicBlock*
397 MBB) const {
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000398 // The predecessor has to be immediately before this block.
399 const MachineBasicBlock *Pred = *MBB->pred_begin();
400
401 // If the predecessor is a switch statement, assume a jump table
402 // implementation, so it is not a fall through.
403 if (const BasicBlock *bb = Pred->getBasicBlock())
404 if (isa<SwitchInst>(bb->getTerminator()))
405 return false;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000406
Akira Hatanakae625ba42011-04-01 18:57:38 +0000407 // If this is a landing pad, it isn't a fall through. If it has no preds,
408 // then nothing falls through to it.
409 if (MBB->isLandingPad() || MBB->pred_empty())
410 return false;
411
412 // If there isn't exactly one predecessor, it can't be a fall through.
413 MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI;
414 ++PI2;
Jia Liuf54f60f2012-02-28 07:46:26 +0000415
Akira Hatanakae625ba42011-04-01 18:57:38 +0000416 if (PI2 != MBB->pred_end())
Jia Liuf54f60f2012-02-28 07:46:26 +0000417 return false;
Akira Hatanakae625ba42011-04-01 18:57:38 +0000418
419 // The predecessor has to be immediately before this block.
420 if (!Pred->isLayoutSuccessor(MBB))
421 return false;
Jia Liuf54f60f2012-02-28 07:46:26 +0000422
Akira Hatanakae625ba42011-04-01 18:57:38 +0000423 // If the block is completely empty, then it definitely does fall through.
424 if (Pred->empty())
425 return true;
Jia Liuf54f60f2012-02-28 07:46:26 +0000426
Akira Hatanakae625ba42011-04-01 18:57:38 +0000427 // Otherwise, check the last instruction.
428 // Check if the last terminator is an unconditional branch.
429 MachineBasicBlock::const_iterator I = Pred->end();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000430 while (I != Pred->begin() && !(--I)->isTerminator()) ;
Akira Hatanakae625ba42011-04-01 18:57:38 +0000431
Evan Cheng7f8e5632011-12-07 07:15:52 +0000432 return !I->isBarrier();
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000433}
434
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000435// Print out an operand for an inline asm expression.
Eric Christophered51b9e2012-05-10 21:48:22 +0000436bool MipsAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000437 unsigned AsmVariant, const char *ExtraCode,
Chris Lattner3bb09762010-04-04 05:29:35 +0000438 raw_ostream &O) {
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000439 // Does this asm operand have a single letter operand modifier?
Eric Christophered51b9e2012-05-10 21:48:22 +0000440 if (ExtraCode && ExtraCode[0]) {
441 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000442
Eric Christophered51b9e2012-05-10 21:48:22 +0000443 const MachineOperand &MO = MI->getOperand(OpNum);
444 switch (ExtraCode[0]) {
Eric Christopherbc5d2492012-05-19 00:51:56 +0000445 default:
Jack Carterb2fd5f62012-06-21 17:14:46 +0000446 // See if this is a generic print operand
447 return AsmPrinter::PrintAsmOperand(MI,OpNum,AsmVariant,ExtraCode,O);
Eric Christopherbc5d2492012-05-19 00:51:56 +0000448 case 'X': // hex const int
449 if ((MO.getType()) != MachineOperand::MO_Immediate)
450 return true;
451 O << "0x" << StringRef(utohexstr(MO.getImm())).lower();
452 return false;
453 case 'x': // hex const int (low 16 bits)
454 if ((MO.getType()) != MachineOperand::MO_Immediate)
455 return true;
456 O << "0x" << StringRef(utohexstr(MO.getImm() & 0xffff)).lower();
457 return false;
458 case 'd': // decimal const int
459 if ((MO.getType()) != MachineOperand::MO_Immediate)
460 return true;
461 O << MO.getImm();
462 return false;
Eric Christopherf481ab32012-05-30 19:05:19 +0000463 case 'm': // decimal const int minus 1
464 if ((MO.getType()) != MachineOperand::MO_Immediate)
465 return true;
466 O << MO.getImm() - 1;
467 return false;
Jack Carter27747b52012-06-28 20:46:26 +0000468 case 'z': {
469 // $0 if zero, regular printing otherwise
Toma Tabacu27cab752014-11-06 14:25:42 +0000470 if (MO.getType() == MachineOperand::MO_Immediate && MO.getImm() == 0) {
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000471 O << "$0";
Toma Tabacu27cab752014-11-06 14:25:42 +0000472 return false;
473 }
474 // If not, call printOperand as normal.
475 break;
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000476 }
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000477 case 'D': // Second part of a double word register operand
478 case 'L': // Low order register of a double word register operand
Jack Cartera62ba822012-07-18 06:41:36 +0000479 case 'M': // High order register of a double word register operand
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000480 {
Jack Carterb2af5122012-07-05 23:58:21 +0000481 if (OpNum == 0)
482 return true;
483 const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
484 if (!FlagsOP.isImm())
485 return true;
486 unsigned Flags = FlagsOP.getImm();
487 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Jack Carter2ab73b12012-07-06 02:44:22 +0000488 // Number of registers represented by this operand. We are looking
489 // for 2 for 32 bit mode and 1 for 64 bit mode.
Jack Carterb2af5122012-07-05 23:58:21 +0000490 if (NumVals != 2) {
Jack Carter2ab73b12012-07-06 02:44:22 +0000491 if (Subtarget->isGP64bit() && NumVals == 1 && MO.isReg()) {
Jack Carterb2af5122012-07-05 23:58:21 +0000492 unsigned Reg = MO.getReg();
493 O << '$' << MipsInstPrinter::getRegisterName(Reg);
494 return false;
495 }
496 return true;
497 }
Jack Carter42ebf982012-07-11 21:41:49 +0000498
499 unsigned RegOp = OpNum;
500 if (!Subtarget->isGP64bit()){
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000501 // Endianess reverses which register holds the high or low value
Jack Cartera62ba822012-07-18 06:41:36 +0000502 // between M and L.
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000503 switch(ExtraCode[0]) {
Jack Cartera62ba822012-07-18 06:41:36 +0000504 case 'M':
505 RegOp = (Subtarget->isLittle()) ? OpNum + 1 : OpNum;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000506 break;
507 case 'L':
Jack Cartera62ba822012-07-18 06:41:36 +0000508 RegOp = (Subtarget->isLittle()) ? OpNum : OpNum + 1;
509 break;
510 case 'D': // Always the second part
511 RegOp = OpNum + 1;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000512 }
513 if (RegOp >= MI->getNumOperands())
514 return true;
515 const MachineOperand &MO = MI->getOperand(RegOp);
516 if (!MO.isReg())
517 return true;
518 unsigned Reg = MO.getReg();
519 O << '$' << MipsInstPrinter::getRegisterName(Reg);
520 return false;
Jack Carterb2af5122012-07-05 23:58:21 +0000521 }
Eric Christophered51b9e2012-05-10 21:48:22 +0000522 }
Daniel Sanders8b59af12013-11-12 12:56:01 +0000523 case 'w':
524 // Print MSA registers for the 'f' constraint
525 // In LLVM, the 'w' modifier doesn't need to do anything.
526 // We can just call printOperand as normal.
527 break;
Jack Carter2ab73b12012-07-06 02:44:22 +0000528 }
529 }
Eric Christophered51b9e2012-05-10 21:48:22 +0000530
531 printOperand(MI, OpNum, O);
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000532 return false;
533}
534
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000535bool MipsAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
536 unsigned OpNum, unsigned AsmVariant,
537 const char *ExtraCode,
538 raw_ostream &O) {
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000539 assert(OpNum + 1 < MI->getNumOperands() && "Insufficient operands");
540 const MachineOperand &BaseMO = MI->getOperand(OpNum);
541 const MachineOperand &OffsetMO = MI->getOperand(OpNum + 1);
542 assert(BaseMO.isReg() && "Unexpected base pointer for inline asm memory operand.");
543 assert(OffsetMO.isImm() && "Unexpected offset for inline asm memory operand.");
544 int Offset = OffsetMO.getImm();
545
Jack Carterb04e3572013-04-09 23:19:50 +0000546 // Currently we are expecting either no ExtraCode or 'D'
547 if (ExtraCode) {
548 if (ExtraCode[0] == 'D')
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000549 Offset += 4;
Jack Carterb04e3572013-04-09 23:19:50 +0000550 else
551 return true; // Unknown modifier.
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000552 // FIXME: M = high order bits
553 // FIXME: L = low order bits
Jack Carterb04e3572013-04-09 23:19:50 +0000554 }
Jia Liuf54f60f2012-02-28 07:46:26 +0000555
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000556 O << Offset << "($" << MipsInstPrinter::getRegisterName(BaseMO.getReg()) << ")";
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000557
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000558 return false;
559}
560
Chris Lattner76c564b2010-04-04 04:47:45 +0000561void MipsAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
562 raw_ostream &O) {
Eric Christopher8b770652015-01-26 19:03:15 +0000563 const DataLayout *DL = TM.getDataLayout();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000564 const MachineOperand &MO = MI->getOperand(opNum);
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +0000565 bool closeP = false;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000566
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000567 if (MO.getTargetFlags())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000568 closeP = true;
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000569
570 switch(MO.getTargetFlags()) {
571 case MipsII::MO_GPREL: O << "%gp_rel("; break;
572 case MipsII::MO_GOT_CALL: O << "%call16("; break;
Akira Hatanaka56d9ef52011-04-01 21:41:06 +0000573 case MipsII::MO_GOT: O << "%got("; break;
574 case MipsII::MO_ABS_HI: O << "%hi("; break;
575 case MipsII::MO_ABS_LO: O << "%lo("; break;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +0000576 case MipsII::MO_TLSGD: O << "%tlsgd("; break;
577 case MipsII::MO_GOTTPREL: O << "%gottprel("; break;
578 case MipsII::MO_TPREL_HI: O << "%tprel_hi("; break;
579 case MipsII::MO_TPREL_LO: O << "%tprel_lo("; break;
Akira Hatanaka25ce3642011-09-22 03:09:07 +0000580 case MipsII::MO_GPOFF_HI: O << "%hi(%neg(%gp_rel("; break;
581 case MipsII::MO_GPOFF_LO: O << "%lo(%neg(%gp_rel("; break;
582 case MipsII::MO_GOT_DISP: O << "%got_disp("; break;
583 case MipsII::MO_GOT_PAGE: O << "%got_page("; break;
584 case MipsII::MO_GOT_OFST: O << "%got_ofst("; break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000585 }
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000586
Chris Lattnereb2cc682009-09-13 20:31:40 +0000587 switch (MO.getType()) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000588 case MachineOperand::MO_Register:
Akira Hatanaka9c6028f2011-07-07 23:56:50 +0000589 O << '$'
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000590 << StringRef(MipsInstPrinter::getRegisterName(MO.getReg())).lower();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000591 break;
592
593 case MachineOperand::MO_Immediate:
Akira Hatanaka2db176c2011-05-24 21:22:21 +0000594 O << MO.getImm();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000595 break;
596
597 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner29bdac42010-03-13 21:04:28 +0000598 O << *MO.getMBB()->getSymbol();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000599 return;
600
601 case MachineOperand::MO_GlobalAddress:
Rafael Espindola79858aa2013-10-29 17:07:16 +0000602 O << *getSymbol(MO.getGlobal());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000603 break;
604
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000605 case MachineOperand::MO_BlockAddress: {
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000606 MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000607 O << BA->getName();
608 break;
609 }
610
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000611 case MachineOperand::MO_ConstantPoolIndex:
Rafael Espindola58873562014-01-03 19:21:54 +0000612 O << DL->getPrivateGlobalPrefix() << "CPI"
Chris Lattnera5bb3702007-12-30 23:10:15 +0000613 << getFunctionNumber() << "_" << MO.getIndex();
Bruno Cardoso Lopes4713b282009-11-19 06:06:13 +0000614 if (MO.getOffset())
615 O << "+" << MO.getOffset();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000616 break;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000617
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000618 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000619 llvm_unreachable("<unknown operand type>");
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000620 }
621
622 if (closeP) O << ")";
623}
624
Chris Lattner76c564b2010-04-04 04:47:45 +0000625void MipsAsmPrinter::printUnsignedImm(const MachineInstr *MI, int opNum,
626 raw_ostream &O) {
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000627 const MachineOperand &MO = MI->getOperand(opNum);
Devang Patel12f68552010-04-27 22:24:37 +0000628 if (MO.isImm())
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000629 O << (unsigned short int)MO.getImm();
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000630 else
Chris Lattner76c564b2010-04-04 04:47:45 +0000631 printOperand(MI, opNum, O);
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000632}
633
Daniel Sanders3f6eb542013-11-12 10:45:18 +0000634void MipsAsmPrinter::printUnsignedImm8(const MachineInstr *MI, int opNum,
635 raw_ostream &O) {
636 const MachineOperand &MO = MI->getOperand(opNum);
637 if (MO.isImm())
638 O << (unsigned short int)(unsigned char)MO.getImm();
639 else
640 printOperand(MI, opNum, O);
641}
642
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000643void MipsAsmPrinter::
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000644printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O) {
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000645 // Load/Store memory operands -- imm($reg)
646 // If PIC target the target is loaded as the
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +0000647 // pattern lw $25,%call16($28)
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000648
649 // opNum can be invalid if instruction has reglist as operand.
650 // MemOperand is always last operand of instruction (base + offset).
651 switch (MI->getOpcode()) {
652 default:
653 break;
654 case Mips::SWM32_MM:
655 case Mips::LWM32_MM:
656 opNum = MI->getNumOperands() - 2;
657 break;
658 }
659
Chris Lattner76c564b2010-04-04 04:47:45 +0000660 printOperand(MI, opNum+1, O);
Akira Hatanaka2e766ed2011-07-07 18:57:00 +0000661 O << "(";
662 printOperand(MI, opNum, O);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000663 O << ")";
664}
665
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000666void MipsAsmPrinter::
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000667printMemOperandEA(const MachineInstr *MI, int opNum, raw_ostream &O) {
668 // when using stack locations for not load/store instructions
669 // print the same way as all normal 3 operand instructions.
670 printOperand(MI, opNum, O);
671 O << ", ";
672 printOperand(MI, opNum+1, O);
673 return;
674}
675
676void MipsAsmPrinter::
Chris Lattner76c564b2010-04-04 04:47:45 +0000677printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
678 const char *Modifier) {
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000679 const MachineOperand &MO = MI->getOperand(opNum);
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000680 O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000681}
682
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000683void MipsAsmPrinter::
684printRegisterList(const MachineInstr *MI, int opNum, raw_ostream &O) {
685 for (int i = opNum, e = MI->getNumOperands(); i != e; ++i) {
686 if (i != opNum) O << ", ";
687 printOperand(MI, i, O);
688 }
689}
690
Bob Wilsonb633d7a2009-09-30 22:06:26 +0000691void MipsAsmPrinter::EmitStartOfAsmFile(Module &M) {
Eric Christopher8af49b32015-02-18 01:01:57 +0000692
693 // Compute MIPS architecture attributes based on the default subtarget
694 // that we'd have constructed. Module level directives aren't LTO
695 // clean anyhow.
696 // FIXME: For ifunc related functions we could iterate over and look
697 // for a feature string that doesn't match the default one.
698 StringRef TT = TM.getTargetTriple();
699 StringRef CPU =
700 MIPS_MC::selectMipsCPU(TM.getTargetTriple(), TM.getTargetCPU());
701 StringRef FS = TM.getTargetFeatureString();
702 const MipsTargetMachine &MTM = static_cast<const MipsTargetMachine &>(TM);
703 const MipsSubtarget STI(TT, CPU, FS, MTM.isLittleEndian(), MTM);
704
705 bool IsABICalls = STI.isABICalls();
706 const MipsABIInfo &ABI = MTM.getABI();
Daniel Sanders16fa1db2014-04-16 13:58:57 +0000707 if (IsABICalls) {
708 getTargetStreamer().emitDirectiveAbiCalls();
Eric Christopher8ef7a6a2014-07-18 00:08:53 +0000709 Reloc::Model RM = TM.getRelocationModel();
Daniel Sanders16fa1db2014-04-16 13:58:57 +0000710 // FIXME: This condition should be a lot more complicated that it is here.
711 // Ideally it should test for properties of the ABI and not the ABI
712 // itself.
713 // For the moment, I'm only correcting enough to make MIPS-IV work.
Eric Christopherd86af632015-01-29 23:27:45 +0000714 if (RM == Reloc::Static && !ABI.IsN64())
Daniel Sanders16fa1db2014-04-16 13:58:57 +0000715 getTargetStreamer().emitDirectiveOptionPic0();
716 }
Jack Carterf9f753c2013-06-18 19:47:15 +0000717
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000718 // Tell the assembler which ABI we are using
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000719 std::string SectionName = std::string(".mdebug.") + getCurrentABIString();
Lang Hames9ff69c82015-04-24 19:11:51 +0000720 OutStreamer->SwitchSection(
Rafael Espindolaba31e272015-01-29 17:33:21 +0000721 OutContext.getELFSection(SectionName, ELF::SHT_PROGBITS, 0));
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000722
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000723 // NaN: At the moment we only support:
724 // 1. .nan legacy (default)
725 // 2. .nan 2008
Eric Christopher8af49b32015-02-18 01:01:57 +0000726 STI.isNaN2008() ? getTargetStreamer().emitDirectiveNaN2008()
727 : getTargetStreamer().emitDirectiveNaNLegacy();
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000728
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000729 // TODO: handle O64 ABI
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000730
Eric Christopherd86af632015-01-29 23:27:45 +0000731 if (ABI.IsEABI()) {
Eric Christopher8af49b32015-02-18 01:01:57 +0000732 if (STI.isGP32bit())
Lang Hames9ff69c82015-04-24 19:11:51 +0000733 OutStreamer->SwitchSection(OutContext.getELFSection(".gcc_compiled_long32",
734 ELF::SHT_PROGBITS, 0));
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000735 else
Lang Hames9ff69c82015-04-24 19:11:51 +0000736 OutStreamer->SwitchSection(OutContext.getELFSection(".gcc_compiled_long64",
737 ELF::SHT_PROGBITS, 0));
Benjamin Kramer0151d7b2010-04-05 10:17:15 +0000738 }
Daniel Sanders7e527422014-07-10 13:38:23 +0000739
Eric Christopher8af49b32015-02-18 01:01:57 +0000740 getTargetStreamer().updateABIInfo(STI);
Daniel Sanders7e527422014-07-10 13:38:23 +0000741
Daniel Sanderse22244b2014-07-21 15:25:24 +0000742 // We should always emit a '.module fp=...' but binutils 2.24 does not accept
743 // it. We therefore emit it when it contradicts the ABI defaults (-mfpxx or
744 // -mfp64) and omit it otherwise.
Eric Christopher8af49b32015-02-18 01:01:57 +0000745 if (ABI.IsO32() && (STI.isABI_FPXX() || STI.isFP64bit()))
Daniel Sanderse22244b2014-07-21 15:25:24 +0000746 getTargetStreamer().emitDirectiveModuleFP();
747
748 // We should always emit a '.module [no]oddspreg' but binutils 2.24 does not
749 // accept it. We therefore emit it when it contradicts the default or an
750 // option has changed the default (i.e. FPXX) and omit it otherwise.
Eric Christopher8af49b32015-02-18 01:01:57 +0000751 if (ABI.IsO32() && (!STI.useOddSPReg() || STI.isABI_FPXX()))
752 getTargetStreamer().emitDirectiveModuleOddSPReg(STI.useOddSPReg(),
Eric Christopherd86af632015-01-29 23:27:45 +0000753 ABI.IsO32());
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000754}
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000755
Eric Christopher64d35be2015-02-19 19:52:25 +0000756void MipsAsmPrinter::emitInlineAsmStart() const {
Toma Tabacua23f13c2014-12-17 10:56:16 +0000757 MipsTargetStreamer &TS = getTargetStreamer();
758
Toma Tabacu68e8a9c2015-01-09 15:00:30 +0000759 // GCC's choice of assembler options for inline assembly code ('at', 'macro'
760 // and 'reorder') is different from LLVM's choice for generated code ('noat',
761 // 'nomacro' and 'noreorder').
762 // In order to maintain compatibility with inline assembly code which depends
763 // on GCC's assembler options being used, we have to switch to those options
764 // for the duration of the inline assembly block and then switch back.
Toma Tabacua23f13c2014-12-17 10:56:16 +0000765 TS.emitDirectiveSetPush();
766 TS.emitDirectiveSetAt();
767 TS.emitDirectiveSetMacro();
768 TS.emitDirectiveSetReorder();
Lang Hames9ff69c82015-04-24 19:11:51 +0000769 OutStreamer->AddBlankLine();
Toma Tabacua23f13c2014-12-17 10:56:16 +0000770}
771
772void MipsAsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
773 const MCSubtargetInfo *EndInfo) const {
Lang Hames9ff69c82015-04-24 19:11:51 +0000774 OutStreamer->AddBlankLine();
Toma Tabacua23f13c2014-12-17 10:56:16 +0000775 getTargetStreamer().emitDirectiveSetPop();
776}
777
Eric Christopher327fc972015-02-21 08:48:22 +0000778void MipsAsmPrinter::EmitJal(const MCSubtargetInfo &STI, MCSymbol *Symbol) {
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000779 MCInst I;
780 I.setOpcode(Mips::JAL);
781 I.addOperand(
Jim Grosbache9119e42015-05-13 18:37:00 +0000782 MCOperand::createExpr(MCSymbolRefExpr::Create(Symbol, OutContext)));
Lang Hames9ff69c82015-04-24 19:11:51 +0000783 OutStreamer->EmitInstruction(I, STI);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000784}
785
Eric Christopher327fc972015-02-21 08:48:22 +0000786void MipsAsmPrinter::EmitInstrReg(const MCSubtargetInfo &STI, unsigned Opcode,
787 unsigned Reg) {
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000788 MCInst I;
789 I.setOpcode(Opcode);
Jim Grosbache9119e42015-05-13 18:37:00 +0000790 I.addOperand(MCOperand::createReg(Reg));
Lang Hames9ff69c82015-04-24 19:11:51 +0000791 OutStreamer->EmitInstruction(I, STI);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000792}
793
Eric Christopher327fc972015-02-21 08:48:22 +0000794void MipsAsmPrinter::EmitInstrRegReg(const MCSubtargetInfo &STI,
795 unsigned Opcode, unsigned Reg1,
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000796 unsigned Reg2) {
797 MCInst I;
798 //
799 // Because of the current td files for Mips32, the operands for MTC1
800 // appear backwards from their normal assembly order. It's not a trivial
801 // change to fix this in the td file so we adjust for it here.
802 //
803 if (Opcode == Mips::MTC1) {
804 unsigned Temp = Reg1;
805 Reg1 = Reg2;
806 Reg2 = Temp;
807 }
808 I.setOpcode(Opcode);
Jim Grosbache9119e42015-05-13 18:37:00 +0000809 I.addOperand(MCOperand::createReg(Reg1));
810 I.addOperand(MCOperand::createReg(Reg2));
Lang Hames9ff69c82015-04-24 19:11:51 +0000811 OutStreamer->EmitInstruction(I, STI);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000812}
813
Eric Christopher327fc972015-02-21 08:48:22 +0000814void MipsAsmPrinter::EmitInstrRegRegReg(const MCSubtargetInfo &STI,
815 unsigned Opcode, unsigned Reg1,
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000816 unsigned Reg2, unsigned Reg3) {
817 MCInst I;
818 I.setOpcode(Opcode);
Jim Grosbache9119e42015-05-13 18:37:00 +0000819 I.addOperand(MCOperand::createReg(Reg1));
820 I.addOperand(MCOperand::createReg(Reg2));
821 I.addOperand(MCOperand::createReg(Reg3));
Lang Hames9ff69c82015-04-24 19:11:51 +0000822 OutStreamer->EmitInstruction(I, STI);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000823}
824
Eric Christopher327fc972015-02-21 08:48:22 +0000825void MipsAsmPrinter::EmitMovFPIntPair(const MCSubtargetInfo &STI,
826 unsigned MovOpc, unsigned Reg1,
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000827 unsigned Reg2, unsigned FPReg1,
828 unsigned FPReg2, bool LE) {
829 if (!LE) {
830 unsigned temp = Reg1;
831 Reg1 = Reg2;
832 Reg2 = temp;
833 }
Eric Christopher327fc972015-02-21 08:48:22 +0000834 EmitInstrRegReg(STI, MovOpc, Reg1, FPReg1);
835 EmitInstrRegReg(STI, MovOpc, Reg2, FPReg2);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000836}
837
Eric Christopher327fc972015-02-21 08:48:22 +0000838void MipsAsmPrinter::EmitSwapFPIntParams(const MCSubtargetInfo &STI,
839 Mips16HardFloatInfo::FPParamVariant PV,
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000840 bool LE, bool ToFP) {
841 using namespace Mips16HardFloatInfo;
842 unsigned MovOpc = ToFP ? Mips::MTC1 : Mips::MFC1;
843 switch (PV) {
844 case FSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000845 EmitInstrRegReg(STI, MovOpc, Mips::A0, Mips::F12);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000846 break;
847 case FFSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000848 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F14, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000849 break;
850 case FDSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000851 EmitInstrRegReg(STI, MovOpc, Mips::A0, Mips::F12);
852 EmitMovFPIntPair(STI, MovOpc, Mips::A2, Mips::A3, Mips::F14, Mips::F15, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000853 break;
854 case DSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000855 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000856 break;
857 case DDSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000858 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
859 EmitMovFPIntPair(STI, MovOpc, Mips::A2, Mips::A3, Mips::F14, Mips::F15, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000860 break;
861 case DFSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000862 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
863 EmitInstrRegReg(STI, MovOpc, Mips::A2, Mips::F14);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000864 break;
865 case NoSig:
866 return;
867 }
868}
869
Eric Christopher327fc972015-02-21 08:48:22 +0000870void MipsAsmPrinter::EmitSwapFPIntRetval(
871 const MCSubtargetInfo &STI, Mips16HardFloatInfo::FPReturnVariant RV,
872 bool LE) {
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000873 using namespace Mips16HardFloatInfo;
874 unsigned MovOpc = Mips::MFC1;
875 switch (RV) {
876 case FRet:
Eric Christopher327fc972015-02-21 08:48:22 +0000877 EmitInstrRegReg(STI, MovOpc, Mips::V0, Mips::F0);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000878 break;
879 case DRet:
Eric Christopher327fc972015-02-21 08:48:22 +0000880 EmitMovFPIntPair(STI, MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000881 break;
882 case CFRet:
Eric Christopher327fc972015-02-21 08:48:22 +0000883 EmitMovFPIntPair(STI, MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000884 break;
885 case CDRet:
Eric Christopher327fc972015-02-21 08:48:22 +0000886 EmitMovFPIntPair(STI, MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
887 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F2, Mips::F3, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000888 break;
889 case NoFPRet:
890 break;
891 }
892}
893
894void MipsAsmPrinter::EmitFPCallStub(
895 const char *Symbol, const Mips16HardFloatInfo::FuncSignature *Signature) {
896 MCSymbol *MSymbol = OutContext.GetOrCreateSymbol(StringRef(Symbol));
897 using namespace Mips16HardFloatInfo;
Eric Christopherbb401642015-02-21 08:32:22 +0000898 bool LE = getDataLayout().isLittleEndian();
Eric Christopher327fc972015-02-21 08:48:22 +0000899 // Construct a local MCSubtargetInfo here.
900 // This is because the MachineFunction won't exist (but have not yet been
901 // freed) and since we're at the global level we can use the default
902 // constructed subtarget.
903 std::unique_ptr<MCSubtargetInfo> STI(TM.getTarget().createMCSubtargetInfo(
904 TM.getTargetTriple(), TM.getTargetCPU(), TM.getTargetFeatureString()));
905
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000906 //
907 // .global xxxx
908 //
Lang Hames9ff69c82015-04-24 19:11:51 +0000909 OutStreamer->EmitSymbolAttribute(MSymbol, MCSA_Global);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000910 const char *RetType;
911 //
912 // make the comment field identifying the return and parameter
913 // types of the floating point stub
914 // # Stub function to call rettype xxxx (params)
915 //
916 switch (Signature->RetSig) {
917 case FRet:
918 RetType = "float";
919 break;
920 case DRet:
921 RetType = "double";
922 break;
923 case CFRet:
924 RetType = "complex";
925 break;
926 case CDRet:
927 RetType = "double complex";
928 break;
929 case NoFPRet:
930 RetType = "";
931 break;
932 }
933 const char *Parms;
934 switch (Signature->ParamSig) {
935 case FSig:
936 Parms = "float";
937 break;
938 case FFSig:
939 Parms = "float, float";
940 break;
941 case FDSig:
942 Parms = "float, double";
943 break;
944 case DSig:
945 Parms = "double";
946 break;
947 case DDSig:
948 Parms = "double, double";
949 break;
950 case DFSig:
951 Parms = "double, float";
952 break;
953 case NoSig:
954 Parms = "";
955 break;
956 }
Lang Hames9ff69c82015-04-24 19:11:51 +0000957 OutStreamer->AddComment("\t# Stub function to call " + Twine(RetType) + " " +
958 Twine(Symbol) + " (" + Twine(Parms) + ")");
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000959 //
960 // probably not necessary but we save and restore the current section state
961 //
Lang Hames9ff69c82015-04-24 19:11:51 +0000962 OutStreamer->PushSection();
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000963 //
964 // .section mips16.call.fpxxxx,"ax",@progbits
965 //
966 const MCSectionELF *M = OutContext.getELFSection(
967 ".mips16.call.fp." + std::string(Symbol), ELF::SHT_PROGBITS,
Rafael Espindolaba31e272015-01-29 17:33:21 +0000968 ELF::SHF_ALLOC | ELF::SHF_EXECINSTR);
Lang Hames9ff69c82015-04-24 19:11:51 +0000969 OutStreamer->SwitchSection(M, nullptr);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000970 //
971 // .align 2
972 //
Lang Hames9ff69c82015-04-24 19:11:51 +0000973 OutStreamer->EmitValueToAlignment(4);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000974 MipsTargetStreamer &TS = getTargetStreamer();
975 //
976 // .set nomips16
977 // .set nomicromips
978 //
979 TS.emitDirectiveSetNoMips16();
980 TS.emitDirectiveSetNoMicroMips();
981 //
982 // .ent __call_stub_fp_xxxx
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000983 // .type __call_stub_fp_xxxx,@function
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000984 // __call_stub_fp_xxxx:
985 //
986 std::string x = "__call_stub_fp_" + std::string(Symbol);
987 MCSymbol *Stub = OutContext.GetOrCreateSymbol(StringRef(x));
988 TS.emitDirectiveEnt(*Stub);
989 MCSymbol *MType =
990 OutContext.GetOrCreateSymbol("__call_stub_fp_" + Twine(Symbol));
Lang Hames9ff69c82015-04-24 19:11:51 +0000991 OutStreamer->EmitSymbolAttribute(MType, MCSA_ELF_TypeFunction);
992 OutStreamer->EmitLabel(Stub);
Eric Christopherd5bc07e2015-02-21 08:32:38 +0000993
994 // Only handle non-pic for now.
995 assert(TM.getRelocationModel() != Reloc::PIC_ &&
996 "should not be here if we are compiling pic");
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000997 TS.emitDirectiveSetReorder();
998 //
999 // We need to add a MipsMCExpr class to MCTargetDesc to fully implement
1000 // stubs without raw text but this current patch is for compiler generated
1001 // functions and they all return some value.
1002 // The calling sequence for non pic is different in that case and we need
1003 // to implement %lo and %hi in order to handle the case of no return value
1004 // See the corresponding method in Mips16HardFloat for details.
1005 //
1006 // mov the return address to S2.
1007 // we have no stack space to store it and we are about to make another call.
1008 // We need to make sure that the enclosing function knows to save S2
1009 // This should have already been handled.
1010 //
1011 // Mov $18, $31
1012
Eric Christopher327fc972015-02-21 08:48:22 +00001013 EmitInstrRegRegReg(*STI, Mips::ADDu, Mips::S2, Mips::RA, Mips::ZERO);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001014
Eric Christopher327fc972015-02-21 08:48:22 +00001015 EmitSwapFPIntParams(*STI, Signature->ParamSig, LE, true);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001016
1017 // Jal xxxx
1018 //
Eric Christopher327fc972015-02-21 08:48:22 +00001019 EmitJal(*STI, MSymbol);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001020
1021 // fix return values
Eric Christopher327fc972015-02-21 08:48:22 +00001022 EmitSwapFPIntRetval(*STI, Signature->RetSig, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001023 //
1024 // do the return
1025 // if (Signature->RetSig == NoFPRet)
1026 // llvm_unreachable("should not be any stubs here with no return value");
1027 // else
Eric Christopher327fc972015-02-21 08:48:22 +00001028 EmitInstrReg(*STI, Mips::JR, Mips::S2);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001029
1030 MCSymbol *Tmp = OutContext.CreateTempSymbol();
Lang Hames9ff69c82015-04-24 19:11:51 +00001031 OutStreamer->EmitLabel(Tmp);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001032 const MCSymbolRefExpr *E = MCSymbolRefExpr::Create(Stub, OutContext);
1033 const MCSymbolRefExpr *T = MCSymbolRefExpr::Create(Tmp, OutContext);
1034 const MCExpr *T_min_E = MCBinaryExpr::CreateSub(T, E, OutContext);
Lang Hames9ff69c82015-04-24 19:11:51 +00001035 OutStreamer->EmitELFSize(Stub, T_min_E);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001036 TS.emitDirectiveEnd(x);
Lang Hames9ff69c82015-04-24 19:11:51 +00001037 OutStreamer->PopSection();
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001038}
1039
1040void MipsAsmPrinter::EmitEndOfAsmFile(Module &M) {
1041 // Emit needed stubs
1042 //
1043 for (std::map<
1044 const char *,
1045 const llvm::Mips16HardFloatInfo::FuncSignature *>::const_iterator
1046 it = StubsNeeded.begin();
1047 it != StubsNeeded.end(); ++it) {
1048 const char *Symbol = it->first;
1049 const llvm::Mips16HardFloatInfo::FuncSignature *Signature = it->second;
1050 EmitFPCallStub(Symbol, Signature);
1051 }
Rafael Espindola2ab7ea72014-01-27 01:33:33 +00001052 // return to the text section
Lang Hames9ff69c82015-04-24 19:11:51 +00001053 OutStreamer->SwitchSection(OutContext.getObjectFileInfo()->getTextSection());
Jack Carterc1b17ed2013-01-18 21:20:38 +00001054}
1055
Akira Hatanakaf2bcad92011-07-01 01:04:43 +00001056void MipsAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
1057 raw_ostream &OS) {
1058 // TODO: implement
1059}
1060
Sasa Stankovic8c5736b2014-02-28 10:00:38 +00001061// Align all targets of indirect branches on bundle size. Used only if target
1062// is NaCl.
1063void MipsAsmPrinter::NaClAlignIndirectJumpTargets(MachineFunction &MF) {
1064 // Align all blocks that are jumped to through jump table.
1065 if (MachineJumpTableInfo *JtInfo = MF.getJumpTableInfo()) {
1066 const std::vector<MachineJumpTableEntry> &JT = JtInfo->getJumpTables();
1067 for (unsigned I = 0; I < JT.size(); ++I) {
1068 const std::vector<MachineBasicBlock*> &MBBs = JT[I].MBBs;
1069
1070 for (unsigned J = 0; J < MBBs.size(); ++J)
1071 MBBs[J]->setAlignment(MIPS_NACL_BUNDLE_ALIGN);
1072 }
1073 }
1074
1075 // If basic block address is taken, block can be target of indirect branch.
1076 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
1077 MBB != E; ++MBB) {
1078 if (MBB->hasAddressTaken())
1079 MBB->setAlignment(MIPS_NACL_BUNDLE_ALIGN);
1080 }
1081}
1082
Sasa Stankovic7b061a42014-04-30 15:06:25 +00001083bool MipsAsmPrinter::isLongBranchPseudo(int Opcode) const {
1084 return (Opcode == Mips::LONG_BRANCH_LUi
1085 || Opcode == Mips::LONG_BRANCH_ADDiu
Sasa Stankovic7b061a42014-04-30 15:06:25 +00001086 || Opcode == Mips::LONG_BRANCH_DADDiu);
1087}
1088
Bob Wilson5a495fe2009-06-23 23:59:40 +00001089// Force static initialization.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +00001090extern "C" void LLVMInitializeMipsAsmPrinter() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +00001091 RegisterAsmPrinter<MipsAsmPrinter> X(TheMipsTarget);
1092 RegisterAsmPrinter<MipsAsmPrinter> Y(TheMipselTarget);
Akira Hatanaka3d673cc2011-09-21 03:00:58 +00001093 RegisterAsmPrinter<MipsAsmPrinter> A(TheMips64Target);
1094 RegisterAsmPrinter<MipsAsmPrinter> B(TheMips64elTarget);
Daniel Dunbare8338102009-07-15 20:24:03 +00001095}