blob: 4f3729c7cb5f9e1e2fcba3901788f3ffdfde378f [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 {
Rafael Espindola4a1a3602014-01-14 01:21:46 +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 Christopher8ef7a6a2014-07-18 00:08:53 +000062 Subtarget = &TM.getSubtarget<MipsSubtarget>();
63
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;
124 TmpInst0.addOperand(MCOperand::CreateReg(ZeroReg));
125 }
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) {
147 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
148 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) {
163 OutStreamer.EmitDataRegion(MCDR_DataRegion);
164 InConstantPool = true;
165 }
166
167 OutStreamer.EmitLabel(GetCPISymbol(LabelId));
168
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.
183 if (emitPseudoExpansionLowering(OutStreamer, &*I))
184 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) {
190 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);
David Woodhousee6c13e42014-01-28 23:12:42 +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();
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000255 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000256 // size of stack area to which FP callee-saved regs are saved.
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000257 unsigned CPURegSize = Mips::GPR32RegClass.getSize();
Craig Topperc7242e02012-04-20 07:30:17 +0000258 unsigned FGR32RegSize = Mips::FGR32RegClass.getSize();
259 unsigned AFGR64RegSize = Mips::AFGR64RegClass.getSize();
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000260 bool HasAFGR64Reg = false;
261 unsigned CSFPRegsSize = 0;
262 unsigned i, e = CSI.size();
263
264 // Set FPU Bitmask.
265 for (i = 0; i != e; ++i) {
Rafael Espindolaf2dffce2010-06-02 20:02:30 +0000266 unsigned Reg = CSI[i].getReg();
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000267 if (Mips::GPR32RegClass.contains(Reg))
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000268 break;
269
Eric Christopherd9134482014-08-04 21:25:23 +0000270 unsigned RegNum =
271 TM.getSubtargetImpl()->getRegisterInfo()->getEncodingValue(Reg);
Craig Topperc7242e02012-04-20 07:30:17 +0000272 if (Mips::AFGR64RegClass.contains(Reg)) {
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000273 FPUBitmask |= (3 << RegNum);
274 CSFPRegsSize += AFGR64RegSize;
275 HasAFGR64Reg = true;
276 continue;
277 }
278
279 FPUBitmask |= (1 << RegNum);
280 CSFPRegsSize += FGR32RegSize;
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000281 }
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000282
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000283 // Set CPU Bitmask.
284 for (; i != e; ++i) {
285 unsigned Reg = CSI[i].getReg();
Eric Christopherd9134482014-08-04 21:25:23 +0000286 unsigned RegNum =
287 TM.getSubtargetImpl()->getRegisterInfo()->getEncodingValue(Reg);
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000288 CPUBitmask |= (1 << RegNum);
289 }
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000290
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000291 // FP Regs are saved right below where the virtual frame pointer points to.
292 FPUTopSavedRegOff = FPUBitmask ?
293 (HasAFGR64Reg ? -AFGR64RegSize : -FGR32RegSize) : 0;
294
295 // CPU Regs are saved below FP Regs.
296 CPUTopSavedRegOff = CPUBitmask ? -CSFPRegsSize - CPURegSize : 0;
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000297
Rafael Espindola25fa2912014-01-27 04:33:11 +0000298 MipsTargetStreamer &TS = getTargetStreamer();
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000299 // Print CPUBitmask
Rafael Espindola25fa2912014-01-27 04:33:11 +0000300 TS.emitMask(CPUBitmask, CPUTopSavedRegOff);
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000301
302 // Print FPUBitmask
Rafael Espindola25fa2912014-01-27 04:33:11 +0000303 TS.emitFMask(FPUBitmask, FPUTopSavedRegOff);
Bruno Cardoso Lopesbcda5e22007-07-11 23:24:41 +0000304}
305
Akira Hatanakae2489122011-04-15 21:51:11 +0000306//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000307// Frame and Set directives
Akira Hatanakae2489122011-04-15 21:51:11 +0000308//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000309
310/// Frame Directive
Chris Lattner5e596182010-04-04 07:05:53 +0000311void MipsAsmPrinter::emitFrameDirective() {
Eric Christopherd9134482014-08-04 21:25:23 +0000312 const TargetRegisterInfo &RI = *TM.getSubtargetImpl()->getRegisterInfo();
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000313
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000314 unsigned stackReg = RI.getFrameRegister(*MF);
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000315 unsigned returnReg = RI.getRARegister();
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000316 unsigned stackSize = MF->getFrameInfo()->getStackSize();
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000317
Rafael Espindola054234f2014-01-27 03:53:56 +0000318 getTargetStreamer().emitFrame(stackReg, stackSize, returnReg);
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000319}
320
321/// Emit Set directives.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000322const char *MipsAsmPrinter::getCurrentABIString() const {
Eric Christophera5762812015-01-26 17:33:46 +0000323 switch (static_cast<MipsTargetMachine &>(TM).getABI().GetEnumValue()) {
Daniel Sanderse2e25da2014-10-24 16:15:27 +0000324 case MipsABIInfo::ABI::O32: return "abi32";
325 case MipsABIInfo::ABI::N32: return "abiN32";
326 case MipsABIInfo::ABI::N64: return "abi64";
327 case MipsABIInfo::ABI::EABI: return "eabi32"; // TODO: handle eabi64
Dmitri Gribenkoca1e27b2012-09-10 21:26:47 +0000328 default: llvm_unreachable("Unknown Mips ABI");
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000329 }
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000330}
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000331
Chris Lattner5d9fb4b2010-01-27 23:23:58 +0000332void MipsAsmPrinter::EmitFunctionEntryLabel() {
Rafael Espindola6633d572014-01-14 18:57:12 +0000333 MipsTargetStreamer &TS = getTargetStreamer();
Sasa Stankovic8c5736b2014-02-28 10:00:38 +0000334
335 // NaCl sandboxing requires that indirect call instructions are masked.
336 // This means that function entry points should be bundle-aligned.
337 if (Subtarget->isTargetNaCl())
338 EmitAlignment(std::max(MF->getAlignment(), MIPS_NACL_BUNDLE_ALIGN));
339
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000340 if (Subtarget->inMicroMipsMode())
Rafael Espindola6633d572014-01-14 18:57:12 +0000341 TS.emitDirectiveSetMicroMips();
Matheus Almeidadc7e48e2014-04-16 11:46:59 +0000342 else
343 TS.emitDirectiveSetNoMicroMips();
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000344
Rafael Espindola6633d572014-01-14 18:57:12 +0000345 if (Subtarget->inMips16Mode())
346 TS.emitDirectiveSetMips16();
347 else
348 TS.emitDirectiveSetNoMips16();
Jack Carterab3cb422013-02-19 22:04:37 +0000349
Rafael Espindola6633d572014-01-14 18:57:12 +0000350 TS.emitDirectiveEnt(*CurrentFnSym);
Chris Lattner5d9fb4b2010-01-27 23:23:58 +0000351 OutStreamer.EmitLabel(CurrentFnSym);
352}
353
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000354/// EmitFunctionBodyStart - Targets can override this to emit stuff before
355/// the first basic block in the function.
356void MipsAsmPrinter::EmitFunctionBodyStart() {
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000357 MipsTargetStreamer &TS = getTargetStreamer();
358
Rafael Espindola7d78b2a2013-10-29 16:24:21 +0000359 MCInstLowering.Initialize(&MF->getContext());
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +0000360
Reed Kotler0f2b10e2013-05-03 23:17:24 +0000361 bool IsNakedFunction =
362 MF->getFunction()->
363 getAttributes().hasAttribute(AttributeSet::FunctionIndex,
364 Attribute::Naked);
365 if (!IsNakedFunction)
366 emitFrameDirective();
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000367
Rafael Espindola25fa2912014-01-27 04:33:11 +0000368 if (!IsNakedFunction)
369 printSavedRegsBitmask();
370
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000371 if (!Subtarget->inMips16Mode()) {
372 TS.emitDirectiveSetNoReorder();
373 TS.emitDirectiveSetNoMacro();
374 TS.emitDirectiveSetNoAt();
Akira Hatanaka8f3573032012-05-12 00:48:43 +0000375 }
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000376}
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000377
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000378/// EmitFunctionBodyEnd - Targets can override this to emit stuff after
379/// the last basic block in the function.
380void MipsAsmPrinter::EmitFunctionBodyEnd() {
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000381 MipsTargetStreamer &TS = getTargetStreamer();
382
Chris Lattnerfd97a332010-01-28 01:48:52 +0000383 // There are instruction for this macros, but they must
384 // always be at the function end, and we can't emit and
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000385 // break with BB logic.
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000386 if (!Subtarget->inMips16Mode()) {
387 TS.emitDirectiveSetAt();
388 TS.emitDirectiveSetMacro();
389 TS.emitDirectiveSetReorder();
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000390 }
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000391 TS.emitDirectiveEnd(CurrentFnSym->getName());
Reed Kotler91ae9822013-10-27 21:57:36 +0000392 // Make sure to terminate any constant pools that were at the end
393 // of the function.
394 if (!InConstantPool)
395 return;
396 InConstantPool = false;
397 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000398}
399
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000400/// isBlockOnlyReachableByFallthough - Return true if the basic block has
401/// exactly one predecessor and the control transfer mechanism between
402/// the predecessor and this block is a fall-through.
Akira Hatanakae2489122011-04-15 21:51:11 +0000403bool MipsAsmPrinter::isBlockOnlyReachableByFallthrough(const MachineBasicBlock*
404 MBB) const {
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000405 // The predecessor has to be immediately before this block.
406 const MachineBasicBlock *Pred = *MBB->pred_begin();
407
408 // If the predecessor is a switch statement, assume a jump table
409 // implementation, so it is not a fall through.
410 if (const BasicBlock *bb = Pred->getBasicBlock())
411 if (isa<SwitchInst>(bb->getTerminator()))
412 return false;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000413
Akira Hatanakae625ba42011-04-01 18:57:38 +0000414 // If this is a landing pad, it isn't a fall through. If it has no preds,
415 // then nothing falls through to it.
416 if (MBB->isLandingPad() || MBB->pred_empty())
417 return false;
418
419 // If there isn't exactly one predecessor, it can't be a fall through.
420 MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI;
421 ++PI2;
Jia Liuf54f60f2012-02-28 07:46:26 +0000422
Akira Hatanakae625ba42011-04-01 18:57:38 +0000423 if (PI2 != MBB->pred_end())
Jia Liuf54f60f2012-02-28 07:46:26 +0000424 return false;
Akira Hatanakae625ba42011-04-01 18:57:38 +0000425
426 // The predecessor has to be immediately before this block.
427 if (!Pred->isLayoutSuccessor(MBB))
428 return false;
Jia Liuf54f60f2012-02-28 07:46:26 +0000429
Akira Hatanakae625ba42011-04-01 18:57:38 +0000430 // If the block is completely empty, then it definitely does fall through.
431 if (Pred->empty())
432 return true;
Jia Liuf54f60f2012-02-28 07:46:26 +0000433
Akira Hatanakae625ba42011-04-01 18:57:38 +0000434 // Otherwise, check the last instruction.
435 // Check if the last terminator is an unconditional branch.
436 MachineBasicBlock::const_iterator I = Pred->end();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000437 while (I != Pred->begin() && !(--I)->isTerminator()) ;
Akira Hatanakae625ba42011-04-01 18:57:38 +0000438
Evan Cheng7f8e5632011-12-07 07:15:52 +0000439 return !I->isBarrier();
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000440}
441
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000442// Print out an operand for an inline asm expression.
Eric Christophered51b9e2012-05-10 21:48:22 +0000443bool MipsAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Chris Lattner3bb09762010-04-04 05:29:35 +0000444 unsigned AsmVariant,const char *ExtraCode,
445 raw_ostream &O) {
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000446 // Does this asm operand have a single letter operand modifier?
Eric Christophered51b9e2012-05-10 21:48:22 +0000447 if (ExtraCode && ExtraCode[0]) {
448 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000449
Eric Christophered51b9e2012-05-10 21:48:22 +0000450 const MachineOperand &MO = MI->getOperand(OpNum);
451 switch (ExtraCode[0]) {
Eric Christopherbc5d2492012-05-19 00:51:56 +0000452 default:
Jack Carterb2fd5f62012-06-21 17:14:46 +0000453 // See if this is a generic print operand
454 return AsmPrinter::PrintAsmOperand(MI,OpNum,AsmVariant,ExtraCode,O);
Eric Christopherbc5d2492012-05-19 00:51:56 +0000455 case 'X': // hex const int
456 if ((MO.getType()) != MachineOperand::MO_Immediate)
457 return true;
458 O << "0x" << StringRef(utohexstr(MO.getImm())).lower();
459 return false;
460 case 'x': // hex const int (low 16 bits)
461 if ((MO.getType()) != MachineOperand::MO_Immediate)
462 return true;
463 O << "0x" << StringRef(utohexstr(MO.getImm() & 0xffff)).lower();
464 return false;
465 case 'd': // decimal const int
466 if ((MO.getType()) != MachineOperand::MO_Immediate)
467 return true;
468 O << MO.getImm();
469 return false;
Eric Christopherf481ab32012-05-30 19:05:19 +0000470 case 'm': // decimal const int minus 1
471 if ((MO.getType()) != MachineOperand::MO_Immediate)
472 return true;
473 O << MO.getImm() - 1;
474 return false;
Jack Carter27747b52012-06-28 20:46:26 +0000475 case 'z': {
476 // $0 if zero, regular printing otherwise
Toma Tabacu27cab752014-11-06 14:25:42 +0000477 if (MO.getType() == MachineOperand::MO_Immediate && MO.getImm() == 0) {
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000478 O << "$0";
Toma Tabacu27cab752014-11-06 14:25:42 +0000479 return false;
480 }
481 // If not, call printOperand as normal.
482 break;
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000483 }
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000484 case 'D': // Second part of a double word register operand
485 case 'L': // Low order register of a double word register operand
Jack Cartera62ba822012-07-18 06:41:36 +0000486 case 'M': // High order register of a double word register operand
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000487 {
Jack Carterb2af5122012-07-05 23:58:21 +0000488 if (OpNum == 0)
489 return true;
490 const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
491 if (!FlagsOP.isImm())
492 return true;
493 unsigned Flags = FlagsOP.getImm();
494 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Jack Carter2ab73b12012-07-06 02:44:22 +0000495 // Number of registers represented by this operand. We are looking
496 // for 2 for 32 bit mode and 1 for 64 bit mode.
Jack Carterb2af5122012-07-05 23:58:21 +0000497 if (NumVals != 2) {
Jack Carter2ab73b12012-07-06 02:44:22 +0000498 if (Subtarget->isGP64bit() && NumVals == 1 && MO.isReg()) {
Jack Carterb2af5122012-07-05 23:58:21 +0000499 unsigned Reg = MO.getReg();
500 O << '$' << MipsInstPrinter::getRegisterName(Reg);
501 return false;
502 }
503 return true;
504 }
Jack Carter42ebf982012-07-11 21:41:49 +0000505
506 unsigned RegOp = OpNum;
507 if (!Subtarget->isGP64bit()){
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000508 // Endianess reverses which register holds the high or low value
Jack Cartera62ba822012-07-18 06:41:36 +0000509 // between M and L.
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000510 switch(ExtraCode[0]) {
Jack Cartera62ba822012-07-18 06:41:36 +0000511 case 'M':
512 RegOp = (Subtarget->isLittle()) ? OpNum + 1 : OpNum;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000513 break;
514 case 'L':
Jack Cartera62ba822012-07-18 06:41:36 +0000515 RegOp = (Subtarget->isLittle()) ? OpNum : OpNum + 1;
516 break;
517 case 'D': // Always the second part
518 RegOp = OpNum + 1;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000519 }
520 if (RegOp >= MI->getNumOperands())
521 return true;
522 const MachineOperand &MO = MI->getOperand(RegOp);
523 if (!MO.isReg())
524 return true;
525 unsigned Reg = MO.getReg();
526 O << '$' << MipsInstPrinter::getRegisterName(Reg);
527 return false;
Jack Carterb2af5122012-07-05 23:58:21 +0000528 }
Eric Christophered51b9e2012-05-10 21:48:22 +0000529 }
Daniel Sanders8b59af12013-11-12 12:56:01 +0000530 case 'w':
531 // Print MSA registers for the 'f' constraint
532 // In LLVM, the 'w' modifier doesn't need to do anything.
533 // We can just call printOperand as normal.
534 break;
Jack Carter2ab73b12012-07-06 02:44:22 +0000535 }
536 }
Eric Christophered51b9e2012-05-10 21:48:22 +0000537
538 printOperand(MI, OpNum, O);
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000539 return false;
540}
541
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000542bool MipsAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
543 unsigned OpNum, unsigned AsmVariant,
544 const char *ExtraCode,
545 raw_ostream &O) {
Jack Carterb04e3572013-04-09 23:19:50 +0000546 int Offset = 0;
547 // Currently we are expecting either no ExtraCode or 'D'
548 if (ExtraCode) {
549 if (ExtraCode[0] == 'D')
550 Offset = 4;
551 else
552 return true; // Unknown modifier.
553 }
Jia Liuf54f60f2012-02-28 07:46:26 +0000554
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000555 const MachineOperand &MO = MI->getOperand(OpNum);
556 assert(MO.isReg() && "unexpected inline asm memory operand");
Jack Carterb04e3572013-04-09 23:19:50 +0000557 O << Offset << "($" << MipsInstPrinter::getRegisterName(MO.getReg()) << ")";
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000558
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000559 return false;
560}
561
Chris Lattner76c564b2010-04-04 04:47:45 +0000562void MipsAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
563 raw_ostream &O) {
Eric Christopher8b770652015-01-26 19:03:15 +0000564 const DataLayout *DL = TM.getDataLayout();
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:
Chris Lattner29bdac42010-03-13 21:04:28 +0000599 O << *MO.getMBB()->getSymbol();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000600 return;
601
602 case MachineOperand::MO_GlobalAddress:
Rafael Espindola79858aa2013-10-29 17:07:16 +0000603 O << *getSymbol(MO.getGlobal());
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:
Rafael Espindola58873562014-01-03 19:21:54 +0000613 O << DL->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
Chris Lattner76c564b2010-04-04 04:47:45 +0000626void MipsAsmPrinter::printUnsignedImm(const MachineInstr *MI, int opNum,
627 raw_ostream &O) {
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000628 const MachineOperand &MO = MI->getOperand(opNum);
Devang Patel12f68552010-04-27 22:24:37 +0000629 if (MO.isImm())
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000630 O << (unsigned short int)MO.getImm();
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000631 else
Chris Lattner76c564b2010-04-04 04:47:45 +0000632 printOperand(MI, opNum, O);
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000633}
634
Daniel Sanders3f6eb542013-11-12 10:45:18 +0000635void MipsAsmPrinter::printUnsignedImm8(const MachineInstr *MI, int opNum,
636 raw_ostream &O) {
637 const MachineOperand &MO = MI->getOperand(opNum);
638 if (MO.isImm())
639 O << (unsigned short int)(unsigned char)MO.getImm();
640 else
641 printOperand(MI, opNum, O);
642}
643
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000644void MipsAsmPrinter::
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000645printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O) {
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000646 // Load/Store memory operands -- imm($reg)
647 // If PIC target the target is loaded as the
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +0000648 // pattern lw $25,%call16($28)
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000649
650 // opNum can be invalid if instruction has reglist as operand.
651 // MemOperand is always last operand of instruction (base + offset).
652 switch (MI->getOpcode()) {
653 default:
654 break;
655 case Mips::SWM32_MM:
656 case Mips::LWM32_MM:
657 opNum = MI->getNumOperands() - 2;
658 break;
659 }
660
Chris Lattner76c564b2010-04-04 04:47:45 +0000661 printOperand(MI, opNum+1, O);
Akira Hatanaka2e766ed2011-07-07 18:57:00 +0000662 O << "(";
663 printOperand(MI, opNum, O);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000664 O << ")";
665}
666
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000667void MipsAsmPrinter::
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000668printMemOperandEA(const MachineInstr *MI, int opNum, raw_ostream &O) {
669 // when using stack locations for not load/store instructions
670 // print the same way as all normal 3 operand instructions.
671 printOperand(MI, opNum, O);
672 O << ", ";
673 printOperand(MI, opNum+1, O);
674 return;
675}
676
677void MipsAsmPrinter::
Chris Lattner76c564b2010-04-04 04:47:45 +0000678printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
679 const char *Modifier) {
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000680 const MachineOperand &MO = MI->getOperand(opNum);
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000681 O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000682}
683
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000684void MipsAsmPrinter::
685printRegisterList(const MachineInstr *MI, int opNum, raw_ostream &O) {
686 for (int i = opNum, e = MI->getNumOperands(); i != e; ++i) {
687 if (i != opNum) O << ", ";
688 printOperand(MI, i, O);
689 }
690}
691
Bob Wilsonb633d7a2009-09-30 22:06:26 +0000692void MipsAsmPrinter::EmitStartOfAsmFile(Module &M) {
Daniel Sanders35837ac2014-08-08 10:01:29 +0000693 bool IsABICalls = Subtarget->isABICalls();
Daniel Sanders16fa1db2014-04-16 13:58:57 +0000694 if (IsABICalls) {
695 getTargetStreamer().emitDirectiveAbiCalls();
Eric Christopher8ef7a6a2014-07-18 00:08:53 +0000696 Reloc::Model RM = TM.getRelocationModel();
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.
701 if (RM == Reloc::Static && !Subtarget->isABI_N64())
702 getTargetStreamer().emitDirectiveOptionPic0();
703 }
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();
Rafael Espindolaba31e272015-01-29 17:33:21 +0000707 OutStreamer.SwitchSection(
708 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
713 Subtarget->isNaN2008() ? getTargetStreamer().emitDirectiveNaN2008()
714 : getTargetStreamer().emitDirectiveNaNLegacy();
715
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000716 // TODO: handle O64 ABI
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000717
718 if (Subtarget->isABI_EABI()) {
719 if (Subtarget->isGP32bit())
Rafael Espindolaba31e272015-01-29 17:33:21 +0000720 OutStreamer.SwitchSection(OutContext.getELFSection(".gcc_compiled_long32",
721 ELF::SHT_PROGBITS, 0));
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000722 else
Rafael Espindolaba31e272015-01-29 17:33:21 +0000723 OutStreamer.SwitchSection(OutContext.getELFSection(".gcc_compiled_long64",
724 ELF::SHT_PROGBITS, 0));
Benjamin Kramer0151d7b2010-04-05 10:17:15 +0000725 }
Daniel Sanders7e527422014-07-10 13:38:23 +0000726
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000727 getTargetStreamer().updateABIInfo(*Subtarget);
Daniel Sanders7e527422014-07-10 13:38:23 +0000728
Daniel Sanderse22244b2014-07-21 15:25:24 +0000729 // We should always emit a '.module fp=...' but binutils 2.24 does not accept
730 // it. We therefore emit it when it contradicts the ABI defaults (-mfpxx or
731 // -mfp64) and omit it otherwise.
732 if (Subtarget->isABI_O32() && (Subtarget->isABI_FPXX() ||
733 Subtarget->isFP64bit()))
734 getTargetStreamer().emitDirectiveModuleFP();
735
736 // We should always emit a '.module [no]oddspreg' but binutils 2.24 does not
737 // accept it. We therefore emit it when it contradicts the default or an
738 // option has changed the default (i.e. FPXX) and omit it otherwise.
739 if (Subtarget->isABI_O32() && (!Subtarget->useOddSPReg() ||
740 Subtarget->isABI_FPXX()))
741 getTargetStreamer().emitDirectiveModuleOddSPReg(Subtarget->useOddSPReg(),
742 Subtarget->isABI_O32());
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000743}
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000744
Toma Tabacua23f13c2014-12-17 10:56:16 +0000745void MipsAsmPrinter::emitInlineAsmStart(
746 const MCSubtargetInfo &StartInfo) const {
747 MipsTargetStreamer &TS = getTargetStreamer();
748
Toma Tabacu68e8a9c2015-01-09 15:00:30 +0000749 // GCC's choice of assembler options for inline assembly code ('at', 'macro'
750 // and 'reorder') is different from LLVM's choice for generated code ('noat',
751 // 'nomacro' and 'noreorder').
752 // In order to maintain compatibility with inline assembly code which depends
753 // on GCC's assembler options being used, we have to switch to those options
754 // for the duration of the inline assembly block and then switch back.
Toma Tabacua23f13c2014-12-17 10:56:16 +0000755 TS.emitDirectiveSetPush();
756 TS.emitDirectiveSetAt();
757 TS.emitDirectiveSetMacro();
758 TS.emitDirectiveSetReorder();
759 OutStreamer.AddBlankLine();
760}
761
762void MipsAsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
763 const MCSubtargetInfo *EndInfo) const {
764 OutStreamer.AddBlankLine();
765 getTargetStreamer().emitDirectiveSetPop();
766}
767
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000768void MipsAsmPrinter::EmitJal(MCSymbol *Symbol) {
769 MCInst I;
770 I.setOpcode(Mips::JAL);
771 I.addOperand(
772 MCOperand::CreateExpr(MCSymbolRefExpr::Create(Symbol, OutContext)));
773 OutStreamer.EmitInstruction(I, getSubtargetInfo());
774}
775
776void MipsAsmPrinter::EmitInstrReg(unsigned Opcode, unsigned Reg) {
777 MCInst I;
778 I.setOpcode(Opcode);
779 I.addOperand(MCOperand::CreateReg(Reg));
780 OutStreamer.EmitInstruction(I, getSubtargetInfo());
781}
782
783void MipsAsmPrinter::EmitInstrRegReg(unsigned Opcode, unsigned Reg1,
784 unsigned Reg2) {
785 MCInst I;
786 //
787 // Because of the current td files for Mips32, the operands for MTC1
788 // appear backwards from their normal assembly order. It's not a trivial
789 // change to fix this in the td file so we adjust for it here.
790 //
791 if (Opcode == Mips::MTC1) {
792 unsigned Temp = Reg1;
793 Reg1 = Reg2;
794 Reg2 = Temp;
795 }
796 I.setOpcode(Opcode);
797 I.addOperand(MCOperand::CreateReg(Reg1));
798 I.addOperand(MCOperand::CreateReg(Reg2));
799 OutStreamer.EmitInstruction(I, getSubtargetInfo());
800}
801
802void MipsAsmPrinter::EmitInstrRegRegReg(unsigned Opcode, unsigned Reg1,
803 unsigned Reg2, unsigned Reg3) {
804 MCInst I;
805 I.setOpcode(Opcode);
806 I.addOperand(MCOperand::CreateReg(Reg1));
807 I.addOperand(MCOperand::CreateReg(Reg2));
808 I.addOperand(MCOperand::CreateReg(Reg3));
809 OutStreamer.EmitInstruction(I, getSubtargetInfo());
810}
811
812void MipsAsmPrinter::EmitMovFPIntPair(unsigned MovOpc, unsigned Reg1,
813 unsigned Reg2, unsigned FPReg1,
814 unsigned FPReg2, bool LE) {
815 if (!LE) {
816 unsigned temp = Reg1;
817 Reg1 = Reg2;
818 Reg2 = temp;
819 }
820 EmitInstrRegReg(MovOpc, Reg1, FPReg1);
821 EmitInstrRegReg(MovOpc, Reg2, FPReg2);
822}
823
824void MipsAsmPrinter::EmitSwapFPIntParams(Mips16HardFloatInfo::FPParamVariant PV,
825 bool LE, bool ToFP) {
826 using namespace Mips16HardFloatInfo;
827 unsigned MovOpc = ToFP ? Mips::MTC1 : Mips::MFC1;
828 switch (PV) {
829 case FSig:
830 EmitInstrRegReg(MovOpc, Mips::A0, Mips::F12);
831 break;
832 case FFSig:
833 EmitMovFPIntPair(MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F14, LE);
834 break;
835 case FDSig:
836 EmitInstrRegReg(MovOpc, Mips::A0, Mips::F12);
837 EmitMovFPIntPair(MovOpc, Mips::A2, Mips::A3, Mips::F14, Mips::F15, LE);
838 break;
839 case DSig:
840 EmitMovFPIntPair(MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
841 break;
842 case DDSig:
843 EmitMovFPIntPair(MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
844 EmitMovFPIntPair(MovOpc, Mips::A2, Mips::A3, Mips::F14, Mips::F15, LE);
845 break;
846 case DFSig:
847 EmitMovFPIntPair(MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
848 EmitInstrRegReg(MovOpc, Mips::A2, Mips::F14);
849 break;
850 case NoSig:
851 return;
852 }
853}
854
855void
856MipsAsmPrinter::EmitSwapFPIntRetval(Mips16HardFloatInfo::FPReturnVariant RV,
857 bool LE) {
858 using namespace Mips16HardFloatInfo;
859 unsigned MovOpc = Mips::MFC1;
860 switch (RV) {
861 case FRet:
862 EmitInstrRegReg(MovOpc, Mips::V0, Mips::F0);
863 break;
864 case DRet:
865 EmitMovFPIntPair(MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
866 break;
867 case CFRet:
868 EmitMovFPIntPair(MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
869 break;
870 case CDRet:
871 EmitMovFPIntPair(MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
872 EmitMovFPIntPair(MovOpc, Mips::A0, Mips::A1, Mips::F2, Mips::F3, LE);
873 break;
874 case NoFPRet:
875 break;
876 }
877}
878
879void MipsAsmPrinter::EmitFPCallStub(
880 const char *Symbol, const Mips16HardFloatInfo::FuncSignature *Signature) {
881 MCSymbol *MSymbol = OutContext.GetOrCreateSymbol(StringRef(Symbol));
882 using namespace Mips16HardFloatInfo;
883 bool LE = Subtarget->isLittle();
884 //
885 // .global xxxx
886 //
887 OutStreamer.EmitSymbolAttribute(MSymbol, MCSA_Global);
888 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 }
935 OutStreamer.AddComment("\t# Stub function to call " + Twine(RetType) + " " +
936 Twine(Symbol) + " (" + Twine(Parms) + ")");
937 //
938 // probably not necessary but we save and restore the current section state
939 //
940 OutStreamer.PushSection();
941 //
942 // .section mips16.call.fpxxxx,"ax",@progbits
943 //
944 const MCSectionELF *M = OutContext.getELFSection(
945 ".mips16.call.fp." + std::string(Symbol), ELF::SHT_PROGBITS,
Rafael Espindolaba31e272015-01-29 17:33:21 +0000946 ELF::SHF_ALLOC | ELF::SHF_EXECINSTR);
Craig Topper062a2ba2014-04-25 05:30:21 +0000947 OutStreamer.SwitchSection(M, nullptr);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000948 //
949 // .align 2
950 //
951 OutStreamer.EmitValueToAlignment(4);
952 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);
965 MCSymbol *Stub = OutContext.GetOrCreateSymbol(StringRef(x));
966 TS.emitDirectiveEnt(*Stub);
967 MCSymbol *MType =
968 OutContext.GetOrCreateSymbol("__call_stub_fp_" + Twine(Symbol));
969 OutStreamer.EmitSymbolAttribute(MType, MCSA_ELF_TypeFunction);
970 OutStreamer.EmitLabel(Stub);
971 //
972 // we just handle non pic for now. these function will not be
973 // called otherwise. when the full stub generation is moved here
974 // we need to deal with pic.
975 //
Eric Christopher822f1e42015-01-06 01:12:40 +0000976 if (TM.getRelocationModel() == Reloc::PIC_)
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000977 llvm_unreachable("should not be here if we are compiling pic");
978 TS.emitDirectiveSetReorder();
979 //
980 // We need to add a MipsMCExpr class to MCTargetDesc to fully implement
981 // stubs without raw text but this current patch is for compiler generated
982 // functions and they all return some value.
983 // The calling sequence for non pic is different in that case and we need
984 // to implement %lo and %hi in order to handle the case of no return value
985 // See the corresponding method in Mips16HardFloat for details.
986 //
987 // mov the return address to S2.
988 // we have no stack space to store it and we are about to make another call.
989 // We need to make sure that the enclosing function knows to save S2
990 // This should have already been handled.
991 //
992 // Mov $18, $31
993
994 EmitInstrRegRegReg(Mips::ADDu, Mips::S2, Mips::RA, Mips::ZERO);
995
996 EmitSwapFPIntParams(Signature->ParamSig, LE, true);
997
998 // Jal xxxx
999 //
1000 EmitJal(MSymbol);
1001
1002 // fix return values
1003 EmitSwapFPIntRetval(Signature->RetSig, LE);
1004 //
1005 // do the return
1006 // if (Signature->RetSig == NoFPRet)
1007 // llvm_unreachable("should not be any stubs here with no return value");
1008 // else
1009 EmitInstrReg(Mips::JR, Mips::S2);
1010
1011 MCSymbol *Tmp = OutContext.CreateTempSymbol();
1012 OutStreamer.EmitLabel(Tmp);
1013 const MCSymbolRefExpr *E = MCSymbolRefExpr::Create(Stub, OutContext);
1014 const MCSymbolRefExpr *T = MCSymbolRefExpr::Create(Tmp, OutContext);
1015 const MCExpr *T_min_E = MCBinaryExpr::CreateSub(T, E, OutContext);
1016 OutStreamer.EmitELFSize(Stub, T_min_E);
1017 TS.emitDirectiveEnd(x);
1018 OutStreamer.PopSection();
1019}
1020
1021void MipsAsmPrinter::EmitEndOfAsmFile(Module &M) {
1022 // Emit needed stubs
1023 //
1024 for (std::map<
1025 const char *,
1026 const llvm::Mips16HardFloatInfo::FuncSignature *>::const_iterator
1027 it = StubsNeeded.begin();
1028 it != StubsNeeded.end(); ++it) {
1029 const char *Symbol = it->first;
1030 const llvm::Mips16HardFloatInfo::FuncSignature *Signature = it->second;
1031 EmitFPCallStub(Symbol, Signature);
1032 }
Rafael Espindola2ab7ea72014-01-27 01:33:33 +00001033 // return to the text section
1034 OutStreamer.SwitchSection(OutContext.getObjectFileInfo()->getTextSection());
Jack Carterc1b17ed2013-01-18 21:20:38 +00001035}
1036
Akira Hatanakaf2bcad92011-07-01 01:04:43 +00001037void MipsAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
1038 raw_ostream &OS) {
1039 // TODO: implement
1040}
1041
Sasa Stankovic8c5736b2014-02-28 10:00:38 +00001042// Align all targets of indirect branches on bundle size. Used only if target
1043// is NaCl.
1044void MipsAsmPrinter::NaClAlignIndirectJumpTargets(MachineFunction &MF) {
1045 // Align all blocks that are jumped to through jump table.
1046 if (MachineJumpTableInfo *JtInfo = MF.getJumpTableInfo()) {
1047 const std::vector<MachineJumpTableEntry> &JT = JtInfo->getJumpTables();
1048 for (unsigned I = 0; I < JT.size(); ++I) {
1049 const std::vector<MachineBasicBlock*> &MBBs = JT[I].MBBs;
1050
1051 for (unsigned J = 0; J < MBBs.size(); ++J)
1052 MBBs[J]->setAlignment(MIPS_NACL_BUNDLE_ALIGN);
1053 }
1054 }
1055
1056 // If basic block address is taken, block can be target of indirect branch.
1057 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
1058 MBB != E; ++MBB) {
1059 if (MBB->hasAddressTaken())
1060 MBB->setAlignment(MIPS_NACL_BUNDLE_ALIGN);
1061 }
1062}
1063
Sasa Stankovic7b061a42014-04-30 15:06:25 +00001064bool MipsAsmPrinter::isLongBranchPseudo(int Opcode) const {
1065 return (Opcode == Mips::LONG_BRANCH_LUi
1066 || Opcode == Mips::LONG_BRANCH_ADDiu
Sasa Stankovic7b061a42014-04-30 15:06:25 +00001067 || Opcode == Mips::LONG_BRANCH_DADDiu);
1068}
1069
Bob Wilson5a495fe2009-06-23 23:59:40 +00001070// Force static initialization.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +00001071extern "C" void LLVMInitializeMipsAsmPrinter() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +00001072 RegisterAsmPrinter<MipsAsmPrinter> X(TheMipsTarget);
1073 RegisterAsmPrinter<MipsAsmPrinter> Y(TheMipselTarget);
Akira Hatanaka3d673cc2011-09-21 03:00:58 +00001074 RegisterAsmPrinter<MipsAsmPrinter> A(TheMips64Target);
1075 RegisterAsmPrinter<MipsAsmPrinter> B(TheMips64elTarget);
Daniel Dunbare8338102009-07-15 20:24:03 +00001076}