blob: 43ef379080200c8e56ace805abed84aabc05df23 [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) {
Eric Christopher8ef7a6a2014-07-18 00:08:53 +000061 Subtarget = &TM.getSubtarget<MipsSubtarget>();
62
Reed Kotler1595f362013-04-09 19:46:01 +000063 // Initialize TargetLoweringObjectFile.
Eric Christopher4e7d1e72014-07-18 23:41:32 +000064 const_cast<TargetLoweringObjectFile &>(getObjFileLowering())
Reed Kotler1595f362013-04-09 19:46:01 +000065 .Initialize(OutContext, TM);
Eric Christopher4e7d1e72014-07-18 23:41:32 +000066
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000067 MipsFI = MF.getInfo<MipsFunctionInfo>();
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000068 if (Subtarget->inMips16Mode())
69 for (std::map<
70 const char *,
71 const llvm::Mips16HardFloatInfo::FuncSignature *>::const_iterator
72 it = MipsFI->StubsNeeded.begin();
73 it != MipsFI->StubsNeeded.end(); ++it) {
74 const char *Symbol = it->first;
75 const llvm::Mips16HardFloatInfo::FuncSignature *Signature = it->second;
76 if (StubsNeeded.find(Symbol) == StubsNeeded.end())
77 StubsNeeded[Symbol] = Signature;
78 }
Reed Kotler91ae9822013-10-27 21:57:36 +000079 MCP = MF.getConstantPool();
Sasa Stankovic8c5736b2014-02-28 10:00:38 +000080
81 // In NaCl, all indirect jump targets must be aligned to bundle size.
82 if (Subtarget->isTargetNaCl())
83 NaClAlignIndirectJumpTargets(MF);
84
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000085 AsmPrinter::runOnMachineFunction(MF);
86 return true;
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +000087}
88
Akira Hatanaka42a35242012-09-27 01:59:07 +000089bool MipsAsmPrinter::lowerOperand(const MachineOperand &MO, MCOperand &MCOp) {
90 MCOp = MCInstLowering.LowerOperand(MO);
91 return MCOp.isValid();
92}
93
94#include "MipsGenMCPseudoLowering.inc"
95
Daniel Sandersf5a5fbd2014-07-09 10:21:59 +000096// Lower PseudoReturn/PseudoIndirectBranch/PseudoIndirectBranch64 to JR, JR_MM,
97// JALR, or JALR64 as appropriate for the target
98void MipsAsmPrinter::emitPseudoIndirectBranch(MCStreamer &OutStreamer,
99 const MachineInstr *MI) {
Daniel Sanders338513b2014-07-09 10:16:07 +0000100 bool HasLinkReg = false;
101 MCInst TmpInst0;
102
103 if (Subtarget->hasMips64r6()) {
104 // MIPS64r6 should use (JALR64 ZERO_64, $rs)
105 TmpInst0.setOpcode(Mips::JALR64);
106 HasLinkReg = true;
107 } else if (Subtarget->hasMips32r6()) {
108 // MIPS32r6 should use (JALR ZERO, $rs)
109 TmpInst0.setOpcode(Mips::JALR);
110 HasLinkReg = true;
111 } else if (Subtarget->inMicroMipsMode())
112 // microMIPS should use (JR_MM $rs)
113 TmpInst0.setOpcode(Mips::JR_MM);
114 else {
115 // Everything else should use (JR $rs)
116 TmpInst0.setOpcode(Mips::JR);
117 }
118
119 MCOperand MCOp;
120
121 if (HasLinkReg) {
122 unsigned ZeroReg = Subtarget->isGP64bit() ? Mips::ZERO_64 : Mips::ZERO;
123 TmpInst0.addOperand(MCOperand::CreateReg(ZeroReg));
124 }
125
126 lowerOperand(MI->getOperand(0), MCOp);
127 TmpInst0.addOperand(MCOp);
128
129 EmitToStreamer(OutStreamer, TmpInst0);
130}
131
Akira Hatanakaddd12652011-07-07 20:10:52 +0000132void MipsAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000133 MipsTargetStreamer &TS = getTargetStreamer();
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000134 TS.forbidModuleDirective();
Daniel Sandersc7dbc632014-07-08 10:11:38 +0000135
Akira Hatanakaddd12652011-07-07 20:10:52 +0000136 if (MI->isDebugValue()) {
Bruno Cardoso Lopescd1d4472011-12-30 21:09:41 +0000137 SmallString<128> Str;
138 raw_svector_ostream OS(Str);
139
Akira Hatanakaddd12652011-07-07 20:10:52 +0000140 PrintDebugValueComment(MI, OS);
141 return;
142 }
143
Reed Kotler91ae9822013-10-27 21:57:36 +0000144 // If we just ended a constant pool, mark it as such.
145 if (InConstantPool && MI->getOpcode() != Mips::CONSTPOOL_ENTRY) {
146 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
147 InConstantPool = false;
148 }
149 if (MI->getOpcode() == Mips::CONSTPOOL_ENTRY) {
150 // CONSTPOOL_ENTRY - This instruction represents a floating
151 //constant pool in the function. The first operand is the ID#
152 // for this instruction, the second is the index into the
153 // MachineConstantPool that this is, the third is the size in
154 // bytes of this constant pool entry.
155 // The required alignment is specified on the basic block holding this MI.
156 //
157 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
158 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
159
160 // If this is the first entry of the pool, mark it.
161 if (!InConstantPool) {
162 OutStreamer.EmitDataRegion(MCDR_DataRegion);
163 InConstantPool = true;
164 }
165
166 OutStreamer.EmitLabel(GetCPISymbol(LabelId));
167
168 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
169 if (MCPE.isMachineConstantPoolEntry())
170 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
171 else
172 EmitGlobalConstant(MCPE.Val.ConstVal);
173 return;
174 }
175
Rafael Espindola14d02fe2014-01-25 15:06:56 +0000176
Akira Hatanaka5ac78682012-06-13 23:25:52 +0000177 MachineBasicBlock::const_instr_iterator I = MI;
178 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
179
180 do {
Akira Hatanaka556135d2013-02-06 21:50:15 +0000181 // Do any auto-generated pseudo lowerings.
182 if (emitPseudoExpansionLowering(OutStreamer, &*I))
183 continue;
Jack Carterc20a21b2012-08-28 19:07:39 +0000184
Daniel Sanders338513b2014-07-09 10:16:07 +0000185 if (I->getOpcode() == Mips::PseudoReturn ||
Daniel Sandersf5a5fbd2014-07-09 10:21:59 +0000186 I->getOpcode() == Mips::PseudoReturn64 ||
187 I->getOpcode() == Mips::PseudoIndirectBranch ||
188 I->getOpcode() == Mips::PseudoIndirectBranch64) {
189 emitPseudoIndirectBranch(OutStreamer, &*I);
Daniel Sanders338513b2014-07-09 10:16:07 +0000190 continue;
191 }
192
Reed Kotler76c9bcd2013-02-15 21:05:58 +0000193 // The inMips16Mode() test is not permanent.
194 // Some instructions are marked as pseudo right now which
195 // would make the test fail for the wrong reason but
196 // that will be fixed soon. We need this here because we are
197 // removing another test for this situation downstream in the
198 // callchain.
199 //
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000200 if (I->isPseudo() && !Subtarget->inMips16Mode()
201 && !isLongBranchPseudo(I->getOpcode()))
Reed Kotler76c9bcd2013-02-15 21:05:58 +0000202 llvm_unreachable("Pseudo opcode found in EmitInstruction()");
203
Akira Hatanaka556135d2013-02-06 21:50:15 +0000204 MCInst TmpInst0;
205 MCInstLowering.Lower(I, TmpInst0);
David Woodhousee6c13e42014-01-28 23:12:42 +0000206 EmitToStreamer(OutStreamer, TmpInst0);
Akira Hatanaka556135d2013-02-06 21:50:15 +0000207 } while ((++I != E) && I->isInsideBundle()); // Delay slot check
Akira Hatanakaddd12652011-07-07 20:10:52 +0000208}
209
Akira Hatanakae2489122011-04-15 21:51:11 +0000210//===----------------------------------------------------------------------===//
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000211//
212// Mips Asm Directives
213//
214// -- Frame directive "frame Stackpointer, Stacksize, RARegister"
215// Describe the stack frame.
216//
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000217// -- Mask directives "(f)mask bitmask, offset"
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000218// Tells the assembler which registers are saved and where.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000219// bitmask - contain a little endian bitset indicating which registers are
220// saved on function prologue (e.g. with a 0x80000000 mask, the
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000221// assembler knows the register 31 (RA) is saved at prologue.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000222// offset - the position before stack pointer subtraction indicating where
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000223// the first saved register on prologue is located. (e.g. with a
224//
225// Consider the following function prologue:
226//
Bill Wendling97925ec2008-02-27 06:33:05 +0000227// .frame $fp,48,$ra
228// .mask 0xc0000000,-8
229// addiu $sp, $sp, -48
230// sw $ra, 40($sp)
231// sw $fp, 36($sp)
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000232//
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000233// With a 0xc0000000 mask, the assembler knows the register 31 (RA) and
234// 30 (FP) are saved at prologue. As the save order on prologue is from
235// left to right, RA is saved first. A -8 offset means that after the
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000236// stack pointer subtration, the first register in the mask (RA) will be
237// saved at address 48-8=40.
238//
Akira Hatanakae2489122011-04-15 21:51:11 +0000239//===----------------------------------------------------------------------===//
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000240
Akira Hatanakae2489122011-04-15 21:51:11 +0000241//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000242// Mask directives
Akira Hatanakae2489122011-04-15 21:51:11 +0000243//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000244
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000245// Create a bitmask with all callee saved registers for CPU or Floating Point
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000246// registers. For CPU registers consider RA, GP and FP for saving if necessary.
Rafael Espindola25fa2912014-01-27 04:33:11 +0000247void MipsAsmPrinter::printSavedRegsBitmask() {
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000248 // CPU and FPU Saved Registers Bitmasks
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000249 unsigned CPUBitmask = 0, FPUBitmask = 0;
250 int CPUTopSavedRegOff, FPUTopSavedRegOff;
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000251
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000252 // Set the CPU and FPU Bitmasks
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000253 const MachineFrameInfo *MFI = MF->getFrameInfo();
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000254 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000255 // size of stack area to which FP callee-saved regs are saved.
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000256 unsigned CPURegSize = Mips::GPR32RegClass.getSize();
Craig Topperc7242e02012-04-20 07:30:17 +0000257 unsigned FGR32RegSize = Mips::FGR32RegClass.getSize();
258 unsigned AFGR64RegSize = Mips::AFGR64RegClass.getSize();
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000259 bool HasAFGR64Reg = false;
260 unsigned CSFPRegsSize = 0;
261 unsigned i, e = CSI.size();
262
263 // Set FPU Bitmask.
264 for (i = 0; i != e; ++i) {
Rafael Espindolaf2dffce2010-06-02 20:02:30 +0000265 unsigned Reg = CSI[i].getReg();
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000266 if (Mips::GPR32RegClass.contains(Reg))
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000267 break;
268
Eric Christopherd9134482014-08-04 21:25:23 +0000269 unsigned RegNum =
270 TM.getSubtargetImpl()->getRegisterInfo()->getEncodingValue(Reg);
Craig Topperc7242e02012-04-20 07:30:17 +0000271 if (Mips::AFGR64RegClass.contains(Reg)) {
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000272 FPUBitmask |= (3 << RegNum);
273 CSFPRegsSize += AFGR64RegSize;
274 HasAFGR64Reg = true;
275 continue;
276 }
277
278 FPUBitmask |= (1 << RegNum);
279 CSFPRegsSize += FGR32RegSize;
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000280 }
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000281
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000282 // Set CPU Bitmask.
283 for (; i != e; ++i) {
284 unsigned Reg = CSI[i].getReg();
Eric Christopherd9134482014-08-04 21:25:23 +0000285 unsigned RegNum =
286 TM.getSubtargetImpl()->getRegisterInfo()->getEncodingValue(Reg);
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000287 CPUBitmask |= (1 << RegNum);
288 }
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000289
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000290 // FP Regs are saved right below where the virtual frame pointer points to.
291 FPUTopSavedRegOff = FPUBitmask ?
292 (HasAFGR64Reg ? -AFGR64RegSize : -FGR32RegSize) : 0;
293
294 // CPU Regs are saved below FP Regs.
295 CPUTopSavedRegOff = CPUBitmask ? -CSFPRegsSize - CPURegSize : 0;
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000296
Rafael Espindola25fa2912014-01-27 04:33:11 +0000297 MipsTargetStreamer &TS = getTargetStreamer();
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000298 // Print CPUBitmask
Rafael Espindola25fa2912014-01-27 04:33:11 +0000299 TS.emitMask(CPUBitmask, CPUTopSavedRegOff);
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000300
301 // Print FPUBitmask
Rafael Espindola25fa2912014-01-27 04:33:11 +0000302 TS.emitFMask(FPUBitmask, FPUTopSavedRegOff);
Bruno Cardoso Lopesbcda5e22007-07-11 23:24:41 +0000303}
304
Akira Hatanakae2489122011-04-15 21:51:11 +0000305//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000306// Frame and Set directives
Akira Hatanakae2489122011-04-15 21:51:11 +0000307//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000308
309/// Frame Directive
Chris Lattner5e596182010-04-04 07:05:53 +0000310void MipsAsmPrinter::emitFrameDirective() {
Eric Christopherd9134482014-08-04 21:25:23 +0000311 const TargetRegisterInfo &RI = *TM.getSubtargetImpl()->getRegisterInfo();
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000312
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000313 unsigned stackReg = RI.getFrameRegister(*MF);
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000314 unsigned returnReg = RI.getRARegister();
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000315 unsigned stackSize = MF->getFrameInfo()->getStackSize();
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000316
Rafael Espindola054234f2014-01-27 03:53:56 +0000317 getTargetStreamer().emitFrame(stackReg, stackSize, returnReg);
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000318}
319
320/// Emit Set directives.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000321const char *MipsAsmPrinter::getCurrentABIString() const {
Daniel Sanderse2e25da2014-10-24 16:15:27 +0000322 switch (Subtarget->getABI().GetEnumValue()) {
323 case MipsABIInfo::ABI::O32: return "abi32";
324 case MipsABIInfo::ABI::N32: return "abiN32";
325 case MipsABIInfo::ABI::N64: return "abi64";
326 case MipsABIInfo::ABI::EABI: return "eabi32"; // TODO: handle eabi64
Dmitri Gribenkoca1e27b2012-09-10 21:26:47 +0000327 default: llvm_unreachable("Unknown Mips ABI");
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000328 }
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000329}
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000330
Chris Lattner5d9fb4b2010-01-27 23:23:58 +0000331void MipsAsmPrinter::EmitFunctionEntryLabel() {
Rafael Espindola6633d572014-01-14 18:57:12 +0000332 MipsTargetStreamer &TS = getTargetStreamer();
Sasa Stankovic8c5736b2014-02-28 10:00:38 +0000333
334 // NaCl sandboxing requires that indirect call instructions are masked.
335 // This means that function entry points should be bundle-aligned.
336 if (Subtarget->isTargetNaCl())
337 EmitAlignment(std::max(MF->getAlignment(), MIPS_NACL_BUNDLE_ALIGN));
338
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000339 if (Subtarget->inMicroMipsMode())
Rafael Espindola6633d572014-01-14 18:57:12 +0000340 TS.emitDirectiveSetMicroMips();
Matheus Almeidadc7e48e2014-04-16 11:46:59 +0000341 else
342 TS.emitDirectiveSetNoMicroMips();
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000343
Rafael Espindola6633d572014-01-14 18:57:12 +0000344 if (Subtarget->inMips16Mode())
345 TS.emitDirectiveSetMips16();
346 else
347 TS.emitDirectiveSetNoMips16();
Jack Carterab3cb422013-02-19 22:04:37 +0000348
Rafael Espindola6633d572014-01-14 18:57:12 +0000349 TS.emitDirectiveEnt(*CurrentFnSym);
Chris Lattner5d9fb4b2010-01-27 23:23:58 +0000350 OutStreamer.EmitLabel(CurrentFnSym);
351}
352
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000353/// EmitFunctionBodyStart - Targets can override this to emit stuff before
354/// the first basic block in the function.
355void MipsAsmPrinter::EmitFunctionBodyStart() {
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000356 MipsTargetStreamer &TS = getTargetStreamer();
357
Rafael Espindola7d78b2a2013-10-29 16:24:21 +0000358 MCInstLowering.Initialize(&MF->getContext());
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +0000359
Reed Kotler0f2b10e2013-05-03 23:17:24 +0000360 bool IsNakedFunction =
361 MF->getFunction()->
362 getAttributes().hasAttribute(AttributeSet::FunctionIndex,
363 Attribute::Naked);
364 if (!IsNakedFunction)
365 emitFrameDirective();
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000366
Rafael Espindola25fa2912014-01-27 04:33:11 +0000367 if (!IsNakedFunction)
368 printSavedRegsBitmask();
369
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000370 if (!Subtarget->inMips16Mode()) {
371 TS.emitDirectiveSetNoReorder();
372 TS.emitDirectiveSetNoMacro();
373 TS.emitDirectiveSetNoAt();
Akira Hatanaka8f3573032012-05-12 00:48:43 +0000374 }
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000375}
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000376
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000377/// EmitFunctionBodyEnd - Targets can override this to emit stuff after
378/// the last basic block in the function.
379void MipsAsmPrinter::EmitFunctionBodyEnd() {
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000380 MipsTargetStreamer &TS = getTargetStreamer();
381
Chris Lattnerfd97a332010-01-28 01:48:52 +0000382 // There are instruction for this macros, but they must
383 // always be at the function end, and we can't emit and
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000384 // break with BB logic.
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000385 if (!Subtarget->inMips16Mode()) {
386 TS.emitDirectiveSetAt();
387 TS.emitDirectiveSetMacro();
388 TS.emitDirectiveSetReorder();
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000389 }
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000390 TS.emitDirectiveEnd(CurrentFnSym->getName());
Reed Kotler91ae9822013-10-27 21:57:36 +0000391 // Make sure to terminate any constant pools that were at the end
392 // of the function.
393 if (!InConstantPool)
394 return;
395 InConstantPool = false;
396 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000397}
398
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000399/// isBlockOnlyReachableByFallthough - Return true if the basic block has
400/// exactly one predecessor and the control transfer mechanism between
401/// the predecessor and this block is a fall-through.
Akira Hatanakae2489122011-04-15 21:51:11 +0000402bool MipsAsmPrinter::isBlockOnlyReachableByFallthrough(const MachineBasicBlock*
403 MBB) const {
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000404 // The predecessor has to be immediately before this block.
405 const MachineBasicBlock *Pred = *MBB->pred_begin();
406
407 // If the predecessor is a switch statement, assume a jump table
408 // implementation, so it is not a fall through.
409 if (const BasicBlock *bb = Pred->getBasicBlock())
410 if (isa<SwitchInst>(bb->getTerminator()))
411 return false;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000412
Akira Hatanakae625ba42011-04-01 18:57:38 +0000413 // If this is a landing pad, it isn't a fall through. If it has no preds,
414 // then nothing falls through to it.
415 if (MBB->isLandingPad() || MBB->pred_empty())
416 return false;
417
418 // If there isn't exactly one predecessor, it can't be a fall through.
419 MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI;
420 ++PI2;
Jia Liuf54f60f2012-02-28 07:46:26 +0000421
Akira Hatanakae625ba42011-04-01 18:57:38 +0000422 if (PI2 != MBB->pred_end())
Jia Liuf54f60f2012-02-28 07:46:26 +0000423 return false;
Akira Hatanakae625ba42011-04-01 18:57:38 +0000424
425 // The predecessor has to be immediately before this block.
426 if (!Pred->isLayoutSuccessor(MBB))
427 return false;
Jia Liuf54f60f2012-02-28 07:46:26 +0000428
Akira Hatanakae625ba42011-04-01 18:57:38 +0000429 // If the block is completely empty, then it definitely does fall through.
430 if (Pred->empty())
431 return true;
Jia Liuf54f60f2012-02-28 07:46:26 +0000432
Akira Hatanakae625ba42011-04-01 18:57:38 +0000433 // Otherwise, check the last instruction.
434 // Check if the last terminator is an unconditional branch.
435 MachineBasicBlock::const_iterator I = Pred->end();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000436 while (I != Pred->begin() && !(--I)->isTerminator()) ;
Akira Hatanakae625ba42011-04-01 18:57:38 +0000437
Evan Cheng7f8e5632011-12-07 07:15:52 +0000438 return !I->isBarrier();
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000439}
440
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000441// Print out an operand for an inline asm expression.
Eric Christophered51b9e2012-05-10 21:48:22 +0000442bool MipsAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Chris Lattner3bb09762010-04-04 05:29:35 +0000443 unsigned AsmVariant,const char *ExtraCode,
444 raw_ostream &O) {
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000445 // Does this asm operand have a single letter operand modifier?
Eric Christophered51b9e2012-05-10 21:48:22 +0000446 if (ExtraCode && ExtraCode[0]) {
447 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000448
Eric Christophered51b9e2012-05-10 21:48:22 +0000449 const MachineOperand &MO = MI->getOperand(OpNum);
450 switch (ExtraCode[0]) {
Eric Christopherbc5d2492012-05-19 00:51:56 +0000451 default:
Jack Carterb2fd5f62012-06-21 17:14:46 +0000452 // See if this is a generic print operand
453 return AsmPrinter::PrintAsmOperand(MI,OpNum,AsmVariant,ExtraCode,O);
Eric Christopherbc5d2492012-05-19 00:51:56 +0000454 case 'X': // hex const int
455 if ((MO.getType()) != MachineOperand::MO_Immediate)
456 return true;
457 O << "0x" << StringRef(utohexstr(MO.getImm())).lower();
458 return false;
459 case 'x': // hex const int (low 16 bits)
460 if ((MO.getType()) != MachineOperand::MO_Immediate)
461 return true;
462 O << "0x" << StringRef(utohexstr(MO.getImm() & 0xffff)).lower();
463 return false;
464 case 'd': // decimal const int
465 if ((MO.getType()) != MachineOperand::MO_Immediate)
466 return true;
467 O << MO.getImm();
468 return false;
Eric Christopherf481ab32012-05-30 19:05:19 +0000469 case 'm': // decimal const int minus 1
470 if ((MO.getType()) != MachineOperand::MO_Immediate)
471 return true;
472 O << MO.getImm() - 1;
473 return false;
Jack Carter27747b52012-06-28 20:46:26 +0000474 case 'z': {
475 // $0 if zero, regular printing otherwise
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000476 if (MO.getType() != MachineOperand::MO_Immediate)
477 return true;
478 int64_t Val = MO.getImm();
479 if (Val)
480 O << Val;
481 else
482 O << "$0";
483 return false;
484 }
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000485 case 'D': // Second part of a double word register operand
486 case 'L': // Low order register of a double word register operand
Jack Cartera62ba822012-07-18 06:41:36 +0000487 case 'M': // High order register of a double word register operand
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000488 {
Jack Carterb2af5122012-07-05 23:58:21 +0000489 if (OpNum == 0)
490 return true;
491 const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
492 if (!FlagsOP.isImm())
493 return true;
494 unsigned Flags = FlagsOP.getImm();
495 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Jack Carter2ab73b12012-07-06 02:44:22 +0000496 // Number of registers represented by this operand. We are looking
497 // for 2 for 32 bit mode and 1 for 64 bit mode.
Jack Carterb2af5122012-07-05 23:58:21 +0000498 if (NumVals != 2) {
Jack Carter2ab73b12012-07-06 02:44:22 +0000499 if (Subtarget->isGP64bit() && NumVals == 1 && MO.isReg()) {
Jack Carterb2af5122012-07-05 23:58:21 +0000500 unsigned Reg = MO.getReg();
501 O << '$' << MipsInstPrinter::getRegisterName(Reg);
502 return false;
503 }
504 return true;
505 }
Jack Carter42ebf982012-07-11 21:41:49 +0000506
507 unsigned RegOp = OpNum;
508 if (!Subtarget->isGP64bit()){
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000509 // Endianess reverses which register holds the high or low value
Jack Cartera62ba822012-07-18 06:41:36 +0000510 // between M and L.
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000511 switch(ExtraCode[0]) {
Jack Cartera62ba822012-07-18 06:41:36 +0000512 case 'M':
513 RegOp = (Subtarget->isLittle()) ? OpNum + 1 : OpNum;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000514 break;
515 case 'L':
Jack Cartera62ba822012-07-18 06:41:36 +0000516 RegOp = (Subtarget->isLittle()) ? OpNum : OpNum + 1;
517 break;
518 case 'D': // Always the second part
519 RegOp = OpNum + 1;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000520 }
521 if (RegOp >= MI->getNumOperands())
522 return true;
523 const MachineOperand &MO = MI->getOperand(RegOp);
524 if (!MO.isReg())
525 return true;
526 unsigned Reg = MO.getReg();
527 O << '$' << MipsInstPrinter::getRegisterName(Reg);
528 return false;
Jack Carterb2af5122012-07-05 23:58:21 +0000529 }
Eric Christophered51b9e2012-05-10 21:48:22 +0000530 }
Daniel Sanders8b59af12013-11-12 12:56:01 +0000531 case 'w':
532 // Print MSA registers for the 'f' constraint
533 // In LLVM, the 'w' modifier doesn't need to do anything.
534 // We can just call printOperand as normal.
535 break;
Jack Carter2ab73b12012-07-06 02:44:22 +0000536 }
537 }
Eric Christophered51b9e2012-05-10 21:48:22 +0000538
539 printOperand(MI, OpNum, O);
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000540 return false;
541}
542
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000543bool MipsAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
544 unsigned OpNum, unsigned AsmVariant,
545 const char *ExtraCode,
546 raw_ostream &O) {
Jack Carterb04e3572013-04-09 23:19:50 +0000547 int Offset = 0;
548 // Currently we are expecting either no ExtraCode or 'D'
549 if (ExtraCode) {
550 if (ExtraCode[0] == 'D')
551 Offset = 4;
552 else
553 return true; // Unknown modifier.
554 }
Jia Liuf54f60f2012-02-28 07:46:26 +0000555
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000556 const MachineOperand &MO = MI->getOperand(OpNum);
557 assert(MO.isReg() && "unexpected inline asm memory operand");
Jack Carterb04e3572013-04-09 23:19:50 +0000558 O << Offset << "($" << MipsInstPrinter::getRegisterName(MO.getReg()) << ")";
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000559
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000560 return false;
561}
562
Chris Lattner76c564b2010-04-04 04:47:45 +0000563void MipsAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
564 raw_ostream &O) {
Eric Christopherd9134482014-08-04 21:25:23 +0000565 const DataLayout *DL = TM.getSubtargetImpl()->getDataLayout();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000566 const MachineOperand &MO = MI->getOperand(opNum);
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +0000567 bool closeP = false;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000568
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000569 if (MO.getTargetFlags())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000570 closeP = true;
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000571
572 switch(MO.getTargetFlags()) {
573 case MipsII::MO_GPREL: O << "%gp_rel("; break;
574 case MipsII::MO_GOT_CALL: O << "%call16("; break;
Akira Hatanaka56d9ef52011-04-01 21:41:06 +0000575 case MipsII::MO_GOT: O << "%got("; break;
576 case MipsII::MO_ABS_HI: O << "%hi("; break;
577 case MipsII::MO_ABS_LO: O << "%lo("; break;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +0000578 case MipsII::MO_TLSGD: O << "%tlsgd("; break;
579 case MipsII::MO_GOTTPREL: O << "%gottprel("; break;
580 case MipsII::MO_TPREL_HI: O << "%tprel_hi("; break;
581 case MipsII::MO_TPREL_LO: O << "%tprel_lo("; break;
Akira Hatanaka25ce3642011-09-22 03:09:07 +0000582 case MipsII::MO_GPOFF_HI: O << "%hi(%neg(%gp_rel("; break;
583 case MipsII::MO_GPOFF_LO: O << "%lo(%neg(%gp_rel("; break;
584 case MipsII::MO_GOT_DISP: O << "%got_disp("; break;
585 case MipsII::MO_GOT_PAGE: O << "%got_page("; break;
586 case MipsII::MO_GOT_OFST: O << "%got_ofst("; break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000587 }
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000588
Chris Lattnereb2cc682009-09-13 20:31:40 +0000589 switch (MO.getType()) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000590 case MachineOperand::MO_Register:
Akira Hatanaka9c6028f2011-07-07 23:56:50 +0000591 O << '$'
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000592 << StringRef(MipsInstPrinter::getRegisterName(MO.getReg())).lower();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000593 break;
594
595 case MachineOperand::MO_Immediate:
Akira Hatanaka2db176c2011-05-24 21:22:21 +0000596 O << MO.getImm();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000597 break;
598
599 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner29bdac42010-03-13 21:04:28 +0000600 O << *MO.getMBB()->getSymbol();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000601 return;
602
603 case MachineOperand::MO_GlobalAddress:
Rafael Espindola79858aa2013-10-29 17:07:16 +0000604 O << *getSymbol(MO.getGlobal());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000605 break;
606
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000607 case MachineOperand::MO_BlockAddress: {
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000608 MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000609 O << BA->getName();
610 break;
611 }
612
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000613 case MachineOperand::MO_ConstantPoolIndex:
Rafael Espindola58873562014-01-03 19:21:54 +0000614 O << DL->getPrivateGlobalPrefix() << "CPI"
Chris Lattnera5bb3702007-12-30 23:10:15 +0000615 << getFunctionNumber() << "_" << MO.getIndex();
Bruno Cardoso Lopes4713b282009-11-19 06:06:13 +0000616 if (MO.getOffset())
617 O << "+" << MO.getOffset();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000618 break;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000619
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000620 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000621 llvm_unreachable("<unknown operand type>");
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000622 }
623
624 if (closeP) O << ")";
625}
626
Chris Lattner76c564b2010-04-04 04:47:45 +0000627void MipsAsmPrinter::printUnsignedImm(const MachineInstr *MI, int opNum,
628 raw_ostream &O) {
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000629 const MachineOperand &MO = MI->getOperand(opNum);
Devang Patel12f68552010-04-27 22:24:37 +0000630 if (MO.isImm())
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000631 O << (unsigned short int)MO.getImm();
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000632 else
Chris Lattner76c564b2010-04-04 04:47:45 +0000633 printOperand(MI, opNum, O);
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000634}
635
Daniel Sanders3f6eb542013-11-12 10:45:18 +0000636void MipsAsmPrinter::printUnsignedImm8(const MachineInstr *MI, int opNum,
637 raw_ostream &O) {
638 const MachineOperand &MO = MI->getOperand(opNum);
639 if (MO.isImm())
640 O << (unsigned short int)(unsigned char)MO.getImm();
641 else
642 printOperand(MI, opNum, O);
643}
644
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000645void MipsAsmPrinter::
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000646printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O) {
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000647 // Load/Store memory operands -- imm($reg)
648 // If PIC target the target is loaded as the
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +0000649 // pattern lw $25,%call16($28)
Chris Lattner76c564b2010-04-04 04:47:45 +0000650 printOperand(MI, opNum+1, O);
Akira Hatanaka2e766ed2011-07-07 18:57:00 +0000651 O << "(";
652 printOperand(MI, opNum, O);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000653 O << ")";
654}
655
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000656void MipsAsmPrinter::
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000657printMemOperandEA(const MachineInstr *MI, int opNum, raw_ostream &O) {
658 // when using stack locations for not load/store instructions
659 // print the same way as all normal 3 operand instructions.
660 printOperand(MI, opNum, O);
661 O << ", ";
662 printOperand(MI, opNum+1, O);
663 return;
664}
665
666void MipsAsmPrinter::
Chris Lattner76c564b2010-04-04 04:47:45 +0000667printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
668 const char *Modifier) {
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000669 const MachineOperand &MO = MI->getOperand(opNum);
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000670 O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000671}
672
Bob Wilsonb633d7a2009-09-30 22:06:26 +0000673void MipsAsmPrinter::EmitStartOfAsmFile(Module &M) {
Daniel Sanders35837ac2014-08-08 10:01:29 +0000674 bool IsABICalls = Subtarget->isABICalls();
Daniel Sanders16fa1db2014-04-16 13:58:57 +0000675 if (IsABICalls) {
676 getTargetStreamer().emitDirectiveAbiCalls();
Eric Christopher8ef7a6a2014-07-18 00:08:53 +0000677 Reloc::Model RM = TM.getRelocationModel();
Daniel Sanders16fa1db2014-04-16 13:58:57 +0000678 // FIXME: This condition should be a lot more complicated that it is here.
679 // Ideally it should test for properties of the ABI and not the ABI
680 // itself.
681 // For the moment, I'm only correcting enough to make MIPS-IV work.
682 if (RM == Reloc::Static && !Subtarget->isABI_N64())
683 getTargetStreamer().emitDirectiveOptionPic0();
684 }
Jack Carterf9f753c2013-06-18 19:47:15 +0000685
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000686 // Tell the assembler which ABI we are using
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000687 std::string SectionName = std::string(".mdebug.") + getCurrentABIString();
688 OutStreamer.SwitchSection(OutContext.getELFSection(
689 SectionName, ELF::SHT_PROGBITS, 0, SectionKind::getDataRel()));
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000690
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000691 // NaN: At the moment we only support:
692 // 1. .nan legacy (default)
693 // 2. .nan 2008
694 Subtarget->isNaN2008() ? getTargetStreamer().emitDirectiveNaN2008()
695 : getTargetStreamer().emitDirectiveNaNLegacy();
696
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000697 // TODO: handle O64 ABI
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000698
699 if (Subtarget->isABI_EABI()) {
700 if (Subtarget->isGP32bit())
701 OutStreamer.SwitchSection(
702 OutContext.getELFSection(".gcc_compiled_long32", ELF::SHT_PROGBITS, 0,
703 SectionKind::getDataRel()));
704 else
705 OutStreamer.SwitchSection(
706 OutContext.getELFSection(".gcc_compiled_long64", ELF::SHT_PROGBITS, 0,
707 SectionKind::getDataRel()));
Benjamin Kramer0151d7b2010-04-05 10:17:15 +0000708 }
Daniel Sanders7e527422014-07-10 13:38:23 +0000709
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000710 getTargetStreamer().updateABIInfo(*Subtarget);
Daniel Sanders7e527422014-07-10 13:38:23 +0000711
Daniel Sanderse22244b2014-07-21 15:25:24 +0000712 // We should always emit a '.module fp=...' but binutils 2.24 does not accept
713 // it. We therefore emit it when it contradicts the ABI defaults (-mfpxx or
714 // -mfp64) and omit it otherwise.
715 if (Subtarget->isABI_O32() && (Subtarget->isABI_FPXX() ||
716 Subtarget->isFP64bit()))
717 getTargetStreamer().emitDirectiveModuleFP();
718
719 // We should always emit a '.module [no]oddspreg' but binutils 2.24 does not
720 // accept it. We therefore emit it when it contradicts the default or an
721 // option has changed the default (i.e. FPXX) and omit it otherwise.
722 if (Subtarget->isABI_O32() && (!Subtarget->useOddSPReg() ||
723 Subtarget->isABI_FPXX()))
724 getTargetStreamer().emitDirectiveModuleOddSPReg(Subtarget->useOddSPReg(),
725 Subtarget->isABI_O32());
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000726}
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000727
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000728void MipsAsmPrinter::EmitJal(MCSymbol *Symbol) {
729 MCInst I;
730 I.setOpcode(Mips::JAL);
731 I.addOperand(
732 MCOperand::CreateExpr(MCSymbolRefExpr::Create(Symbol, OutContext)));
733 OutStreamer.EmitInstruction(I, getSubtargetInfo());
734}
735
736void MipsAsmPrinter::EmitInstrReg(unsigned Opcode, unsigned Reg) {
737 MCInst I;
738 I.setOpcode(Opcode);
739 I.addOperand(MCOperand::CreateReg(Reg));
740 OutStreamer.EmitInstruction(I, getSubtargetInfo());
741}
742
743void MipsAsmPrinter::EmitInstrRegReg(unsigned Opcode, unsigned Reg1,
744 unsigned Reg2) {
745 MCInst I;
746 //
747 // Because of the current td files for Mips32, the operands for MTC1
748 // appear backwards from their normal assembly order. It's not a trivial
749 // change to fix this in the td file so we adjust for it here.
750 //
751 if (Opcode == Mips::MTC1) {
752 unsigned Temp = Reg1;
753 Reg1 = Reg2;
754 Reg2 = Temp;
755 }
756 I.setOpcode(Opcode);
757 I.addOperand(MCOperand::CreateReg(Reg1));
758 I.addOperand(MCOperand::CreateReg(Reg2));
759 OutStreamer.EmitInstruction(I, getSubtargetInfo());
760}
761
762void MipsAsmPrinter::EmitInstrRegRegReg(unsigned Opcode, unsigned Reg1,
763 unsigned Reg2, unsigned Reg3) {
764 MCInst I;
765 I.setOpcode(Opcode);
766 I.addOperand(MCOperand::CreateReg(Reg1));
767 I.addOperand(MCOperand::CreateReg(Reg2));
768 I.addOperand(MCOperand::CreateReg(Reg3));
769 OutStreamer.EmitInstruction(I, getSubtargetInfo());
770}
771
772void MipsAsmPrinter::EmitMovFPIntPair(unsigned MovOpc, unsigned Reg1,
773 unsigned Reg2, unsigned FPReg1,
774 unsigned FPReg2, bool LE) {
775 if (!LE) {
776 unsigned temp = Reg1;
777 Reg1 = Reg2;
778 Reg2 = temp;
779 }
780 EmitInstrRegReg(MovOpc, Reg1, FPReg1);
781 EmitInstrRegReg(MovOpc, Reg2, FPReg2);
782}
783
784void MipsAsmPrinter::EmitSwapFPIntParams(Mips16HardFloatInfo::FPParamVariant PV,
785 bool LE, bool ToFP) {
786 using namespace Mips16HardFloatInfo;
787 unsigned MovOpc = ToFP ? Mips::MTC1 : Mips::MFC1;
788 switch (PV) {
789 case FSig:
790 EmitInstrRegReg(MovOpc, Mips::A0, Mips::F12);
791 break;
792 case FFSig:
793 EmitMovFPIntPair(MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F14, LE);
794 break;
795 case FDSig:
796 EmitInstrRegReg(MovOpc, Mips::A0, Mips::F12);
797 EmitMovFPIntPair(MovOpc, Mips::A2, Mips::A3, Mips::F14, Mips::F15, LE);
798 break;
799 case DSig:
800 EmitMovFPIntPair(MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
801 break;
802 case DDSig:
803 EmitMovFPIntPair(MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
804 EmitMovFPIntPair(MovOpc, Mips::A2, Mips::A3, Mips::F14, Mips::F15, LE);
805 break;
806 case DFSig:
807 EmitMovFPIntPair(MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
808 EmitInstrRegReg(MovOpc, Mips::A2, Mips::F14);
809 break;
810 case NoSig:
811 return;
812 }
813}
814
815void
816MipsAsmPrinter::EmitSwapFPIntRetval(Mips16HardFloatInfo::FPReturnVariant RV,
817 bool LE) {
818 using namespace Mips16HardFloatInfo;
819 unsigned MovOpc = Mips::MFC1;
820 switch (RV) {
821 case FRet:
822 EmitInstrRegReg(MovOpc, Mips::V0, Mips::F0);
823 break;
824 case DRet:
825 EmitMovFPIntPair(MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
826 break;
827 case CFRet:
828 EmitMovFPIntPair(MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
829 break;
830 case CDRet:
831 EmitMovFPIntPair(MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
832 EmitMovFPIntPair(MovOpc, Mips::A0, Mips::A1, Mips::F2, Mips::F3, LE);
833 break;
834 case NoFPRet:
835 break;
836 }
837}
838
839void MipsAsmPrinter::EmitFPCallStub(
840 const char *Symbol, const Mips16HardFloatInfo::FuncSignature *Signature) {
841 MCSymbol *MSymbol = OutContext.GetOrCreateSymbol(StringRef(Symbol));
842 using namespace Mips16HardFloatInfo;
843 bool LE = Subtarget->isLittle();
844 //
845 // .global xxxx
846 //
847 OutStreamer.EmitSymbolAttribute(MSymbol, MCSA_Global);
848 const char *RetType;
849 //
850 // make the comment field identifying the return and parameter
851 // types of the floating point stub
852 // # Stub function to call rettype xxxx (params)
853 //
854 switch (Signature->RetSig) {
855 case FRet:
856 RetType = "float";
857 break;
858 case DRet:
859 RetType = "double";
860 break;
861 case CFRet:
862 RetType = "complex";
863 break;
864 case CDRet:
865 RetType = "double complex";
866 break;
867 case NoFPRet:
868 RetType = "";
869 break;
870 }
871 const char *Parms;
872 switch (Signature->ParamSig) {
873 case FSig:
874 Parms = "float";
875 break;
876 case FFSig:
877 Parms = "float, float";
878 break;
879 case FDSig:
880 Parms = "float, double";
881 break;
882 case DSig:
883 Parms = "double";
884 break;
885 case DDSig:
886 Parms = "double, double";
887 break;
888 case DFSig:
889 Parms = "double, float";
890 break;
891 case NoSig:
892 Parms = "";
893 break;
894 }
895 OutStreamer.AddComment("\t# Stub function to call " + Twine(RetType) + " " +
896 Twine(Symbol) + " (" + Twine(Parms) + ")");
897 //
898 // probably not necessary but we save and restore the current section state
899 //
900 OutStreamer.PushSection();
901 //
902 // .section mips16.call.fpxxxx,"ax",@progbits
903 //
904 const MCSectionELF *M = OutContext.getELFSection(
905 ".mips16.call.fp." + std::string(Symbol), ELF::SHT_PROGBITS,
906 ELF::SHF_ALLOC | ELF::SHF_EXECINSTR, SectionKind::getText());
Craig Topper062a2ba2014-04-25 05:30:21 +0000907 OutStreamer.SwitchSection(M, nullptr);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000908 //
909 // .align 2
910 //
911 OutStreamer.EmitValueToAlignment(4);
912 MipsTargetStreamer &TS = getTargetStreamer();
913 //
914 // .set nomips16
915 // .set nomicromips
916 //
917 TS.emitDirectiveSetNoMips16();
918 TS.emitDirectiveSetNoMicroMips();
919 //
920 // .ent __call_stub_fp_xxxx
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000921 // .type __call_stub_fp_xxxx,@function
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000922 // __call_stub_fp_xxxx:
923 //
924 std::string x = "__call_stub_fp_" + std::string(Symbol);
925 MCSymbol *Stub = OutContext.GetOrCreateSymbol(StringRef(x));
926 TS.emitDirectiveEnt(*Stub);
927 MCSymbol *MType =
928 OutContext.GetOrCreateSymbol("__call_stub_fp_" + Twine(Symbol));
929 OutStreamer.EmitSymbolAttribute(MType, MCSA_ELF_TypeFunction);
930 OutStreamer.EmitLabel(Stub);
931 //
932 // we just handle non pic for now. these function will not be
933 // called otherwise. when the full stub generation is moved here
934 // we need to deal with pic.
935 //
936 if (Subtarget->getRelocationModel() == Reloc::PIC_)
937 llvm_unreachable("should not be here if we are compiling pic");
938 TS.emitDirectiveSetReorder();
939 //
940 // We need to add a MipsMCExpr class to MCTargetDesc to fully implement
941 // stubs without raw text but this current patch is for compiler generated
942 // functions and they all return some value.
943 // The calling sequence for non pic is different in that case and we need
944 // to implement %lo and %hi in order to handle the case of no return value
945 // See the corresponding method in Mips16HardFloat for details.
946 //
947 // mov the return address to S2.
948 // we have no stack space to store it and we are about to make another call.
949 // We need to make sure that the enclosing function knows to save S2
950 // This should have already been handled.
951 //
952 // Mov $18, $31
953
954 EmitInstrRegRegReg(Mips::ADDu, Mips::S2, Mips::RA, Mips::ZERO);
955
956 EmitSwapFPIntParams(Signature->ParamSig, LE, true);
957
958 // Jal xxxx
959 //
960 EmitJal(MSymbol);
961
962 // fix return values
963 EmitSwapFPIntRetval(Signature->RetSig, LE);
964 //
965 // do the return
966 // if (Signature->RetSig == NoFPRet)
967 // llvm_unreachable("should not be any stubs here with no return value");
968 // else
969 EmitInstrReg(Mips::JR, Mips::S2);
970
971 MCSymbol *Tmp = OutContext.CreateTempSymbol();
972 OutStreamer.EmitLabel(Tmp);
973 const MCSymbolRefExpr *E = MCSymbolRefExpr::Create(Stub, OutContext);
974 const MCSymbolRefExpr *T = MCSymbolRefExpr::Create(Tmp, OutContext);
975 const MCExpr *T_min_E = MCBinaryExpr::CreateSub(T, E, OutContext);
976 OutStreamer.EmitELFSize(Stub, T_min_E);
977 TS.emitDirectiveEnd(x);
978 OutStreamer.PopSection();
979}
980
981void MipsAsmPrinter::EmitEndOfAsmFile(Module &M) {
982 // Emit needed stubs
983 //
984 for (std::map<
985 const char *,
986 const llvm::Mips16HardFloatInfo::FuncSignature *>::const_iterator
987 it = StubsNeeded.begin();
988 it != StubsNeeded.end(); ++it) {
989 const char *Symbol = it->first;
990 const llvm::Mips16HardFloatInfo::FuncSignature *Signature = it->second;
991 EmitFPCallStub(Symbol, Signature);
992 }
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000993 // return to the text section
994 OutStreamer.SwitchSection(OutContext.getObjectFileInfo()->getTextSection());
Jack Carterc1b17ed2013-01-18 21:20:38 +0000995}
996
Akira Hatanakaf2bcad92011-07-01 01:04:43 +0000997void MipsAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
998 raw_ostream &OS) {
999 // TODO: implement
1000}
1001
Sasa Stankovic8c5736b2014-02-28 10:00:38 +00001002// Align all targets of indirect branches on bundle size. Used only if target
1003// is NaCl.
1004void MipsAsmPrinter::NaClAlignIndirectJumpTargets(MachineFunction &MF) {
1005 // Align all blocks that are jumped to through jump table.
1006 if (MachineJumpTableInfo *JtInfo = MF.getJumpTableInfo()) {
1007 const std::vector<MachineJumpTableEntry> &JT = JtInfo->getJumpTables();
1008 for (unsigned I = 0; I < JT.size(); ++I) {
1009 const std::vector<MachineBasicBlock*> &MBBs = JT[I].MBBs;
1010
1011 for (unsigned J = 0; J < MBBs.size(); ++J)
1012 MBBs[J]->setAlignment(MIPS_NACL_BUNDLE_ALIGN);
1013 }
1014 }
1015
1016 // If basic block address is taken, block can be target of indirect branch.
1017 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
1018 MBB != E; ++MBB) {
1019 if (MBB->hasAddressTaken())
1020 MBB->setAlignment(MIPS_NACL_BUNDLE_ALIGN);
1021 }
1022}
1023
Sasa Stankovic7b061a42014-04-30 15:06:25 +00001024bool MipsAsmPrinter::isLongBranchPseudo(int Opcode) const {
1025 return (Opcode == Mips::LONG_BRANCH_LUi
1026 || Opcode == Mips::LONG_BRANCH_ADDiu
Sasa Stankovic7b061a42014-04-30 15:06:25 +00001027 || Opcode == Mips::LONG_BRANCH_DADDiu);
1028}
1029
Bob Wilson5a495fe2009-06-23 23:59:40 +00001030// Force static initialization.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +00001031extern "C" void LLVMInitializeMipsAsmPrinter() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +00001032 RegisterAsmPrinter<MipsAsmPrinter> X(TheMipsTarget);
1033 RegisterAsmPrinter<MipsAsmPrinter> Y(TheMipselTarget);
Akira Hatanaka3d673cc2011-09-21 03:00:58 +00001034 RegisterAsmPrinter<MipsAsmPrinter> A(TheMips64Target);
1035 RegisterAsmPrinter<MipsAsmPrinter> B(TheMips64elTarget);
Daniel Dunbare8338102009-07-15 20:24:03 +00001036}