blob: 8dab262ac2569fa8f4c7d484fe1941e7bedc8c64 [file] [log] [blame]
Jia Liu9f610112012-02-17 08:55:11 +00001//===-- MipsAsmPrinter.cpp - Mips LLVM Assembly Printer -------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00007//
Akira Hatanakae2489122011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00009//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to GAS-format MIPS assembly language.
12//
Akira Hatanakae2489122011-04-15 21:51:11 +000013//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000014
Akira Hatanaka9c6028f2011-07-07 23:56:50 +000015#include "InstPrinter/MipsInstPrinter.h"
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000016#include "MCTargetDesc/MipsBaseInfo.h"
Sasa Stankovic8c5736b2014-02-28 10:00:38 +000017#include "MCTargetDesc/MipsMCNaCl.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "Mips.h"
Jack Carterc1b17ed2013-01-18 21:20:38 +000019#include "MipsAsmPrinter.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "MipsInstrInfo.h"
21#include "MipsMCInstLower.h"
Eric Christophera5762812015-01-26 17:33:46 +000022#include "MipsTargetMachine.h"
Rafael Espindolaa17151a2013-10-08 13:08:17 +000023#include "MipsTargetStreamer.h"
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +000024#include "llvm/ADT/SmallString.h"
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +000025#include "llvm/ADT/Twine.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000026#include "llvm/CodeGen/MachineConstantPool.h"
Bruno Cardoso Lopesbcda5e22007-07-11 23:24:41 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
Jack Carterb2af5122012-07-05 23:58:21 +000028#include "llvm/CodeGen/MachineFunctionPass.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000029#include "llvm/CodeGen/MachineInstr.h"
Sasa Stankovic8c5736b2014-02-28 10:00:38 +000030#include "llvm/CodeGen/MachineJumpTableInfo.h"
Akira Hatanaka2fcc1cf2011-08-12 21:30:06 +000031#include "llvm/CodeGen/MachineMemOperand.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/BasicBlock.h"
33#include "llvm/IR/DataLayout.h"
34#include "llvm/IR/InlineAsm.h"
35#include "llvm/IR/Instructions.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000036#include "llvm/IR/Mangler.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000037#include "llvm/MC/MCAsmInfo.h"
Rafael Espindola2ab7ea72014-01-27 01:33:33 +000038#include "llvm/MC/MCContext.h"
Rafael Espindolaac4ad252013-10-05 16:42:21 +000039#include "llvm/MC/MCELFStreamer.h"
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000040#include "llvm/MC/MCExpr.h"
Akira Hatanaka9c6028f2011-07-07 23:56:50 +000041#include "llvm/MC/MCInst.h"
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000042#include "llvm/MC/MCSection.h"
Rafael Espindola2ab7ea72014-01-27 01:33:33 +000043#include "llvm/MC/MCSectionELF.h"
Rafael Espindolaa8695762015-06-02 00:25:12 +000044#include "llvm/MC/MCSymbolELF.h"
Jack Carterab3cb422013-02-19 22:04:37 +000045#include "llvm/Support/ELF.h"
Jack Carterb2af5122012-07-05 23:58:21 +000046#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000047#include "llvm/Support/raw_ostream.h"
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +000048#include "llvm/Target/TargetLoweringObjectFile.h"
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +000049#include "llvm/Target/TargetOptions.h"
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000050#include <string>
Akira Hatanakaf2bcad92011-07-01 01:04:43 +000051
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000052using namespace llvm;
53
Chandler Carruth84e68b22014-04-22 02:41:26 +000054#define DEBUG_TYPE "mips-asm-printer"
55
Toma Tabacua23f13c2014-12-17 10:56:16 +000056MipsTargetStreamer &MipsAsmPrinter::getTargetStreamer() const {
Lang Hames9ff69c82015-04-24 19:11:51 +000057 return static_cast<MipsTargetStreamer &>(*OutStreamer->getTargetStreamer());
Rafael Espindolaa17151a2013-10-08 13:08:17 +000058}
59
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000060bool MipsAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Eric Christopher3ee30d02015-02-20 08:39:06 +000061 Subtarget = &MF.getSubtarget<MipsSubtarget>();
Eric Christopher8ef7a6a2014-07-18 00:08:53 +000062
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000063 MipsFI = MF.getInfo<MipsFunctionInfo>();
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000064 if (Subtarget->inMips16Mode())
65 for (std::map<
66 const char *,
67 const llvm::Mips16HardFloatInfo::FuncSignature *>::const_iterator
68 it = MipsFI->StubsNeeded.begin();
69 it != MipsFI->StubsNeeded.end(); ++it) {
70 const char *Symbol = it->first;
71 const llvm::Mips16HardFloatInfo::FuncSignature *Signature = it->second;
72 if (StubsNeeded.find(Symbol) == StubsNeeded.end())
73 StubsNeeded[Symbol] = Signature;
74 }
Reed Kotler91ae9822013-10-27 21:57:36 +000075 MCP = MF.getConstantPool();
Sasa Stankovic8c5736b2014-02-28 10:00:38 +000076
77 // In NaCl, all indirect jump targets must be aligned to bundle size.
78 if (Subtarget->isTargetNaCl())
79 NaClAlignIndirectJumpTargets(MF);
80
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000081 AsmPrinter::runOnMachineFunction(MF);
82 return true;
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +000083}
84
Akira Hatanaka42a35242012-09-27 01:59:07 +000085bool MipsAsmPrinter::lowerOperand(const MachineOperand &MO, MCOperand &MCOp) {
86 MCOp = MCInstLowering.LowerOperand(MO);
87 return MCOp.isValid();
88}
89
90#include "MipsGenMCPseudoLowering.inc"
91
Daniel Sandersf5a5fbd2014-07-09 10:21:59 +000092// Lower PseudoReturn/PseudoIndirectBranch/PseudoIndirectBranch64 to JR, JR_MM,
93// JALR, or JALR64 as appropriate for the target
94void MipsAsmPrinter::emitPseudoIndirectBranch(MCStreamer &OutStreamer,
95 const MachineInstr *MI) {
Daniel Sanders338513b2014-07-09 10:16:07 +000096 bool HasLinkReg = false;
Simon Dardisea343152016-08-18 13:22:43 +000097 bool InMicroMipsMode = Subtarget->inMicroMipsMode();
Daniel Sanders338513b2014-07-09 10:16:07 +000098 MCInst TmpInst0;
99
100 if (Subtarget->hasMips64r6()) {
101 // MIPS64r6 should use (JALR64 ZERO_64, $rs)
102 TmpInst0.setOpcode(Mips::JALR64);
103 HasLinkReg = true;
104 } else if (Subtarget->hasMips32r6()) {
105 // MIPS32r6 should use (JALR ZERO, $rs)
Simon Dardisea343152016-08-18 13:22:43 +0000106 if (InMicroMipsMode)
107 TmpInst0.setOpcode(Mips::JRC16_MMR6);
108 else {
109 TmpInst0.setOpcode(Mips::JALR);
110 HasLinkReg = true;
111 }
Daniel Sanders338513b2014-07-09 10:16:07 +0000112 } 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
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000173 EmitGlobalConstant(MF->getDataLayout(), MCPE.Val.ConstVal);
Reed Kotler91ae9822013-10-27 21:57:36 +0000174 return;
175 }
176
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000177
178 MachineBasicBlock::const_instr_iterator I = MI->getIterator();
179 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
Akira Hatanaka5ac78682012-06-13 23:25:52 +0000180
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 ||
Simon Dardisea343152016-08-18 13:22:43 +0000189 I->getOpcode() == Mips::PseudoIndirectBranch64 ||
190 I->getOpcode() == Mips::TAILCALLREG ||
191 I->getOpcode() == Mips::TAILCALLREG64) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000192 emitPseudoIndirectBranch(*OutStreamer, &*I);
Daniel Sanders338513b2014-07-09 10:16:07 +0000193 continue;
194 }
195
Reed Kotler76c9bcd2013-02-15 21:05:58 +0000196 // The inMips16Mode() test is not permanent.
197 // Some instructions are marked as pseudo right now which
198 // would make the test fail for the wrong reason but
199 // that will be fixed soon. We need this here because we are
200 // removing another test for this situation downstream in the
201 // callchain.
202 //
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000203 if (I->isPseudo() && !Subtarget->inMips16Mode()
204 && !isLongBranchPseudo(I->getOpcode()))
Reed Kotler76c9bcd2013-02-15 21:05:58 +0000205 llvm_unreachable("Pseudo opcode found in EmitInstruction()");
206
Akira Hatanaka556135d2013-02-06 21:50:15 +0000207 MCInst TmpInst0;
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +0000208 MCInstLowering.Lower(&*I, TmpInst0);
Lang Hames9ff69c82015-04-24 19:11:51 +0000209 EmitToStreamer(*OutStreamer, TmpInst0);
Akira Hatanaka556135d2013-02-06 21:50:15 +0000210 } while ((++I != E) && I->isInsideBundle()); // Delay slot check
Akira Hatanakaddd12652011-07-07 20:10:52 +0000211}
212
Akira Hatanakae2489122011-04-15 21:51:11 +0000213//===----------------------------------------------------------------------===//
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000214//
215// Mips Asm Directives
216//
217// -- Frame directive "frame Stackpointer, Stacksize, RARegister"
218// Describe the stack frame.
219//
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000220// -- Mask directives "(f)mask bitmask, offset"
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000221// Tells the assembler which registers are saved and where.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000222// bitmask - contain a little endian bitset indicating which registers are
223// saved on function prologue (e.g. with a 0x80000000 mask, the
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000224// assembler knows the register 31 (RA) is saved at prologue.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000225// offset - the position before stack pointer subtraction indicating where
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000226// the first saved register on prologue is located. (e.g. with a
227//
228// Consider the following function prologue:
229//
Bill Wendling97925ec2008-02-27 06:33:05 +0000230// .frame $fp,48,$ra
231// .mask 0xc0000000,-8
232// addiu $sp, $sp, -48
233// sw $ra, 40($sp)
234// sw $fp, 36($sp)
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000235//
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000236// With a 0xc0000000 mask, the assembler knows the register 31 (RA) and
237// 30 (FP) are saved at prologue. As the save order on prologue is from
238// left to right, RA is saved first. A -8 offset means that after the
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000239// stack pointer subtration, the first register in the mask (RA) will be
240// saved at address 48-8=40.
241//
Akira Hatanakae2489122011-04-15 21:51:11 +0000242//===----------------------------------------------------------------------===//
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000243
Akira Hatanakae2489122011-04-15 21:51:11 +0000244//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000245// Mask directives
Akira Hatanakae2489122011-04-15 21:51:11 +0000246//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000247
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000248// Create a bitmask with all callee saved registers for CPU or Floating Point
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000249// registers. For CPU registers consider RA, GP and FP for saving if necessary.
Rafael Espindola25fa2912014-01-27 04:33:11 +0000250void MipsAsmPrinter::printSavedRegsBitmask() {
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000251 // CPU and FPU Saved Registers Bitmasks
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000252 unsigned CPUBitmask = 0, FPUBitmask = 0;
253 int CPUTopSavedRegOff, FPUTopSavedRegOff;
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000254
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000255 // Set the CPU and FPU Bitmasks
Matthias Braun941a7052016-07-28 18:40:00 +0000256 const MachineFrameInfo &MFI = MF->getFrameInfo();
Eric Christophercba722f2015-03-21 03:13:07 +0000257 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
Matthias Braun941a7052016-07-28 18:40:00 +0000258 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000259 // size of stack area to which FP callee-saved regs are saved.
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000260 unsigned CPURegSize = Mips::GPR32RegClass.getSize();
Craig Topperc7242e02012-04-20 07:30:17 +0000261 unsigned FGR32RegSize = Mips::FGR32RegClass.getSize();
262 unsigned AFGR64RegSize = Mips::AFGR64RegClass.getSize();
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000263 bool HasAFGR64Reg = false;
264 unsigned CSFPRegsSize = 0;
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000265
Toma Tabacube218922015-04-09 10:54:16 +0000266 for (const auto &I : CSI) {
267 unsigned Reg = I.getReg();
Eric Christophercba722f2015-03-21 03:13:07 +0000268 unsigned RegNum = TRI->getEncodingValue(Reg);
Toma Tabacube218922015-04-09 10:54:16 +0000269
270 // If it's a floating point register, set the FPU Bitmask.
271 // If it's a general purpose register, set the CPU Bitmask.
272 if (Mips::FGR32RegClass.contains(Reg)) {
273 FPUBitmask |= (1 << RegNum);
274 CSFPRegsSize += FGR32RegSize;
275 } else if (Mips::AFGR64RegClass.contains(Reg)) {
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000276 FPUBitmask |= (3 << RegNum);
277 CSFPRegsSize += AFGR64RegSize;
278 HasAFGR64Reg = true;
Toma Tabacube218922015-04-09 10:54:16 +0000279 } else if (Mips::GPR32RegClass.contains(Reg))
280 CPUBitmask |= (1 << RegNum);
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000281 }
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000282
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000283 // FP Regs are saved right below where the virtual frame pointer points to.
284 FPUTopSavedRegOff = FPUBitmask ?
285 (HasAFGR64Reg ? -AFGR64RegSize : -FGR32RegSize) : 0;
286
287 // CPU Regs are saved below FP Regs.
288 CPUTopSavedRegOff = CPUBitmask ? -CSFPRegsSize - CPURegSize : 0;
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000289
Rafael Espindola25fa2912014-01-27 04:33:11 +0000290 MipsTargetStreamer &TS = getTargetStreamer();
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000291 // Print CPUBitmask
Rafael Espindola25fa2912014-01-27 04:33:11 +0000292 TS.emitMask(CPUBitmask, CPUTopSavedRegOff);
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000293
294 // Print FPUBitmask
Rafael Espindola25fa2912014-01-27 04:33:11 +0000295 TS.emitFMask(FPUBitmask, FPUTopSavedRegOff);
Bruno Cardoso Lopesbcda5e22007-07-11 23:24:41 +0000296}
297
Akira Hatanakae2489122011-04-15 21:51:11 +0000298//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000299// Frame and Set directives
Akira Hatanakae2489122011-04-15 21:51:11 +0000300//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000301
302/// Frame Directive
Chris Lattner5e596182010-04-04 07:05:53 +0000303void MipsAsmPrinter::emitFrameDirective() {
Eric Christophercba722f2015-03-21 03:13:07 +0000304 const TargetRegisterInfo &RI = *MF->getSubtarget().getRegisterInfo();
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000305
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000306 unsigned stackReg = RI.getFrameRegister(*MF);
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000307 unsigned returnReg = RI.getRARegister();
Matthias Braun941a7052016-07-28 18:40:00 +0000308 unsigned stackSize = MF->getFrameInfo().getStackSize();
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000309
Rafael Espindola054234f2014-01-27 03:53:56 +0000310 getTargetStreamer().emitFrame(stackReg, stackSize, returnReg);
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000311}
312
313/// Emit Set directives.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000314const char *MipsAsmPrinter::getCurrentABIString() const {
Eric Christophera5762812015-01-26 17:33:46 +0000315 switch (static_cast<MipsTargetMachine &>(TM).getABI().GetEnumValue()) {
Daniel Sanderse2e25da2014-10-24 16:15:27 +0000316 case MipsABIInfo::ABI::O32: return "abi32";
317 case MipsABIInfo::ABI::N32: return "abiN32";
318 case MipsABIInfo::ABI::N64: return "abi64";
Dmitri Gribenkoca1e27b2012-09-10 21:26:47 +0000319 default: llvm_unreachable("Unknown Mips ABI");
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000320 }
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000321}
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000322
Chris Lattner5d9fb4b2010-01-27 23:23:58 +0000323void MipsAsmPrinter::EmitFunctionEntryLabel() {
Rafael Espindola6633d572014-01-14 18:57:12 +0000324 MipsTargetStreamer &TS = getTargetStreamer();
Sasa Stankovic8c5736b2014-02-28 10:00:38 +0000325
326 // NaCl sandboxing requires that indirect call instructions are masked.
327 // This means that function entry points should be bundle-aligned.
328 if (Subtarget->isTargetNaCl())
329 EmitAlignment(std::max(MF->getAlignment(), MIPS_NACL_BUNDLE_ALIGN));
330
Daniel Sanders1d148642016-06-16 09:17:03 +0000331 if (Subtarget->inMicroMipsMode()) {
Rafael Espindola6633d572014-01-14 18:57:12 +0000332 TS.emitDirectiveSetMicroMips();
Daniel Sanders1d148642016-06-16 09:17:03 +0000333 TS.setUsesMicroMips();
334 } else
Matheus Almeidadc7e48e2014-04-16 11:46:59 +0000335 TS.emitDirectiveSetNoMicroMips();
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000336
Rafael Espindola6633d572014-01-14 18:57:12 +0000337 if (Subtarget->inMips16Mode())
338 TS.emitDirectiveSetMips16();
339 else
340 TS.emitDirectiveSetNoMips16();
Jack Carterab3cb422013-02-19 22:04:37 +0000341
Rafael Espindola6633d572014-01-14 18:57:12 +0000342 TS.emitDirectiveEnt(*CurrentFnSym);
Lang Hames9ff69c82015-04-24 19:11:51 +0000343 OutStreamer->EmitLabel(CurrentFnSym);
Chris Lattner5d9fb4b2010-01-27 23:23:58 +0000344}
345
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000346/// EmitFunctionBodyStart - Targets can override this to emit stuff before
347/// the first basic block in the function.
348void MipsAsmPrinter::EmitFunctionBodyStart() {
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000349 MipsTargetStreamer &TS = getTargetStreamer();
350
Rafael Espindola7d78b2a2013-10-29 16:24:21 +0000351 MCInstLowering.Initialize(&MF->getContext());
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +0000352
Duncan P. N. Exon Smith2e753142015-02-14 02:37:48 +0000353 bool IsNakedFunction = MF->getFunction()->hasFnAttribute(Attribute::Naked);
Reed Kotler0f2b10e2013-05-03 23:17:24 +0000354 if (!IsNakedFunction)
355 emitFrameDirective();
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000356
Rafael Espindola25fa2912014-01-27 04:33:11 +0000357 if (!IsNakedFunction)
358 printSavedRegsBitmask();
359
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000360 if (!Subtarget->inMips16Mode()) {
361 TS.emitDirectiveSetNoReorder();
362 TS.emitDirectiveSetNoMacro();
363 TS.emitDirectiveSetNoAt();
Akira Hatanaka8f3573032012-05-12 00:48:43 +0000364 }
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000365}
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000366
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000367/// EmitFunctionBodyEnd - Targets can override this to emit stuff after
368/// the last basic block in the function.
369void MipsAsmPrinter::EmitFunctionBodyEnd() {
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000370 MipsTargetStreamer &TS = getTargetStreamer();
371
Chris Lattnerfd97a332010-01-28 01:48:52 +0000372 // There are instruction for this macros, but they must
373 // always be at the function end, and we can't emit and
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000374 // break with BB logic.
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000375 if (!Subtarget->inMips16Mode()) {
376 TS.emitDirectiveSetAt();
377 TS.emitDirectiveSetMacro();
378 TS.emitDirectiveSetReorder();
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000379 }
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000380 TS.emitDirectiveEnd(CurrentFnSym->getName());
Reed Kotler91ae9822013-10-27 21:57:36 +0000381 // Make sure to terminate any constant pools that were at the end
382 // of the function.
383 if (!InConstantPool)
384 return;
385 InConstantPool = false;
Lang Hames9ff69c82015-04-24 19:11:51 +0000386 OutStreamer->EmitDataRegion(MCDR_DataRegionEnd);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000387}
388
Vasileios Kalintiris42544d62015-05-08 09:10:15 +0000389void MipsAsmPrinter::EmitBasicBlockEnd(const MachineBasicBlock &MBB) {
390 MipsTargetStreamer &TS = getTargetStreamer();
391 if (MBB.size() == 0)
392 TS.emitDirectiveInsn();
393}
394
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000395/// isBlockOnlyReachableByFallthough - Return true if the basic block has
396/// exactly one predecessor and the control transfer mechanism between
397/// the predecessor and this block is a fall-through.
Akira Hatanakae2489122011-04-15 21:51:11 +0000398bool MipsAsmPrinter::isBlockOnlyReachableByFallthrough(const MachineBasicBlock*
399 MBB) const {
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000400 // The predecessor has to be immediately before this block.
401 const MachineBasicBlock *Pred = *MBB->pred_begin();
402
403 // If the predecessor is a switch statement, assume a jump table
404 // implementation, so it is not a fall through.
405 if (const BasicBlock *bb = Pred->getBasicBlock())
406 if (isa<SwitchInst>(bb->getTerminator()))
407 return false;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000408
Akira Hatanakae625ba42011-04-01 18:57:38 +0000409 // If this is a landing pad, it isn't a fall through. If it has no preds,
410 // then nothing falls through to it.
Reid Kleckner0e288232015-08-27 23:27:47 +0000411 if (MBB->isEHPad() || MBB->pred_empty())
Akira Hatanakae625ba42011-04-01 18:57:38 +0000412 return false;
413
414 // If there isn't exactly one predecessor, it can't be a fall through.
415 MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI;
416 ++PI2;
Jia Liuf54f60f2012-02-28 07:46:26 +0000417
Akira Hatanakae625ba42011-04-01 18:57:38 +0000418 if (PI2 != MBB->pred_end())
Jia Liuf54f60f2012-02-28 07:46:26 +0000419 return false;
Akira Hatanakae625ba42011-04-01 18:57:38 +0000420
421 // The predecessor has to be immediately before this block.
422 if (!Pred->isLayoutSuccessor(MBB))
423 return false;
Jia Liuf54f60f2012-02-28 07:46:26 +0000424
Akira Hatanakae625ba42011-04-01 18:57:38 +0000425 // If the block is completely empty, then it definitely does fall through.
426 if (Pred->empty())
427 return true;
Jia Liuf54f60f2012-02-28 07:46:26 +0000428
Akira Hatanakae625ba42011-04-01 18:57:38 +0000429 // Otherwise, check the last instruction.
430 // Check if the last terminator is an unconditional branch.
431 MachineBasicBlock::const_iterator I = Pred->end();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000432 while (I != Pred->begin() && !(--I)->isTerminator()) ;
Akira Hatanakae625ba42011-04-01 18:57:38 +0000433
Evan Cheng7f8e5632011-12-07 07:15:52 +0000434 return !I->isBarrier();
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000435}
436
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000437// Print out an operand for an inline asm expression.
Eric Christophered51b9e2012-05-10 21:48:22 +0000438bool MipsAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000439 unsigned AsmVariant, const char *ExtraCode,
Chris Lattner3bb09762010-04-04 05:29:35 +0000440 raw_ostream &O) {
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000441 // Does this asm operand have a single letter operand modifier?
Eric Christophered51b9e2012-05-10 21:48:22 +0000442 if (ExtraCode && ExtraCode[0]) {
443 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000444
Eric Christophered51b9e2012-05-10 21:48:22 +0000445 const MachineOperand &MO = MI->getOperand(OpNum);
446 switch (ExtraCode[0]) {
Eric Christopherbc5d2492012-05-19 00:51:56 +0000447 default:
Jack Carterb2fd5f62012-06-21 17:14:46 +0000448 // See if this is a generic print operand
449 return AsmPrinter::PrintAsmOperand(MI,OpNum,AsmVariant,ExtraCode,O);
Eric Christopherbc5d2492012-05-19 00:51:56 +0000450 case 'X': // hex const int
451 if ((MO.getType()) != MachineOperand::MO_Immediate)
452 return true;
Benjamin Kramer33b46912015-05-23 16:53:07 +0000453 O << "0x" << Twine::utohexstr(MO.getImm());
Eric Christopherbc5d2492012-05-19 00:51:56 +0000454 return false;
455 case 'x': // hex const int (low 16 bits)
456 if ((MO.getType()) != MachineOperand::MO_Immediate)
457 return true;
Benjamin Kramer33b46912015-05-23 16:53:07 +0000458 O << "0x" << Twine::utohexstr(MO.getImm() & 0xffff);
Eric Christopherbc5d2492012-05-19 00:51:56 +0000459 return false;
460 case 'd': // decimal const int
461 if ((MO.getType()) != MachineOperand::MO_Immediate)
462 return true;
463 O << MO.getImm();
464 return false;
Eric Christopherf481ab32012-05-30 19:05:19 +0000465 case 'm': // decimal const int minus 1
466 if ((MO.getType()) != MachineOperand::MO_Immediate)
467 return true;
468 O << MO.getImm() - 1;
469 return false;
Jack Carter27747b52012-06-28 20:46:26 +0000470 case 'z': {
471 // $0 if zero, regular printing otherwise
Toma Tabacu27cab752014-11-06 14:25:42 +0000472 if (MO.getType() == MachineOperand::MO_Immediate && MO.getImm() == 0) {
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000473 O << "$0";
Toma Tabacu27cab752014-11-06 14:25:42 +0000474 return false;
475 }
476 // If not, call printOperand as normal.
477 break;
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000478 }
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000479 case 'D': // Second part of a double word register operand
480 case 'L': // Low order register of a double word register operand
Jack Cartera62ba822012-07-18 06:41:36 +0000481 case 'M': // High order register of a double word register operand
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000482 {
Jack Carterb2af5122012-07-05 23:58:21 +0000483 if (OpNum == 0)
484 return true;
485 const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
486 if (!FlagsOP.isImm())
487 return true;
488 unsigned Flags = FlagsOP.getImm();
489 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Jack Carter2ab73b12012-07-06 02:44:22 +0000490 // Number of registers represented by this operand. We are looking
491 // for 2 for 32 bit mode and 1 for 64 bit mode.
Jack Carterb2af5122012-07-05 23:58:21 +0000492 if (NumVals != 2) {
Jack Carter2ab73b12012-07-06 02:44:22 +0000493 if (Subtarget->isGP64bit() && NumVals == 1 && MO.isReg()) {
Jack Carterb2af5122012-07-05 23:58:21 +0000494 unsigned Reg = MO.getReg();
495 O << '$' << MipsInstPrinter::getRegisterName(Reg);
496 return false;
497 }
498 return true;
499 }
Jack Carter42ebf982012-07-11 21:41:49 +0000500
501 unsigned RegOp = OpNum;
502 if (!Subtarget->isGP64bit()){
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000503 // Endianess reverses which register holds the high or low value
Jack Cartera62ba822012-07-18 06:41:36 +0000504 // between M and L.
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000505 switch(ExtraCode[0]) {
Jack Cartera62ba822012-07-18 06:41:36 +0000506 case 'M':
507 RegOp = (Subtarget->isLittle()) ? OpNum + 1 : OpNum;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000508 break;
509 case 'L':
Jack Cartera62ba822012-07-18 06:41:36 +0000510 RegOp = (Subtarget->isLittle()) ? OpNum : OpNum + 1;
511 break;
512 case 'D': // Always the second part
513 RegOp = OpNum + 1;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000514 }
515 if (RegOp >= MI->getNumOperands())
516 return true;
517 const MachineOperand &MO = MI->getOperand(RegOp);
518 if (!MO.isReg())
519 return true;
520 unsigned Reg = MO.getReg();
521 O << '$' << MipsInstPrinter::getRegisterName(Reg);
522 return false;
Jack Carterb2af5122012-07-05 23:58:21 +0000523 }
Eric Christophered51b9e2012-05-10 21:48:22 +0000524 }
Daniel Sanders8b59af12013-11-12 12:56:01 +0000525 case 'w':
526 // Print MSA registers for the 'f' constraint
527 // In LLVM, the 'w' modifier doesn't need to do anything.
528 // We can just call printOperand as normal.
529 break;
Jack Carter2ab73b12012-07-06 02:44:22 +0000530 }
531 }
Eric Christophered51b9e2012-05-10 21:48:22 +0000532
533 printOperand(MI, OpNum, O);
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000534 return false;
535}
536
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000537bool MipsAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
538 unsigned OpNum, unsigned AsmVariant,
539 const char *ExtraCode,
540 raw_ostream &O) {
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000541 assert(OpNum + 1 < MI->getNumOperands() && "Insufficient operands");
542 const MachineOperand &BaseMO = MI->getOperand(OpNum);
543 const MachineOperand &OffsetMO = MI->getOperand(OpNum + 1);
544 assert(BaseMO.isReg() && "Unexpected base pointer for inline asm memory operand.");
545 assert(OffsetMO.isImm() && "Unexpected offset for inline asm memory operand.");
546 int Offset = OffsetMO.getImm();
547
Jack Carterb04e3572013-04-09 23:19:50 +0000548 // Currently we are expecting either no ExtraCode or 'D'
549 if (ExtraCode) {
550 if (ExtraCode[0] == 'D')
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000551 Offset += 4;
Jack Carterb04e3572013-04-09 23:19:50 +0000552 else
553 return true; // Unknown modifier.
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000554 // FIXME: M = high order bits
555 // FIXME: L = low order bits
Jack Carterb04e3572013-04-09 23:19:50 +0000556 }
Jia Liuf54f60f2012-02-28 07:46:26 +0000557
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000558 O << Offset << "($" << MipsInstPrinter::getRegisterName(BaseMO.getReg()) << ")";
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000559
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000560 return false;
561}
562
Chris Lattner76c564b2010-04-04 04:47:45 +0000563void MipsAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
564 raw_ostream &O) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000565 const MachineOperand &MO = MI->getOperand(opNum);
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +0000566 bool closeP = false;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000567
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000568 if (MO.getTargetFlags())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000569 closeP = true;
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000570
571 switch(MO.getTargetFlags()) {
572 case MipsII::MO_GPREL: O << "%gp_rel("; break;
573 case MipsII::MO_GOT_CALL: O << "%call16("; break;
Akira Hatanaka56d9ef52011-04-01 21:41:06 +0000574 case MipsII::MO_GOT: O << "%got("; break;
575 case MipsII::MO_ABS_HI: O << "%hi("; break;
576 case MipsII::MO_ABS_LO: O << "%lo("; break;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +0000577 case MipsII::MO_TLSGD: O << "%tlsgd("; break;
578 case MipsII::MO_GOTTPREL: O << "%gottprel("; break;
579 case MipsII::MO_TPREL_HI: O << "%tprel_hi("; break;
580 case MipsII::MO_TPREL_LO: O << "%tprel_lo("; break;
Akira Hatanaka25ce3642011-09-22 03:09:07 +0000581 case MipsII::MO_GPOFF_HI: O << "%hi(%neg(%gp_rel("; break;
582 case MipsII::MO_GPOFF_LO: O << "%lo(%neg(%gp_rel("; break;
583 case MipsII::MO_GOT_DISP: O << "%got_disp("; break;
584 case MipsII::MO_GOT_PAGE: O << "%got_page("; break;
585 case MipsII::MO_GOT_OFST: O << "%got_ofst("; break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000586 }
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000587
Chris Lattnereb2cc682009-09-13 20:31:40 +0000588 switch (MO.getType()) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000589 case MachineOperand::MO_Register:
Akira Hatanaka9c6028f2011-07-07 23:56:50 +0000590 O << '$'
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000591 << StringRef(MipsInstPrinter::getRegisterName(MO.getReg())).lower();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000592 break;
593
594 case MachineOperand::MO_Immediate:
Akira Hatanaka2db176c2011-05-24 21:22:21 +0000595 O << MO.getImm();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000596 break;
597
598 case MachineOperand::MO_MachineBasicBlock:
Matt Arsenault8b643552015-06-09 00:31:39 +0000599 MO.getMBB()->getSymbol()->print(O, MAI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000600 return;
601
602 case MachineOperand::MO_GlobalAddress:
Matt Arsenault8b643552015-06-09 00:31:39 +0000603 getSymbol(MO.getGlobal())->print(O, MAI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000604 break;
605
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000606 case MachineOperand::MO_BlockAddress: {
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000607 MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000608 O << BA->getName();
609 break;
610 }
611
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000612 case MachineOperand::MO_ConstantPoolIndex:
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000613 O << getDataLayout().getPrivateGlobalPrefix() << "CPI"
Chris Lattnera5bb3702007-12-30 23:10:15 +0000614 << getFunctionNumber() << "_" << MO.getIndex();
Bruno Cardoso Lopes4713b282009-11-19 06:06:13 +0000615 if (MO.getOffset())
616 O << "+" << MO.getOffset();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000617 break;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000618
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000619 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000620 llvm_unreachable("<unknown operand type>");
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000621 }
622
623 if (closeP) O << ")";
624}
625
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000626void MipsAsmPrinter::
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000627printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O) {
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000628 // Load/Store memory operands -- imm($reg)
629 // If PIC target the target is loaded as the
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +0000630 // pattern lw $25,%call16($28)
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000631
632 // opNum can be invalid if instruction has reglist as operand.
633 // MemOperand is always last operand of instruction (base + offset).
634 switch (MI->getOpcode()) {
635 default:
636 break;
637 case Mips::SWM32_MM:
638 case Mips::LWM32_MM:
639 opNum = MI->getNumOperands() - 2;
640 break;
641 }
642
Chris Lattner76c564b2010-04-04 04:47:45 +0000643 printOperand(MI, opNum+1, O);
Akira Hatanaka2e766ed2011-07-07 18:57:00 +0000644 O << "(";
645 printOperand(MI, opNum, O);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000646 O << ")";
647}
648
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000649void MipsAsmPrinter::
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000650printMemOperandEA(const MachineInstr *MI, int opNum, raw_ostream &O) {
651 // when using stack locations for not load/store instructions
652 // print the same way as all normal 3 operand instructions.
653 printOperand(MI, opNum, O);
654 O << ", ";
655 printOperand(MI, opNum+1, O);
656 return;
657}
658
659void MipsAsmPrinter::
Simon Dardisba92b032016-09-09 11:06:01 +0000660printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
661 const char *Modifier) {
662 const MachineOperand &MO = MI->getOperand(opNum);
663 O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
664}
665
666void MipsAsmPrinter::
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000667printRegisterList(const MachineInstr *MI, int opNum, raw_ostream &O) {
668 for (int i = opNum, e = MI->getNumOperands(); i != e; ++i) {
669 if (i != opNum) O << ", ";
670 printOperand(MI, i, O);
671 }
672}
673
Bob Wilsonb633d7a2009-09-30 22:06:26 +0000674void MipsAsmPrinter::EmitStartOfAsmFile(Module &M) {
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000675 MipsTargetStreamer &TS = getTargetStreamer();
676
677 // MipsTargetStreamer has an initialization order problem when emitting an
678 // object file directly (see MipsTargetELFStreamer for full details). Work
679 // around it by re-initializing the PIC state here.
Rafael Espindola699281c2016-05-18 11:58:50 +0000680 TS.setPic(OutContext.getObjectFileInfo()->isPositionIndependent());
Eric Christopher8af49b32015-02-18 01:01:57 +0000681
682 // Compute MIPS architecture attributes based on the default subtarget
683 // that we'd have constructed. Module level directives aren't LTO
684 // clean anyhow.
685 // FIXME: For ifunc related functions we could iterate over and look
686 // for a feature string that doesn't match the default one.
Daniel Sanders50f17232015-09-15 16:17:27 +0000687 const Triple &TT = TM.getTargetTriple();
Daniel Sandersa73f1fd2015-06-10 12:11:26 +0000688 StringRef CPU = MIPS_MC::selectMipsCPU(TT, TM.getTargetCPU());
Eric Christopher8af49b32015-02-18 01:01:57 +0000689 StringRef FS = TM.getTargetFeatureString();
690 const MipsTargetMachine &MTM = static_cast<const MipsTargetMachine &>(TM);
Daniel Sanders50f17232015-09-15 16:17:27 +0000691 const MipsSubtarget STI(TT, CPU, FS, MTM.isLittleEndian(), MTM);
Eric Christopher8af49b32015-02-18 01:01:57 +0000692
693 bool IsABICalls = STI.isABICalls();
694 const MipsABIInfo &ABI = MTM.getABI();
Daniel Sanders16fa1db2014-04-16 13:58:57 +0000695 if (IsABICalls) {
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000696 TS.emitDirectiveAbiCalls();
Daniel Sanders16fa1db2014-04-16 13:58:57 +0000697 // FIXME: This condition should be a lot more complicated that it is here.
698 // Ideally it should test for properties of the ABI and not the ABI
699 // itself.
700 // For the moment, I'm only correcting enough to make MIPS-IV work.
Rafael Espindolab0f59cb2016-06-27 17:21:46 +0000701 if (!isPositionIndependent() && !ABI.IsN64())
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000702 TS.emitDirectiveOptionPic0();
Daniel Sanders16fa1db2014-04-16 13:58:57 +0000703 }
Jack Carterf9f753c2013-06-18 19:47:15 +0000704
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000705 // Tell the assembler which ABI we are using
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000706 std::string SectionName = std::string(".mdebug.") + getCurrentABIString();
Lang Hames9ff69c82015-04-24 19:11:51 +0000707 OutStreamer->SwitchSection(
Rafael Espindolaba31e272015-01-29 17:33:21 +0000708 OutContext.getELFSection(SectionName, ELF::SHT_PROGBITS, 0));
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000709
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000710 // NaN: At the moment we only support:
711 // 1. .nan legacy (default)
712 // 2. .nan 2008
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000713 STI.isNaN2008() ? TS.emitDirectiveNaN2008()
714 : TS.emitDirectiveNaNLegacy();
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000715
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000716 // TODO: handle O64 ABI
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000717
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000718 TS.updateABIInfo(STI);
Daniel Sanders7e527422014-07-10 13:38:23 +0000719
Daniel Sanderse22244b2014-07-21 15:25:24 +0000720 // We should always emit a '.module fp=...' but binutils 2.24 does not accept
721 // it. We therefore emit it when it contradicts the ABI defaults (-mfpxx or
722 // -mfp64) and omit it otherwise.
Eric Christopher8af49b32015-02-18 01:01:57 +0000723 if (ABI.IsO32() && (STI.isABI_FPXX() || STI.isFP64bit()))
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000724 TS.emitDirectiveModuleFP();
Daniel Sanderse22244b2014-07-21 15:25:24 +0000725
726 // We should always emit a '.module [no]oddspreg' but binutils 2.24 does not
727 // accept it. We therefore emit it when it contradicts the default or an
728 // option has changed the default (i.e. FPXX) and omit it otherwise.
Eric Christopher8af49b32015-02-18 01:01:57 +0000729 if (ABI.IsO32() && (!STI.useOddSPReg() || STI.isABI_FPXX()))
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000730 TS.emitDirectiveModuleOddSPReg();
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000731}
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000732
Eric Christopher64d35be2015-02-19 19:52:25 +0000733void MipsAsmPrinter::emitInlineAsmStart() const {
Toma Tabacua23f13c2014-12-17 10:56:16 +0000734 MipsTargetStreamer &TS = getTargetStreamer();
735
Toma Tabacu68e8a9c2015-01-09 15:00:30 +0000736 // GCC's choice of assembler options for inline assembly code ('at', 'macro'
737 // and 'reorder') is different from LLVM's choice for generated code ('noat',
738 // 'nomacro' and 'noreorder').
739 // In order to maintain compatibility with inline assembly code which depends
740 // on GCC's assembler options being used, we have to switch to those options
741 // for the duration of the inline assembly block and then switch back.
Toma Tabacua23f13c2014-12-17 10:56:16 +0000742 TS.emitDirectiveSetPush();
743 TS.emitDirectiveSetAt();
744 TS.emitDirectiveSetMacro();
745 TS.emitDirectiveSetReorder();
Lang Hames9ff69c82015-04-24 19:11:51 +0000746 OutStreamer->AddBlankLine();
Toma Tabacua23f13c2014-12-17 10:56:16 +0000747}
748
749void MipsAsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
750 const MCSubtargetInfo *EndInfo) const {
Lang Hames9ff69c82015-04-24 19:11:51 +0000751 OutStreamer->AddBlankLine();
Toma Tabacua23f13c2014-12-17 10:56:16 +0000752 getTargetStreamer().emitDirectiveSetPop();
753}
754
Eric Christopher327fc972015-02-21 08:48:22 +0000755void MipsAsmPrinter::EmitJal(const MCSubtargetInfo &STI, MCSymbol *Symbol) {
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000756 MCInst I;
757 I.setOpcode(Mips::JAL);
758 I.addOperand(
Jim Grosbach13760bd2015-05-30 01:25:56 +0000759 MCOperand::createExpr(MCSymbolRefExpr::create(Symbol, OutContext)));
Lang Hames9ff69c82015-04-24 19:11:51 +0000760 OutStreamer->EmitInstruction(I, STI);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000761}
762
Eric Christopher327fc972015-02-21 08:48:22 +0000763void MipsAsmPrinter::EmitInstrReg(const MCSubtargetInfo &STI, unsigned Opcode,
764 unsigned Reg) {
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000765 MCInst I;
766 I.setOpcode(Opcode);
Jim Grosbache9119e42015-05-13 18:37:00 +0000767 I.addOperand(MCOperand::createReg(Reg));
Lang Hames9ff69c82015-04-24 19:11:51 +0000768 OutStreamer->EmitInstruction(I, STI);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000769}
770
Eric Christopher327fc972015-02-21 08:48:22 +0000771void MipsAsmPrinter::EmitInstrRegReg(const MCSubtargetInfo &STI,
772 unsigned Opcode, unsigned Reg1,
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000773 unsigned Reg2) {
774 MCInst I;
775 //
776 // Because of the current td files for Mips32, the operands for MTC1
777 // appear backwards from their normal assembly order. It's not a trivial
778 // change to fix this in the td file so we adjust for it here.
779 //
780 if (Opcode == Mips::MTC1) {
781 unsigned Temp = Reg1;
782 Reg1 = Reg2;
783 Reg2 = Temp;
784 }
785 I.setOpcode(Opcode);
Jim Grosbache9119e42015-05-13 18:37:00 +0000786 I.addOperand(MCOperand::createReg(Reg1));
787 I.addOperand(MCOperand::createReg(Reg2));
Lang Hames9ff69c82015-04-24 19:11:51 +0000788 OutStreamer->EmitInstruction(I, STI);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000789}
790
Eric Christopher327fc972015-02-21 08:48:22 +0000791void MipsAsmPrinter::EmitInstrRegRegReg(const MCSubtargetInfo &STI,
792 unsigned Opcode, unsigned Reg1,
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000793 unsigned Reg2, unsigned Reg3) {
794 MCInst I;
795 I.setOpcode(Opcode);
Jim Grosbache9119e42015-05-13 18:37:00 +0000796 I.addOperand(MCOperand::createReg(Reg1));
797 I.addOperand(MCOperand::createReg(Reg2));
798 I.addOperand(MCOperand::createReg(Reg3));
Lang Hames9ff69c82015-04-24 19:11:51 +0000799 OutStreamer->EmitInstruction(I, STI);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000800}
801
Eric Christopher327fc972015-02-21 08:48:22 +0000802void MipsAsmPrinter::EmitMovFPIntPair(const MCSubtargetInfo &STI,
803 unsigned MovOpc, unsigned Reg1,
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000804 unsigned Reg2, unsigned FPReg1,
805 unsigned FPReg2, bool LE) {
806 if (!LE) {
807 unsigned temp = Reg1;
808 Reg1 = Reg2;
809 Reg2 = temp;
810 }
Eric Christopher327fc972015-02-21 08:48:22 +0000811 EmitInstrRegReg(STI, MovOpc, Reg1, FPReg1);
812 EmitInstrRegReg(STI, MovOpc, Reg2, FPReg2);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000813}
814
Eric Christopher327fc972015-02-21 08:48:22 +0000815void MipsAsmPrinter::EmitSwapFPIntParams(const MCSubtargetInfo &STI,
816 Mips16HardFloatInfo::FPParamVariant PV,
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000817 bool LE, bool ToFP) {
818 using namespace Mips16HardFloatInfo;
819 unsigned MovOpc = ToFP ? Mips::MTC1 : Mips::MFC1;
820 switch (PV) {
821 case FSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000822 EmitInstrRegReg(STI, MovOpc, Mips::A0, Mips::F12);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000823 break;
824 case FFSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000825 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F14, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000826 break;
827 case FDSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000828 EmitInstrRegReg(STI, MovOpc, Mips::A0, Mips::F12);
829 EmitMovFPIntPair(STI, MovOpc, Mips::A2, Mips::A3, Mips::F14, Mips::F15, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000830 break;
831 case DSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000832 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000833 break;
834 case DDSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000835 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
836 EmitMovFPIntPair(STI, MovOpc, Mips::A2, Mips::A3, Mips::F14, Mips::F15, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000837 break;
838 case DFSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000839 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
840 EmitInstrRegReg(STI, MovOpc, Mips::A2, Mips::F14);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000841 break;
842 case NoSig:
843 return;
844 }
845}
846
Eric Christopher327fc972015-02-21 08:48:22 +0000847void MipsAsmPrinter::EmitSwapFPIntRetval(
848 const MCSubtargetInfo &STI, Mips16HardFloatInfo::FPReturnVariant RV,
849 bool LE) {
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000850 using namespace Mips16HardFloatInfo;
851 unsigned MovOpc = Mips::MFC1;
852 switch (RV) {
853 case FRet:
Eric Christopher327fc972015-02-21 08:48:22 +0000854 EmitInstrRegReg(STI, MovOpc, Mips::V0, Mips::F0);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000855 break;
856 case DRet:
Eric Christopher327fc972015-02-21 08:48:22 +0000857 EmitMovFPIntPair(STI, MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000858 break;
859 case CFRet:
Eric Christopher327fc972015-02-21 08:48:22 +0000860 EmitMovFPIntPair(STI, MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000861 break;
862 case CDRet:
Eric Christopher327fc972015-02-21 08:48:22 +0000863 EmitMovFPIntPair(STI, MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
864 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F2, Mips::F3, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000865 break;
866 case NoFPRet:
867 break;
868 }
869}
870
871void MipsAsmPrinter::EmitFPCallStub(
872 const char *Symbol, const Mips16HardFloatInfo::FuncSignature *Signature) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000873 MCSymbol *MSymbol = OutContext.getOrCreateSymbol(StringRef(Symbol));
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000874 using namespace Mips16HardFloatInfo;
Eric Christopherbb401642015-02-21 08:32:22 +0000875 bool LE = getDataLayout().isLittleEndian();
Eric Christopher327fc972015-02-21 08:48:22 +0000876 // Construct a local MCSubtargetInfo here.
877 // This is because the MachineFunction won't exist (but have not yet been
878 // freed) and since we're at the global level we can use the default
879 // constructed subtarget.
880 std::unique_ptr<MCSubtargetInfo> STI(TM.getTarget().createMCSubtargetInfo(
Daniel Sanders335487a2015-06-16 13:15:50 +0000881 TM.getTargetTriple().str(), TM.getTargetCPU(),
882 TM.getTargetFeatureString()));
Eric Christopher327fc972015-02-21 08:48:22 +0000883
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000884 //
885 // .global xxxx
886 //
Lang Hames9ff69c82015-04-24 19:11:51 +0000887 OutStreamer->EmitSymbolAttribute(MSymbol, MCSA_Global);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000888 const char *RetType;
889 //
890 // make the comment field identifying the return and parameter
891 // types of the floating point stub
892 // # Stub function to call rettype xxxx (params)
893 //
894 switch (Signature->RetSig) {
895 case FRet:
896 RetType = "float";
897 break;
898 case DRet:
899 RetType = "double";
900 break;
901 case CFRet:
902 RetType = "complex";
903 break;
904 case CDRet:
905 RetType = "double complex";
906 break;
907 case NoFPRet:
908 RetType = "";
909 break;
910 }
911 const char *Parms;
912 switch (Signature->ParamSig) {
913 case FSig:
914 Parms = "float";
915 break;
916 case FFSig:
917 Parms = "float, float";
918 break;
919 case FDSig:
920 Parms = "float, double";
921 break;
922 case DSig:
923 Parms = "double";
924 break;
925 case DDSig:
926 Parms = "double, double";
927 break;
928 case DFSig:
929 Parms = "double, float";
930 break;
931 case NoSig:
932 Parms = "";
933 break;
934 }
Lang Hames9ff69c82015-04-24 19:11:51 +0000935 OutStreamer->AddComment("\t# Stub function to call " + Twine(RetType) + " " +
936 Twine(Symbol) + " (" + Twine(Parms) + ")");
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000937 //
938 // probably not necessary but we save and restore the current section state
939 //
Lang Hames9ff69c82015-04-24 19:11:51 +0000940 OutStreamer->PushSection();
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000941 //
942 // .section mips16.call.fpxxxx,"ax",@progbits
943 //
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000944 MCSectionELF *M = OutContext.getELFSection(
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000945 ".mips16.call.fp." + std::string(Symbol), ELF::SHT_PROGBITS,
Rafael Espindolaba31e272015-01-29 17:33:21 +0000946 ELF::SHF_ALLOC | ELF::SHF_EXECINSTR);
Lang Hames9ff69c82015-04-24 19:11:51 +0000947 OutStreamer->SwitchSection(M, nullptr);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000948 //
949 // .align 2
950 //
Lang Hames9ff69c82015-04-24 19:11:51 +0000951 OutStreamer->EmitValueToAlignment(4);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000952 MipsTargetStreamer &TS = getTargetStreamer();
953 //
954 // .set nomips16
955 // .set nomicromips
956 //
957 TS.emitDirectiveSetNoMips16();
958 TS.emitDirectiveSetNoMicroMips();
959 //
960 // .ent __call_stub_fp_xxxx
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000961 // .type __call_stub_fp_xxxx,@function
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000962 // __call_stub_fp_xxxx:
963 //
964 std::string x = "__call_stub_fp_" + std::string(Symbol);
Rafael Espindolaa8695762015-06-02 00:25:12 +0000965 MCSymbolELF *Stub =
966 cast<MCSymbolELF>(OutContext.getOrCreateSymbol(StringRef(x)));
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000967 TS.emitDirectiveEnt(*Stub);
968 MCSymbol *MType =
Jim Grosbach6f482002015-05-18 18:43:14 +0000969 OutContext.getOrCreateSymbol("__call_stub_fp_" + Twine(Symbol));
Lang Hames9ff69c82015-04-24 19:11:51 +0000970 OutStreamer->EmitSymbolAttribute(MType, MCSA_ELF_TypeFunction);
971 OutStreamer->EmitLabel(Stub);
Eric Christopherd5bc07e2015-02-21 08:32:38 +0000972
973 // Only handle non-pic for now.
Rafael Espindolab0f59cb2016-06-27 17:21:46 +0000974 assert(!isPositionIndependent() &&
Eric Christopherd5bc07e2015-02-21 08:32:38 +0000975 "should not be here if we are compiling pic");
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000976 TS.emitDirectiveSetReorder();
977 //
978 // We need to add a MipsMCExpr class to MCTargetDesc to fully implement
979 // stubs without raw text but this current patch is for compiler generated
980 // functions and they all return some value.
981 // The calling sequence for non pic is different in that case and we need
982 // to implement %lo and %hi in order to handle the case of no return value
983 // See the corresponding method in Mips16HardFloat for details.
984 //
985 // mov the return address to S2.
986 // we have no stack space to store it and we are about to make another call.
987 // We need to make sure that the enclosing function knows to save S2
988 // This should have already been handled.
989 //
990 // Mov $18, $31
991
Vasileios Kalintiris1c78ca62015-08-11 08:56:25 +0000992 EmitInstrRegRegReg(*STI, Mips::OR, Mips::S2, Mips::RA, Mips::ZERO);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000993
Eric Christopher327fc972015-02-21 08:48:22 +0000994 EmitSwapFPIntParams(*STI, Signature->ParamSig, LE, true);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000995
996 // Jal xxxx
997 //
Eric Christopher327fc972015-02-21 08:48:22 +0000998 EmitJal(*STI, MSymbol);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000999
1000 // fix return values
Eric Christopher327fc972015-02-21 08:48:22 +00001001 EmitSwapFPIntRetval(*STI, Signature->RetSig, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001002 //
1003 // do the return
1004 // if (Signature->RetSig == NoFPRet)
1005 // llvm_unreachable("should not be any stubs here with no return value");
1006 // else
Eric Christopher327fc972015-02-21 08:48:22 +00001007 EmitInstrReg(*STI, Mips::JR, Mips::S2);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001008
Jim Grosbach6f482002015-05-18 18:43:14 +00001009 MCSymbol *Tmp = OutContext.createTempSymbol();
Lang Hames9ff69c82015-04-24 19:11:51 +00001010 OutStreamer->EmitLabel(Tmp);
Jim Grosbach13760bd2015-05-30 01:25:56 +00001011 const MCSymbolRefExpr *E = MCSymbolRefExpr::create(Stub, OutContext);
1012 const MCSymbolRefExpr *T = MCSymbolRefExpr::create(Tmp, OutContext);
1013 const MCExpr *T_min_E = MCBinaryExpr::createSub(T, E, OutContext);
Rafael Espindolaa8695762015-06-02 00:25:12 +00001014 OutStreamer->emitELFSize(Stub, T_min_E);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001015 TS.emitDirectiveEnd(x);
Lang Hames9ff69c82015-04-24 19:11:51 +00001016 OutStreamer->PopSection();
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001017}
1018
1019void MipsAsmPrinter::EmitEndOfAsmFile(Module &M) {
1020 // Emit needed stubs
1021 //
1022 for (std::map<
1023 const char *,
1024 const llvm::Mips16HardFloatInfo::FuncSignature *>::const_iterator
1025 it = StubsNeeded.begin();
1026 it != StubsNeeded.end(); ++it) {
1027 const char *Symbol = it->first;
1028 const llvm::Mips16HardFloatInfo::FuncSignature *Signature = it->second;
1029 EmitFPCallStub(Symbol, Signature);
1030 }
Rafael Espindola2ab7ea72014-01-27 01:33:33 +00001031 // return to the text section
Lang Hames9ff69c82015-04-24 19:11:51 +00001032 OutStreamer->SwitchSection(OutContext.getObjectFileInfo()->getTextSection());
Jack Carterc1b17ed2013-01-18 21:20:38 +00001033}
1034
Akira Hatanakaf2bcad92011-07-01 01:04:43 +00001035void MipsAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
1036 raw_ostream &OS) {
1037 // TODO: implement
1038}
1039
Sasa Stankovic8c5736b2014-02-28 10:00:38 +00001040// Align all targets of indirect branches on bundle size. Used only if target
1041// is NaCl.
1042void MipsAsmPrinter::NaClAlignIndirectJumpTargets(MachineFunction &MF) {
1043 // Align all blocks that are jumped to through jump table.
1044 if (MachineJumpTableInfo *JtInfo = MF.getJumpTableInfo()) {
1045 const std::vector<MachineJumpTableEntry> &JT = JtInfo->getJumpTables();
1046 for (unsigned I = 0; I < JT.size(); ++I) {
1047 const std::vector<MachineBasicBlock*> &MBBs = JT[I].MBBs;
1048
1049 for (unsigned J = 0; J < MBBs.size(); ++J)
1050 MBBs[J]->setAlignment(MIPS_NACL_BUNDLE_ALIGN);
1051 }
1052 }
1053
1054 // If basic block address is taken, block can be target of indirect branch.
Vasileios Kalintiris5a971a42016-04-15 20:43:17 +00001055 for (auto &MBB : MF) {
1056 if (MBB.hasAddressTaken())
1057 MBB.setAlignment(MIPS_NACL_BUNDLE_ALIGN);
Sasa Stankovic8c5736b2014-02-28 10:00:38 +00001058 }
1059}
1060
Sasa Stankovic7b061a42014-04-30 15:06:25 +00001061bool MipsAsmPrinter::isLongBranchPseudo(int Opcode) const {
1062 return (Opcode == Mips::LONG_BRANCH_LUi
1063 || Opcode == Mips::LONG_BRANCH_ADDiu
Sasa Stankovic7b061a42014-04-30 15:06:25 +00001064 || Opcode == Mips::LONG_BRANCH_DADDiu);
1065}
1066
Bob Wilson5a495fe2009-06-23 23:59:40 +00001067// Force static initialization.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +00001068extern "C" void LLVMInitializeMipsAsmPrinter() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +00001069 RegisterAsmPrinter<MipsAsmPrinter> X(TheMipsTarget);
1070 RegisterAsmPrinter<MipsAsmPrinter> Y(TheMipselTarget);
Akira Hatanaka3d673cc2011-09-21 03:00:58 +00001071 RegisterAsmPrinter<MipsAsmPrinter> A(TheMips64Target);
1072 RegisterAsmPrinter<MipsAsmPrinter> B(TheMips64elTarget);
Daniel Dunbare8338102009-07-15 20:24:03 +00001073}