blob: 16db9ac532457a7b0a3d3dde75a5263df4612454 [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"
Rafael Espindolaa17151a2013-10-08 13:08:17 +000022#include "MipsTargetStreamer.h"
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +000023#include "llvm/ADT/SmallString.h"
24#include "llvm/ADT/StringExtras.h"
25#include "llvm/ADT/Twine.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000026#include "llvm/CodeGen/MachineConstantPool.h"
Bruno Cardoso Lopesbcda5e22007-07-11 23:24:41 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
Jack Carterb2af5122012-07-05 23:58:21 +000028#include "llvm/CodeGen/MachineFunctionPass.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000029#include "llvm/CodeGen/MachineInstr.h"
Sasa Stankovic8c5736b2014-02-28 10:00:38 +000030#include "llvm/CodeGen/MachineJumpTableInfo.h"
Akira Hatanaka2fcc1cf2011-08-12 21:30:06 +000031#include "llvm/CodeGen/MachineMemOperand.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/BasicBlock.h"
33#include "llvm/IR/DataLayout.h"
34#include "llvm/IR/InlineAsm.h"
35#include "llvm/IR/Instructions.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000036#include "llvm/IR/Mangler.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000037#include "llvm/MC/MCAsmInfo.h"
Rafael Espindola2ab7ea72014-01-27 01:33:33 +000038#include "llvm/MC/MCContext.h"
Rafael Espindolaac4ad252013-10-05 16:42:21 +000039#include "llvm/MC/MCELFStreamer.h"
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000040#include "llvm/MC/MCExpr.h"
Akira Hatanaka9c6028f2011-07-07 23:56:50 +000041#include "llvm/MC/MCInst.h"
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000042#include "llvm/MC/MCSection.h"
Rafael Espindola2ab7ea72014-01-27 01:33:33 +000043#include "llvm/MC/MCSectionELF.h"
Chris Lattner4cd44982009-09-13 17:14:04 +000044#include "llvm/MC/MCSymbol.h"
Jack Carterab3cb422013-02-19 22:04:37 +000045#include "llvm/Support/ELF.h"
Jack Carterb2af5122012-07-05 23:58:21 +000046#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000047#include "llvm/Support/raw_ostream.h"
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +000048#include "llvm/Target/TargetLoweringObjectFile.h"
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +000049#include "llvm/Target/TargetOptions.h"
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000050#include <string>
Akira Hatanakaf2bcad92011-07-01 01:04:43 +000051
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000052using namespace llvm;
53
Chandler Carruth84e68b22014-04-22 02:41:26 +000054#define DEBUG_TYPE "mips-asm-printer"
55
Rafael Espindolaa17151a2013-10-08 13:08:17 +000056MipsTargetStreamer &MipsAsmPrinter::getTargetStreamer() {
Rafael Espindola4a1a3602014-01-14 01:21:46 +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) {
Reed Kotler1595f362013-04-09 19:46:01 +000061 // Initialize TargetLoweringObjectFile.
62 if (Subtarget->allowMixed16_32())
63 const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
64 .Initialize(OutContext, TM);
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000065 MipsFI = MF.getInfo<MipsFunctionInfo>();
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000066 if (Subtarget->inMips16Mode())
67 for (std::map<
68 const char *,
69 const llvm::Mips16HardFloatInfo::FuncSignature *>::const_iterator
70 it = MipsFI->StubsNeeded.begin();
71 it != MipsFI->StubsNeeded.end(); ++it) {
72 const char *Symbol = it->first;
73 const llvm::Mips16HardFloatInfo::FuncSignature *Signature = it->second;
74 if (StubsNeeded.find(Symbol) == StubsNeeded.end())
75 StubsNeeded[Symbol] = Signature;
76 }
Reed Kotler91ae9822013-10-27 21:57:36 +000077 MCP = MF.getConstantPool();
Sasa Stankovic8c5736b2014-02-28 10:00:38 +000078
79 // In NaCl, all indirect jump targets must be aligned to bundle size.
80 if (Subtarget->isTargetNaCl())
81 NaClAlignIndirectJumpTargets(MF);
82
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000083 AsmPrinter::runOnMachineFunction(MF);
84 return true;
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +000085}
86
Akira Hatanaka42a35242012-09-27 01:59:07 +000087bool MipsAsmPrinter::lowerOperand(const MachineOperand &MO, MCOperand &MCOp) {
88 MCOp = MCInstLowering.LowerOperand(MO);
89 return MCOp.isValid();
90}
91
92#include "MipsGenMCPseudoLowering.inc"
93
Akira Hatanakaddd12652011-07-07 20:10:52 +000094void MipsAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Vladimir Medicfb8a2a92014-07-08 08:59:22 +000095 MipsTargetStreamer &TS = getTargetStreamer();
96 TS.setCanHaveModuleDir(false);
Daniel Sandersc7dbc632014-07-08 10:11:38 +000097
Akira Hatanakaddd12652011-07-07 20:10:52 +000098 if (MI->isDebugValue()) {
Bruno Cardoso Lopescd1d4472011-12-30 21:09:41 +000099 SmallString<128> Str;
100 raw_svector_ostream OS(Str);
101
Akira Hatanakaddd12652011-07-07 20:10:52 +0000102 PrintDebugValueComment(MI, OS);
103 return;
104 }
105
Reed Kotler91ae9822013-10-27 21:57:36 +0000106 // If we just ended a constant pool, mark it as such.
107 if (InConstantPool && MI->getOpcode() != Mips::CONSTPOOL_ENTRY) {
108 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
109 InConstantPool = false;
110 }
111 if (MI->getOpcode() == Mips::CONSTPOOL_ENTRY) {
112 // CONSTPOOL_ENTRY - This instruction represents a floating
113 //constant pool in the function. The first operand is the ID#
114 // for this instruction, the second is the index into the
115 // MachineConstantPool that this is, the third is the size in
116 // bytes of this constant pool entry.
117 // The required alignment is specified on the basic block holding this MI.
118 //
119 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
120 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
121
122 // If this is the first entry of the pool, mark it.
123 if (!InConstantPool) {
124 OutStreamer.EmitDataRegion(MCDR_DataRegion);
125 InConstantPool = true;
126 }
127
128 OutStreamer.EmitLabel(GetCPISymbol(LabelId));
129
130 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
131 if (MCPE.isMachineConstantPoolEntry())
132 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
133 else
134 EmitGlobalConstant(MCPE.Val.ConstVal);
135 return;
136 }
137
Rafael Espindola14d02fe2014-01-25 15:06:56 +0000138
Akira Hatanaka5ac78682012-06-13 23:25:52 +0000139 MachineBasicBlock::const_instr_iterator I = MI;
140 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
141
142 do {
Akira Hatanaka556135d2013-02-06 21:50:15 +0000143 // Do any auto-generated pseudo lowerings.
144 if (emitPseudoExpansionLowering(OutStreamer, &*I))
145 continue;
Jack Carterc20a21b2012-08-28 19:07:39 +0000146
Reed Kotler76c9bcd2013-02-15 21:05:58 +0000147 // The inMips16Mode() test is not permanent.
148 // Some instructions are marked as pseudo right now which
149 // would make the test fail for the wrong reason but
150 // that will be fixed soon. We need this here because we are
151 // removing another test for this situation downstream in the
152 // callchain.
153 //
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000154 if (I->isPseudo() && !Subtarget->inMips16Mode()
155 && !isLongBranchPseudo(I->getOpcode()))
Reed Kotler76c9bcd2013-02-15 21:05:58 +0000156 llvm_unreachable("Pseudo opcode found in EmitInstruction()");
157
Akira Hatanaka556135d2013-02-06 21:50:15 +0000158 MCInst TmpInst0;
159 MCInstLowering.Lower(I, TmpInst0);
David Woodhousee6c13e42014-01-28 23:12:42 +0000160 EmitToStreamer(OutStreamer, TmpInst0);
Akira Hatanaka556135d2013-02-06 21:50:15 +0000161 } while ((++I != E) && I->isInsideBundle()); // Delay slot check
Akira Hatanakaddd12652011-07-07 20:10:52 +0000162}
163
Akira Hatanakae2489122011-04-15 21:51:11 +0000164//===----------------------------------------------------------------------===//
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000165//
166// Mips Asm Directives
167//
168// -- Frame directive "frame Stackpointer, Stacksize, RARegister"
169// Describe the stack frame.
170//
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000171// -- Mask directives "(f)mask bitmask, offset"
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000172// Tells the assembler which registers are saved and where.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000173// bitmask - contain a little endian bitset indicating which registers are
174// saved on function prologue (e.g. with a 0x80000000 mask, the
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000175// assembler knows the register 31 (RA) is saved at prologue.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000176// offset - the position before stack pointer subtraction indicating where
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000177// the first saved register on prologue is located. (e.g. with a
178//
179// Consider the following function prologue:
180//
Bill Wendling97925ec2008-02-27 06:33:05 +0000181// .frame $fp,48,$ra
182// .mask 0xc0000000,-8
183// addiu $sp, $sp, -48
184// sw $ra, 40($sp)
185// sw $fp, 36($sp)
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000186//
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000187// With a 0xc0000000 mask, the assembler knows the register 31 (RA) and
188// 30 (FP) are saved at prologue. As the save order on prologue is from
189// left to right, RA is saved first. A -8 offset means that after the
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000190// stack pointer subtration, the first register in the mask (RA) will be
191// saved at address 48-8=40.
192//
Akira Hatanakae2489122011-04-15 21:51:11 +0000193//===----------------------------------------------------------------------===//
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000194
Akira Hatanakae2489122011-04-15 21:51:11 +0000195//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000196// Mask directives
Akira Hatanakae2489122011-04-15 21:51:11 +0000197//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000198
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000199// Create a bitmask with all callee saved registers for CPU or Floating Point
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000200// registers. For CPU registers consider RA, GP and FP for saving if necessary.
Rafael Espindola25fa2912014-01-27 04:33:11 +0000201void MipsAsmPrinter::printSavedRegsBitmask() {
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000202 // CPU and FPU Saved Registers Bitmasks
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000203 unsigned CPUBitmask = 0, FPUBitmask = 0;
204 int CPUTopSavedRegOff, FPUTopSavedRegOff;
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000205
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000206 // Set the CPU and FPU Bitmasks
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000207 const MachineFrameInfo *MFI = MF->getFrameInfo();
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000208 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000209 // size of stack area to which FP callee-saved regs are saved.
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000210 unsigned CPURegSize = Mips::GPR32RegClass.getSize();
Craig Topperc7242e02012-04-20 07:30:17 +0000211 unsigned FGR32RegSize = Mips::FGR32RegClass.getSize();
212 unsigned AFGR64RegSize = Mips::AFGR64RegClass.getSize();
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000213 bool HasAFGR64Reg = false;
214 unsigned CSFPRegsSize = 0;
215 unsigned i, e = CSI.size();
216
217 // Set FPU Bitmask.
218 for (i = 0; i != e; ++i) {
Rafael Espindolaf2dffce2010-06-02 20:02:30 +0000219 unsigned Reg = CSI[i].getReg();
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000220 if (Mips::GPR32RegClass.contains(Reg))
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000221 break;
222
Akira Hatanaka5d6faed2012-12-10 20:04:40 +0000223 unsigned RegNum = TM.getRegisterInfo()->getEncodingValue(Reg);
Craig Topperc7242e02012-04-20 07:30:17 +0000224 if (Mips::AFGR64RegClass.contains(Reg)) {
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000225 FPUBitmask |= (3 << RegNum);
226 CSFPRegsSize += AFGR64RegSize;
227 HasAFGR64Reg = true;
228 continue;
229 }
230
231 FPUBitmask |= (1 << RegNum);
232 CSFPRegsSize += FGR32RegSize;
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000233 }
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000234
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000235 // Set CPU Bitmask.
236 for (; i != e; ++i) {
237 unsigned Reg = CSI[i].getReg();
Akira Hatanaka5d6faed2012-12-10 20:04:40 +0000238 unsigned RegNum = TM.getRegisterInfo()->getEncodingValue(Reg);
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000239 CPUBitmask |= (1 << RegNum);
240 }
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000241
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000242 // FP Regs are saved right below where the virtual frame pointer points to.
243 FPUTopSavedRegOff = FPUBitmask ?
244 (HasAFGR64Reg ? -AFGR64RegSize : -FGR32RegSize) : 0;
245
246 // CPU Regs are saved below FP Regs.
247 CPUTopSavedRegOff = CPUBitmask ? -CSFPRegsSize - CPURegSize : 0;
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000248
Rafael Espindola25fa2912014-01-27 04:33:11 +0000249 MipsTargetStreamer &TS = getTargetStreamer();
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000250 // Print CPUBitmask
Rafael Espindola25fa2912014-01-27 04:33:11 +0000251 TS.emitMask(CPUBitmask, CPUTopSavedRegOff);
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000252
253 // Print FPUBitmask
Rafael Espindola25fa2912014-01-27 04:33:11 +0000254 TS.emitFMask(FPUBitmask, FPUTopSavedRegOff);
Bruno Cardoso Lopesbcda5e22007-07-11 23:24:41 +0000255}
256
Akira Hatanakae2489122011-04-15 21:51:11 +0000257//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000258// Frame and Set directives
Akira Hatanakae2489122011-04-15 21:51:11 +0000259//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000260
261/// Frame Directive
Chris Lattner5e596182010-04-04 07:05:53 +0000262void MipsAsmPrinter::emitFrameDirective() {
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000263 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
264
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000265 unsigned stackReg = RI.getFrameRegister(*MF);
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000266 unsigned returnReg = RI.getRARegister();
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000267 unsigned stackSize = MF->getFrameInfo()->getStackSize();
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000268
Rafael Espindola054234f2014-01-27 03:53:56 +0000269 getTargetStreamer().emitFrame(stackReg, stackSize, returnReg);
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000270}
271
272/// Emit Set directives.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000273const char *MipsAsmPrinter::getCurrentABIString() const {
Chris Lattner5e596182010-04-04 07:05:53 +0000274 switch (Subtarget->getTargetABI()) {
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000275 case MipsSubtarget::O32: return "abi32";
Chris Lattner5e596182010-04-04 07:05:53 +0000276 case MipsSubtarget::N32: return "abiN32";
277 case MipsSubtarget::N64: return "abi64";
278 case MipsSubtarget::EABI: return "eabi32"; // TODO: handle eabi64
Dmitri Gribenkoca1e27b2012-09-10 21:26:47 +0000279 default: llvm_unreachable("Unknown Mips ABI");
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000280 }
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000281}
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000282
Chris Lattner5d9fb4b2010-01-27 23:23:58 +0000283void MipsAsmPrinter::EmitFunctionEntryLabel() {
Rafael Espindola6633d572014-01-14 18:57:12 +0000284 MipsTargetStreamer &TS = getTargetStreamer();
Sasa Stankovic8c5736b2014-02-28 10:00:38 +0000285
286 // NaCl sandboxing requires that indirect call instructions are masked.
287 // This means that function entry points should be bundle-aligned.
288 if (Subtarget->isTargetNaCl())
289 EmitAlignment(std::max(MF->getAlignment(), MIPS_NACL_BUNDLE_ALIGN));
290
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000291 if (Subtarget->inMicroMipsMode())
Rafael Espindola6633d572014-01-14 18:57:12 +0000292 TS.emitDirectiveSetMicroMips();
Matheus Almeidadc7e48e2014-04-16 11:46:59 +0000293 else
294 TS.emitDirectiveSetNoMicroMips();
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000295
Rafael Espindola6633d572014-01-14 18:57:12 +0000296 if (Subtarget->inMips16Mode())
297 TS.emitDirectiveSetMips16();
298 else
299 TS.emitDirectiveSetNoMips16();
Jack Carterab3cb422013-02-19 22:04:37 +0000300
Rafael Espindola6633d572014-01-14 18:57:12 +0000301 TS.emitDirectiveEnt(*CurrentFnSym);
Chris Lattner5d9fb4b2010-01-27 23:23:58 +0000302 OutStreamer.EmitLabel(CurrentFnSym);
303}
304
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000305/// EmitFunctionBodyStart - Targets can override this to emit stuff before
306/// the first basic block in the function.
307void MipsAsmPrinter::EmitFunctionBodyStart() {
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000308 MipsTargetStreamer &TS = getTargetStreamer();
309
Rafael Espindola7d78b2a2013-10-29 16:24:21 +0000310 MCInstLowering.Initialize(&MF->getContext());
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +0000311
Reed Kotler0f2b10e2013-05-03 23:17:24 +0000312 bool IsNakedFunction =
313 MF->getFunction()->
314 getAttributes().hasAttribute(AttributeSet::FunctionIndex,
315 Attribute::Naked);
316 if (!IsNakedFunction)
317 emitFrameDirective();
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000318
Rafael Espindola25fa2912014-01-27 04:33:11 +0000319 if (!IsNakedFunction)
320 printSavedRegsBitmask();
321
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000322 if (!Subtarget->inMips16Mode()) {
323 TS.emitDirectiveSetNoReorder();
324 TS.emitDirectiveSetNoMacro();
325 TS.emitDirectiveSetNoAt();
Akira Hatanaka8f3573032012-05-12 00:48:43 +0000326 }
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000327}
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000328
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000329/// EmitFunctionBodyEnd - Targets can override this to emit stuff after
330/// the last basic block in the function.
331void MipsAsmPrinter::EmitFunctionBodyEnd() {
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000332 MipsTargetStreamer &TS = getTargetStreamer();
333
Chris Lattnerfd97a332010-01-28 01:48:52 +0000334 // There are instruction for this macros, but they must
335 // always be at the function end, and we can't emit and
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000336 // break with BB logic.
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000337 if (!Subtarget->inMips16Mode()) {
338 TS.emitDirectiveSetAt();
339 TS.emitDirectiveSetMacro();
340 TS.emitDirectiveSetReorder();
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000341 }
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000342 TS.emitDirectiveEnd(CurrentFnSym->getName());
Reed Kotler91ae9822013-10-27 21:57:36 +0000343 // Make sure to terminate any constant pools that were at the end
344 // of the function.
345 if (!InConstantPool)
346 return;
347 InConstantPool = false;
348 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000349}
350
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000351/// isBlockOnlyReachableByFallthough - Return true if the basic block has
352/// exactly one predecessor and the control transfer mechanism between
353/// the predecessor and this block is a fall-through.
Akira Hatanakae2489122011-04-15 21:51:11 +0000354bool MipsAsmPrinter::isBlockOnlyReachableByFallthrough(const MachineBasicBlock*
355 MBB) const {
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000356 // The predecessor has to be immediately before this block.
357 const MachineBasicBlock *Pred = *MBB->pred_begin();
358
359 // If the predecessor is a switch statement, assume a jump table
360 // implementation, so it is not a fall through.
361 if (const BasicBlock *bb = Pred->getBasicBlock())
362 if (isa<SwitchInst>(bb->getTerminator()))
363 return false;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000364
Akira Hatanakae625ba42011-04-01 18:57:38 +0000365 // If this is a landing pad, it isn't a fall through. If it has no preds,
366 // then nothing falls through to it.
367 if (MBB->isLandingPad() || MBB->pred_empty())
368 return false;
369
370 // If there isn't exactly one predecessor, it can't be a fall through.
371 MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI;
372 ++PI2;
Jia Liuf54f60f2012-02-28 07:46:26 +0000373
Akira Hatanakae625ba42011-04-01 18:57:38 +0000374 if (PI2 != MBB->pred_end())
Jia Liuf54f60f2012-02-28 07:46:26 +0000375 return false;
Akira Hatanakae625ba42011-04-01 18:57:38 +0000376
377 // The predecessor has to be immediately before this block.
378 if (!Pred->isLayoutSuccessor(MBB))
379 return false;
Jia Liuf54f60f2012-02-28 07:46:26 +0000380
Akira Hatanakae625ba42011-04-01 18:57:38 +0000381 // If the block is completely empty, then it definitely does fall through.
382 if (Pred->empty())
383 return true;
Jia Liuf54f60f2012-02-28 07:46:26 +0000384
Akira Hatanakae625ba42011-04-01 18:57:38 +0000385 // Otherwise, check the last instruction.
386 // Check if the last terminator is an unconditional branch.
387 MachineBasicBlock::const_iterator I = Pred->end();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000388 while (I != Pred->begin() && !(--I)->isTerminator()) ;
Akira Hatanakae625ba42011-04-01 18:57:38 +0000389
Evan Cheng7f8e5632011-12-07 07:15:52 +0000390 return !I->isBarrier();
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000391}
392
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000393// Print out an operand for an inline asm expression.
Eric Christophered51b9e2012-05-10 21:48:22 +0000394bool MipsAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Chris Lattner3bb09762010-04-04 05:29:35 +0000395 unsigned AsmVariant,const char *ExtraCode,
396 raw_ostream &O) {
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000397 // Does this asm operand have a single letter operand modifier?
Eric Christophered51b9e2012-05-10 21:48:22 +0000398 if (ExtraCode && ExtraCode[0]) {
399 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000400
Eric Christophered51b9e2012-05-10 21:48:22 +0000401 const MachineOperand &MO = MI->getOperand(OpNum);
402 switch (ExtraCode[0]) {
Eric Christopherbc5d2492012-05-19 00:51:56 +0000403 default:
Jack Carterb2fd5f62012-06-21 17:14:46 +0000404 // See if this is a generic print operand
405 return AsmPrinter::PrintAsmOperand(MI,OpNum,AsmVariant,ExtraCode,O);
Eric Christopherbc5d2492012-05-19 00:51:56 +0000406 case 'X': // hex const int
407 if ((MO.getType()) != MachineOperand::MO_Immediate)
408 return true;
409 O << "0x" << StringRef(utohexstr(MO.getImm())).lower();
410 return false;
411 case 'x': // hex const int (low 16 bits)
412 if ((MO.getType()) != MachineOperand::MO_Immediate)
413 return true;
414 O << "0x" << StringRef(utohexstr(MO.getImm() & 0xffff)).lower();
415 return false;
416 case 'd': // decimal const int
417 if ((MO.getType()) != MachineOperand::MO_Immediate)
418 return true;
419 O << MO.getImm();
420 return false;
Eric Christopherf481ab32012-05-30 19:05:19 +0000421 case 'm': // decimal const int minus 1
422 if ((MO.getType()) != MachineOperand::MO_Immediate)
423 return true;
424 O << MO.getImm() - 1;
425 return false;
Jack Carter27747b52012-06-28 20:46:26 +0000426 case 'z': {
427 // $0 if zero, regular printing otherwise
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000428 if (MO.getType() != MachineOperand::MO_Immediate)
429 return true;
430 int64_t Val = MO.getImm();
431 if (Val)
432 O << Val;
433 else
434 O << "$0";
435 return false;
436 }
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000437 case 'D': // Second part of a double word register operand
438 case 'L': // Low order register of a double word register operand
Jack Cartera62ba822012-07-18 06:41:36 +0000439 case 'M': // High order register of a double word register operand
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000440 {
Jack Carterb2af5122012-07-05 23:58:21 +0000441 if (OpNum == 0)
442 return true;
443 const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
444 if (!FlagsOP.isImm())
445 return true;
446 unsigned Flags = FlagsOP.getImm();
447 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Jack Carter2ab73b12012-07-06 02:44:22 +0000448 // Number of registers represented by this operand. We are looking
449 // for 2 for 32 bit mode and 1 for 64 bit mode.
Jack Carterb2af5122012-07-05 23:58:21 +0000450 if (NumVals != 2) {
Jack Carter2ab73b12012-07-06 02:44:22 +0000451 if (Subtarget->isGP64bit() && NumVals == 1 && MO.isReg()) {
Jack Carterb2af5122012-07-05 23:58:21 +0000452 unsigned Reg = MO.getReg();
453 O << '$' << MipsInstPrinter::getRegisterName(Reg);
454 return false;
455 }
456 return true;
457 }
Jack Carter42ebf982012-07-11 21:41:49 +0000458
459 unsigned RegOp = OpNum;
460 if (!Subtarget->isGP64bit()){
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000461 // Endianess reverses which register holds the high or low value
Jack Cartera62ba822012-07-18 06:41:36 +0000462 // between M and L.
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000463 switch(ExtraCode[0]) {
Jack Cartera62ba822012-07-18 06:41:36 +0000464 case 'M':
465 RegOp = (Subtarget->isLittle()) ? OpNum + 1 : OpNum;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000466 break;
467 case 'L':
Jack Cartera62ba822012-07-18 06:41:36 +0000468 RegOp = (Subtarget->isLittle()) ? OpNum : OpNum + 1;
469 break;
470 case 'D': // Always the second part
471 RegOp = OpNum + 1;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000472 }
473 if (RegOp >= MI->getNumOperands())
474 return true;
475 const MachineOperand &MO = MI->getOperand(RegOp);
476 if (!MO.isReg())
477 return true;
478 unsigned Reg = MO.getReg();
479 O << '$' << MipsInstPrinter::getRegisterName(Reg);
480 return false;
Jack Carterb2af5122012-07-05 23:58:21 +0000481 }
Eric Christophered51b9e2012-05-10 21:48:22 +0000482 }
Daniel Sanders8b59af12013-11-12 12:56:01 +0000483 case 'w':
484 // Print MSA registers for the 'f' constraint
485 // In LLVM, the 'w' modifier doesn't need to do anything.
486 // We can just call printOperand as normal.
487 break;
Jack Carter2ab73b12012-07-06 02:44:22 +0000488 }
489 }
Eric Christophered51b9e2012-05-10 21:48:22 +0000490
491 printOperand(MI, OpNum, O);
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000492 return false;
493}
494
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000495bool MipsAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
496 unsigned OpNum, unsigned AsmVariant,
497 const char *ExtraCode,
498 raw_ostream &O) {
Jack Carterb04e3572013-04-09 23:19:50 +0000499 int Offset = 0;
500 // Currently we are expecting either no ExtraCode or 'D'
501 if (ExtraCode) {
502 if (ExtraCode[0] == 'D')
503 Offset = 4;
504 else
505 return true; // Unknown modifier.
506 }
Jia Liuf54f60f2012-02-28 07:46:26 +0000507
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000508 const MachineOperand &MO = MI->getOperand(OpNum);
509 assert(MO.isReg() && "unexpected inline asm memory operand");
Jack Carterb04e3572013-04-09 23:19:50 +0000510 O << Offset << "($" << MipsInstPrinter::getRegisterName(MO.getReg()) << ")";
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000511
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000512 return false;
513}
514
Chris Lattner76c564b2010-04-04 04:47:45 +0000515void MipsAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
516 raw_ostream &O) {
Rafael Espindola58873562014-01-03 19:21:54 +0000517 const DataLayout *DL = TM.getDataLayout();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000518 const MachineOperand &MO = MI->getOperand(opNum);
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +0000519 bool closeP = false;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000520
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000521 if (MO.getTargetFlags())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000522 closeP = true;
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000523
524 switch(MO.getTargetFlags()) {
525 case MipsII::MO_GPREL: O << "%gp_rel("; break;
526 case MipsII::MO_GOT_CALL: O << "%call16("; break;
Akira Hatanaka56d9ef52011-04-01 21:41:06 +0000527 case MipsII::MO_GOT: O << "%got("; break;
528 case MipsII::MO_ABS_HI: O << "%hi("; break;
529 case MipsII::MO_ABS_LO: O << "%lo("; break;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +0000530 case MipsII::MO_TLSGD: O << "%tlsgd("; break;
531 case MipsII::MO_GOTTPREL: O << "%gottprel("; break;
532 case MipsII::MO_TPREL_HI: O << "%tprel_hi("; break;
533 case MipsII::MO_TPREL_LO: O << "%tprel_lo("; break;
Akira Hatanaka25ce3642011-09-22 03:09:07 +0000534 case MipsII::MO_GPOFF_HI: O << "%hi(%neg(%gp_rel("; break;
535 case MipsII::MO_GPOFF_LO: O << "%lo(%neg(%gp_rel("; break;
536 case MipsII::MO_GOT_DISP: O << "%got_disp("; break;
537 case MipsII::MO_GOT_PAGE: O << "%got_page("; break;
538 case MipsII::MO_GOT_OFST: O << "%got_ofst("; break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000539 }
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000540
Chris Lattnereb2cc682009-09-13 20:31:40 +0000541 switch (MO.getType()) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000542 case MachineOperand::MO_Register:
Akira Hatanaka9c6028f2011-07-07 23:56:50 +0000543 O << '$'
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000544 << StringRef(MipsInstPrinter::getRegisterName(MO.getReg())).lower();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000545 break;
546
547 case MachineOperand::MO_Immediate:
Akira Hatanaka2db176c2011-05-24 21:22:21 +0000548 O << MO.getImm();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000549 break;
550
551 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner29bdac42010-03-13 21:04:28 +0000552 O << *MO.getMBB()->getSymbol();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000553 return;
554
555 case MachineOperand::MO_GlobalAddress:
Rafael Espindola79858aa2013-10-29 17:07:16 +0000556 O << *getSymbol(MO.getGlobal());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000557 break;
558
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000559 case MachineOperand::MO_BlockAddress: {
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000560 MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000561 O << BA->getName();
562 break;
563 }
564
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000565 case MachineOperand::MO_ConstantPoolIndex:
Rafael Espindola58873562014-01-03 19:21:54 +0000566 O << DL->getPrivateGlobalPrefix() << "CPI"
Chris Lattnera5bb3702007-12-30 23:10:15 +0000567 << getFunctionNumber() << "_" << MO.getIndex();
Bruno Cardoso Lopes4713b282009-11-19 06:06:13 +0000568 if (MO.getOffset())
569 O << "+" << MO.getOffset();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000570 break;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000571
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000572 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000573 llvm_unreachable("<unknown operand type>");
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000574 }
575
576 if (closeP) O << ")";
577}
578
Chris Lattner76c564b2010-04-04 04:47:45 +0000579void MipsAsmPrinter::printUnsignedImm(const MachineInstr *MI, int opNum,
580 raw_ostream &O) {
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000581 const MachineOperand &MO = MI->getOperand(opNum);
Devang Patel12f68552010-04-27 22:24:37 +0000582 if (MO.isImm())
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000583 O << (unsigned short int)MO.getImm();
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000584 else
Chris Lattner76c564b2010-04-04 04:47:45 +0000585 printOperand(MI, opNum, O);
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000586}
587
Daniel Sanders3f6eb542013-11-12 10:45:18 +0000588void MipsAsmPrinter::printUnsignedImm8(const MachineInstr *MI, int opNum,
589 raw_ostream &O) {
590 const MachineOperand &MO = MI->getOperand(opNum);
591 if (MO.isImm())
592 O << (unsigned short int)(unsigned char)MO.getImm();
593 else
594 printOperand(MI, opNum, O);
595}
596
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000597void MipsAsmPrinter::
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000598printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O) {
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000599 // Load/Store memory operands -- imm($reg)
600 // If PIC target the target is loaded as the
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +0000601 // pattern lw $25,%call16($28)
Chris Lattner76c564b2010-04-04 04:47:45 +0000602 printOperand(MI, opNum+1, O);
Akira Hatanaka2e766ed2011-07-07 18:57:00 +0000603 O << "(";
604 printOperand(MI, opNum, O);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000605 O << ")";
606}
607
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000608void MipsAsmPrinter::
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000609printMemOperandEA(const MachineInstr *MI, int opNum, raw_ostream &O) {
610 // when using stack locations for not load/store instructions
611 // print the same way as all normal 3 operand instructions.
612 printOperand(MI, opNum, O);
613 O << ", ";
614 printOperand(MI, opNum+1, O);
615 return;
616}
617
618void MipsAsmPrinter::
Chris Lattner76c564b2010-04-04 04:47:45 +0000619printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
620 const char *Modifier) {
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000621 const MachineOperand &MO = MI->getOperand(opNum);
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000622 O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000623}
624
Bob Wilsonb633d7a2009-09-30 22:06:26 +0000625void MipsAsmPrinter::EmitStartOfAsmFile(Module &M) {
Jack Carterf9f753c2013-06-18 19:47:15 +0000626 // TODO: Need to add -mabicalls and -mno-abicalls flags.
627 // Currently we assume that -mabicalls is the default.
Daniel Sanders16fa1db2014-04-16 13:58:57 +0000628 bool IsABICalls = true;
629 if (IsABICalls) {
630 getTargetStreamer().emitDirectiveAbiCalls();
631 Reloc::Model RM = Subtarget->getRelocationModel();
632 // FIXME: This condition should be a lot more complicated that it is here.
633 // Ideally it should test for properties of the ABI and not the ABI
634 // itself.
635 // For the moment, I'm only correcting enough to make MIPS-IV work.
636 if (RM == Reloc::Static && !Subtarget->isABI_N64())
637 getTargetStreamer().emitDirectiveOptionPic0();
638 }
Jack Carterf9f753c2013-06-18 19:47:15 +0000639
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000640 // Tell the assembler which ABI we are using
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000641 std::string SectionName = std::string(".mdebug.") + getCurrentABIString();
642 OutStreamer.SwitchSection(OutContext.getELFSection(
643 SectionName, ELF::SHT_PROGBITS, 0, SectionKind::getDataRel()));
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000644
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000645 // NaN: At the moment we only support:
646 // 1. .nan legacy (default)
647 // 2. .nan 2008
648 Subtarget->isNaN2008() ? getTargetStreamer().emitDirectiveNaN2008()
649 : getTargetStreamer().emitDirectiveNaNLegacy();
650
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000651 // TODO: handle O64 ABI
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000652
653 if (Subtarget->isABI_EABI()) {
654 if (Subtarget->isGP32bit())
655 OutStreamer.SwitchSection(
656 OutContext.getELFSection(".gcc_compiled_long32", ELF::SHT_PROGBITS, 0,
657 SectionKind::getDataRel()));
658 else
659 OutStreamer.SwitchSection(
660 OutContext.getELFSection(".gcc_compiled_long64", ELF::SHT_PROGBITS, 0,
661 SectionKind::getDataRel()));
Benjamin Kramer0151d7b2010-04-05 10:17:15 +0000662 }
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000663 getTargetStreamer().updateABIInfo(*Subtarget);
Daniel Sandersc7dbc632014-07-08 10:11:38 +0000664 getTargetStreamer().emitDirectiveModuleFP(
665 getTargetStreamer().getABIFlagsSection().FpABI, Subtarget->isABI_O32());
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000666}
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000667
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000668void MipsAsmPrinter::EmitJal(MCSymbol *Symbol) {
669 MCInst I;
670 I.setOpcode(Mips::JAL);
671 I.addOperand(
672 MCOperand::CreateExpr(MCSymbolRefExpr::Create(Symbol, OutContext)));
673 OutStreamer.EmitInstruction(I, getSubtargetInfo());
674}
675
676void MipsAsmPrinter::EmitInstrReg(unsigned Opcode, unsigned Reg) {
677 MCInst I;
678 I.setOpcode(Opcode);
679 I.addOperand(MCOperand::CreateReg(Reg));
680 OutStreamer.EmitInstruction(I, getSubtargetInfo());
681}
682
683void MipsAsmPrinter::EmitInstrRegReg(unsigned Opcode, unsigned Reg1,
684 unsigned Reg2) {
685 MCInst I;
686 //
687 // Because of the current td files for Mips32, the operands for MTC1
688 // appear backwards from their normal assembly order. It's not a trivial
689 // change to fix this in the td file so we adjust for it here.
690 //
691 if (Opcode == Mips::MTC1) {
692 unsigned Temp = Reg1;
693 Reg1 = Reg2;
694 Reg2 = Temp;
695 }
696 I.setOpcode(Opcode);
697 I.addOperand(MCOperand::CreateReg(Reg1));
698 I.addOperand(MCOperand::CreateReg(Reg2));
699 OutStreamer.EmitInstruction(I, getSubtargetInfo());
700}
701
702void MipsAsmPrinter::EmitInstrRegRegReg(unsigned Opcode, unsigned Reg1,
703 unsigned Reg2, unsigned Reg3) {
704 MCInst I;
705 I.setOpcode(Opcode);
706 I.addOperand(MCOperand::CreateReg(Reg1));
707 I.addOperand(MCOperand::CreateReg(Reg2));
708 I.addOperand(MCOperand::CreateReg(Reg3));
709 OutStreamer.EmitInstruction(I, getSubtargetInfo());
710}
711
712void MipsAsmPrinter::EmitMovFPIntPair(unsigned MovOpc, unsigned Reg1,
713 unsigned Reg2, unsigned FPReg1,
714 unsigned FPReg2, bool LE) {
715 if (!LE) {
716 unsigned temp = Reg1;
717 Reg1 = Reg2;
718 Reg2 = temp;
719 }
720 EmitInstrRegReg(MovOpc, Reg1, FPReg1);
721 EmitInstrRegReg(MovOpc, Reg2, FPReg2);
722}
723
724void MipsAsmPrinter::EmitSwapFPIntParams(Mips16HardFloatInfo::FPParamVariant PV,
725 bool LE, bool ToFP) {
726 using namespace Mips16HardFloatInfo;
727 unsigned MovOpc = ToFP ? Mips::MTC1 : Mips::MFC1;
728 switch (PV) {
729 case FSig:
730 EmitInstrRegReg(MovOpc, Mips::A0, Mips::F12);
731 break;
732 case FFSig:
733 EmitMovFPIntPair(MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F14, LE);
734 break;
735 case FDSig:
736 EmitInstrRegReg(MovOpc, Mips::A0, Mips::F12);
737 EmitMovFPIntPair(MovOpc, Mips::A2, Mips::A3, Mips::F14, Mips::F15, LE);
738 break;
739 case DSig:
740 EmitMovFPIntPair(MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
741 break;
742 case DDSig:
743 EmitMovFPIntPair(MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
744 EmitMovFPIntPair(MovOpc, Mips::A2, Mips::A3, Mips::F14, Mips::F15, LE);
745 break;
746 case DFSig:
747 EmitMovFPIntPair(MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
748 EmitInstrRegReg(MovOpc, Mips::A2, Mips::F14);
749 break;
750 case NoSig:
751 return;
752 }
753}
754
755void
756MipsAsmPrinter::EmitSwapFPIntRetval(Mips16HardFloatInfo::FPReturnVariant RV,
757 bool LE) {
758 using namespace Mips16HardFloatInfo;
759 unsigned MovOpc = Mips::MFC1;
760 switch (RV) {
761 case FRet:
762 EmitInstrRegReg(MovOpc, Mips::V0, Mips::F0);
763 break;
764 case DRet:
765 EmitMovFPIntPair(MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
766 break;
767 case CFRet:
768 EmitMovFPIntPair(MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
769 break;
770 case CDRet:
771 EmitMovFPIntPair(MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
772 EmitMovFPIntPair(MovOpc, Mips::A0, Mips::A1, Mips::F2, Mips::F3, LE);
773 break;
774 case NoFPRet:
775 break;
776 }
777}
778
779void MipsAsmPrinter::EmitFPCallStub(
780 const char *Symbol, const Mips16HardFloatInfo::FuncSignature *Signature) {
781 MCSymbol *MSymbol = OutContext.GetOrCreateSymbol(StringRef(Symbol));
782 using namespace Mips16HardFloatInfo;
783 bool LE = Subtarget->isLittle();
784 //
785 // .global xxxx
786 //
787 OutStreamer.EmitSymbolAttribute(MSymbol, MCSA_Global);
788 const char *RetType;
789 //
790 // make the comment field identifying the return and parameter
791 // types of the floating point stub
792 // # Stub function to call rettype xxxx (params)
793 //
794 switch (Signature->RetSig) {
795 case FRet:
796 RetType = "float";
797 break;
798 case DRet:
799 RetType = "double";
800 break;
801 case CFRet:
802 RetType = "complex";
803 break;
804 case CDRet:
805 RetType = "double complex";
806 break;
807 case NoFPRet:
808 RetType = "";
809 break;
810 }
811 const char *Parms;
812 switch (Signature->ParamSig) {
813 case FSig:
814 Parms = "float";
815 break;
816 case FFSig:
817 Parms = "float, float";
818 break;
819 case FDSig:
820 Parms = "float, double";
821 break;
822 case DSig:
823 Parms = "double";
824 break;
825 case DDSig:
826 Parms = "double, double";
827 break;
828 case DFSig:
829 Parms = "double, float";
830 break;
831 case NoSig:
832 Parms = "";
833 break;
834 }
835 OutStreamer.AddComment("\t# Stub function to call " + Twine(RetType) + " " +
836 Twine(Symbol) + " (" + Twine(Parms) + ")");
837 //
838 // probably not necessary but we save and restore the current section state
839 //
840 OutStreamer.PushSection();
841 //
842 // .section mips16.call.fpxxxx,"ax",@progbits
843 //
844 const MCSectionELF *M = OutContext.getELFSection(
845 ".mips16.call.fp." + std::string(Symbol), ELF::SHT_PROGBITS,
846 ELF::SHF_ALLOC | ELF::SHF_EXECINSTR, SectionKind::getText());
Craig Topper062a2ba2014-04-25 05:30:21 +0000847 OutStreamer.SwitchSection(M, nullptr);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000848 //
849 // .align 2
850 //
851 OutStreamer.EmitValueToAlignment(4);
852 MipsTargetStreamer &TS = getTargetStreamer();
853 //
854 // .set nomips16
855 // .set nomicromips
856 //
857 TS.emitDirectiveSetNoMips16();
858 TS.emitDirectiveSetNoMicroMips();
859 //
860 // .ent __call_stub_fp_xxxx
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000861 // .type __call_stub_fp_xxxx,@function
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000862 // __call_stub_fp_xxxx:
863 //
864 std::string x = "__call_stub_fp_" + std::string(Symbol);
865 MCSymbol *Stub = OutContext.GetOrCreateSymbol(StringRef(x));
866 TS.emitDirectiveEnt(*Stub);
867 MCSymbol *MType =
868 OutContext.GetOrCreateSymbol("__call_stub_fp_" + Twine(Symbol));
869 OutStreamer.EmitSymbolAttribute(MType, MCSA_ELF_TypeFunction);
870 OutStreamer.EmitLabel(Stub);
871 //
872 // we just handle non pic for now. these function will not be
873 // called otherwise. when the full stub generation is moved here
874 // we need to deal with pic.
875 //
876 if (Subtarget->getRelocationModel() == Reloc::PIC_)
877 llvm_unreachable("should not be here if we are compiling pic");
878 TS.emitDirectiveSetReorder();
879 //
880 // We need to add a MipsMCExpr class to MCTargetDesc to fully implement
881 // stubs without raw text but this current patch is for compiler generated
882 // functions and they all return some value.
883 // The calling sequence for non pic is different in that case and we need
884 // to implement %lo and %hi in order to handle the case of no return value
885 // See the corresponding method in Mips16HardFloat for details.
886 //
887 // mov the return address to S2.
888 // we have no stack space to store it and we are about to make another call.
889 // We need to make sure that the enclosing function knows to save S2
890 // This should have already been handled.
891 //
892 // Mov $18, $31
893
894 EmitInstrRegRegReg(Mips::ADDu, Mips::S2, Mips::RA, Mips::ZERO);
895
896 EmitSwapFPIntParams(Signature->ParamSig, LE, true);
897
898 // Jal xxxx
899 //
900 EmitJal(MSymbol);
901
902 // fix return values
903 EmitSwapFPIntRetval(Signature->RetSig, LE);
904 //
905 // do the return
906 // if (Signature->RetSig == NoFPRet)
907 // llvm_unreachable("should not be any stubs here with no return value");
908 // else
909 EmitInstrReg(Mips::JR, Mips::S2);
910
911 MCSymbol *Tmp = OutContext.CreateTempSymbol();
912 OutStreamer.EmitLabel(Tmp);
913 const MCSymbolRefExpr *E = MCSymbolRefExpr::Create(Stub, OutContext);
914 const MCSymbolRefExpr *T = MCSymbolRefExpr::Create(Tmp, OutContext);
915 const MCExpr *T_min_E = MCBinaryExpr::CreateSub(T, E, OutContext);
916 OutStreamer.EmitELFSize(Stub, T_min_E);
917 TS.emitDirectiveEnd(x);
918 OutStreamer.PopSection();
919}
920
921void MipsAsmPrinter::EmitEndOfAsmFile(Module &M) {
922 // Emit needed stubs
923 //
924 for (std::map<
925 const char *,
926 const llvm::Mips16HardFloatInfo::FuncSignature *>::const_iterator
927 it = StubsNeeded.begin();
928 it != StubsNeeded.end(); ++it) {
929 const char *Symbol = it->first;
930 const llvm::Mips16HardFloatInfo::FuncSignature *Signature = it->second;
931 EmitFPCallStub(Symbol, Signature);
932 }
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000933 // return to the text section
934 OutStreamer.SwitchSection(OutContext.getObjectFileInfo()->getTextSection());
Jack Carterc1b17ed2013-01-18 21:20:38 +0000935}
936
Akira Hatanakaf2bcad92011-07-01 01:04:43 +0000937void MipsAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
938 raw_ostream &OS) {
939 // TODO: implement
940}
941
Sasa Stankovic8c5736b2014-02-28 10:00:38 +0000942// Align all targets of indirect branches on bundle size. Used only if target
943// is NaCl.
944void MipsAsmPrinter::NaClAlignIndirectJumpTargets(MachineFunction &MF) {
945 // Align all blocks that are jumped to through jump table.
946 if (MachineJumpTableInfo *JtInfo = MF.getJumpTableInfo()) {
947 const std::vector<MachineJumpTableEntry> &JT = JtInfo->getJumpTables();
948 for (unsigned I = 0; I < JT.size(); ++I) {
949 const std::vector<MachineBasicBlock*> &MBBs = JT[I].MBBs;
950
951 for (unsigned J = 0; J < MBBs.size(); ++J)
952 MBBs[J]->setAlignment(MIPS_NACL_BUNDLE_ALIGN);
953 }
954 }
955
956 // If basic block address is taken, block can be target of indirect branch.
957 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
958 MBB != E; ++MBB) {
959 if (MBB->hasAddressTaken())
960 MBB->setAlignment(MIPS_NACL_BUNDLE_ALIGN);
961 }
962}
963
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000964bool MipsAsmPrinter::isLongBranchPseudo(int Opcode) const {
965 return (Opcode == Mips::LONG_BRANCH_LUi
966 || Opcode == Mips::LONG_BRANCH_ADDiu
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000967 || Opcode == Mips::LONG_BRANCH_DADDiu);
968}
969
Bob Wilson5a495fe2009-06-23 23:59:40 +0000970// Force static initialization.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000971extern "C" void LLVMInitializeMipsAsmPrinter() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +0000972 RegisterAsmPrinter<MipsAsmPrinter> X(TheMipsTarget);
973 RegisterAsmPrinter<MipsAsmPrinter> Y(TheMipselTarget);
Akira Hatanaka3d673cc2011-09-21 03:00:58 +0000974 RegisterAsmPrinter<MipsAsmPrinter> A(TheMips64Target);
975 RegisterAsmPrinter<MipsAsmPrinter> B(TheMips64elTarget);
Daniel Dunbare8338102009-07-15 20:24:03 +0000976}