blob: c9b56aa6352feae77452b8668d2431a76dbd2442 [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
15#define DEBUG_TYPE "mips-asm-printer"
Akira Hatanaka9c6028f2011-07-07 23:56:50 +000016#include "InstPrinter/MipsInstPrinter.h"
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000017#include "MCTargetDesc/MipsBaseInfo.h"
Sasa Stankovic8c5736b2014-02-28 10:00:38 +000018#include "MCTargetDesc/MipsMCNaCl.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "Mips.h"
Jack Carterc1b17ed2013-01-18 21:20:38 +000020#include "MipsAsmPrinter.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "MipsInstrInfo.h"
22#include "MipsMCInstLower.h"
Rafael Espindolaa17151a2013-10-08 13:08:17 +000023#include "MipsTargetStreamer.h"
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +000024#include "llvm/ADT/SmallString.h"
25#include "llvm/ADT/StringExtras.h"
26#include "llvm/ADT/Twine.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000027#include "llvm/CodeGen/MachineConstantPool.h"
Bruno Cardoso Lopesbcda5e22007-07-11 23:24:41 +000028#include "llvm/CodeGen/MachineFrameInfo.h"
Jack Carterb2af5122012-07-05 23:58:21 +000029#include "llvm/CodeGen/MachineFunctionPass.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000030#include "llvm/CodeGen/MachineInstr.h"
Sasa Stankovic8c5736b2014-02-28 10:00:38 +000031#include "llvm/CodeGen/MachineJumpTableInfo.h"
Akira Hatanaka2fcc1cf2011-08-12 21:30:06 +000032#include "llvm/CodeGen/MachineMemOperand.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000033#include "llvm/IR/BasicBlock.h"
34#include "llvm/IR/DataLayout.h"
35#include "llvm/IR/InlineAsm.h"
36#include "llvm/IR/Instructions.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000037#include "llvm/IR/Mangler.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000038#include "llvm/MC/MCAsmInfo.h"
Rafael Espindola2ab7ea72014-01-27 01:33:33 +000039#include "llvm/MC/MCContext.h"
Rafael Espindolaac4ad252013-10-05 16:42:21 +000040#include "llvm/MC/MCELFStreamer.h"
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000041#include "llvm/MC/MCExpr.h"
Akira Hatanaka9c6028f2011-07-07 23:56:50 +000042#include "llvm/MC/MCInst.h"
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000043#include "llvm/MC/MCSection.h"
Rafael Espindola2ab7ea72014-01-27 01:33:33 +000044#include "llvm/MC/MCSectionELF.h"
Chris Lattner4cd44982009-09-13 17:14:04 +000045#include "llvm/MC/MCSymbol.h"
Jack Carterab3cb422013-02-19 22:04:37 +000046#include "llvm/Support/ELF.h"
Jack Carterb2af5122012-07-05 23:58:21 +000047#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000048#include "llvm/Support/raw_ostream.h"
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +000049#include "llvm/Target/TargetLoweringObjectFile.h"
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +000050#include "llvm/Target/TargetOptions.h"
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000051#include <string>
Akira Hatanakaf2bcad92011-07-01 01:04:43 +000052
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000053using namespace llvm;
54
Rafael Espindolaa17151a2013-10-08 13:08:17 +000055MipsTargetStreamer &MipsAsmPrinter::getTargetStreamer() {
Rafael Espindola4a1a3602014-01-14 01:21:46 +000056 return static_cast<MipsTargetStreamer &>(*OutStreamer.getTargetStreamer());
Rafael Espindolaa17151a2013-10-08 13:08:17 +000057}
58
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000059bool MipsAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Reed Kotler1595f362013-04-09 19:46:01 +000060 // Initialize TargetLoweringObjectFile.
61 if (Subtarget->allowMixed16_32())
62 const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
63 .Initialize(OutContext, TM);
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000064 MipsFI = MF.getInfo<MipsFunctionInfo>();
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000065 if (Subtarget->inMips16Mode())
66 for (std::map<
67 const char *,
68 const llvm::Mips16HardFloatInfo::FuncSignature *>::const_iterator
69 it = MipsFI->StubsNeeded.begin();
70 it != MipsFI->StubsNeeded.end(); ++it) {
71 const char *Symbol = it->first;
72 const llvm::Mips16HardFloatInfo::FuncSignature *Signature = it->second;
73 if (StubsNeeded.find(Symbol) == StubsNeeded.end())
74 StubsNeeded[Symbol] = Signature;
75 }
Reed Kotler91ae9822013-10-27 21:57:36 +000076 MCP = MF.getConstantPool();
Sasa Stankovic8c5736b2014-02-28 10:00:38 +000077
78 // In NaCl, all indirect jump targets must be aligned to bundle size.
79 if (Subtarget->isTargetNaCl())
80 NaClAlignIndirectJumpTargets(MF);
81
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000082 AsmPrinter::runOnMachineFunction(MF);
83 return true;
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +000084}
85
Akira Hatanaka42a35242012-09-27 01:59:07 +000086bool MipsAsmPrinter::lowerOperand(const MachineOperand &MO, MCOperand &MCOp) {
87 MCOp = MCInstLowering.LowerOperand(MO);
88 return MCOp.isValid();
89}
90
91#include "MipsGenMCPseudoLowering.inc"
92
Akira Hatanakaddd12652011-07-07 20:10:52 +000093void MipsAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Akira Hatanakaddd12652011-07-07 20:10:52 +000094 if (MI->isDebugValue()) {
Bruno Cardoso Lopescd1d4472011-12-30 21:09:41 +000095 SmallString<128> Str;
96 raw_svector_ostream OS(Str);
97
Akira Hatanakaddd12652011-07-07 20:10:52 +000098 PrintDebugValueComment(MI, OS);
99 return;
100 }
101
Reed Kotler91ae9822013-10-27 21:57:36 +0000102 // If we just ended a constant pool, mark it as such.
103 if (InConstantPool && MI->getOpcode() != Mips::CONSTPOOL_ENTRY) {
104 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
105 InConstantPool = false;
106 }
107 if (MI->getOpcode() == Mips::CONSTPOOL_ENTRY) {
108 // CONSTPOOL_ENTRY - This instruction represents a floating
109 //constant pool in the function. The first operand is the ID#
110 // for this instruction, the second is the index into the
111 // MachineConstantPool that this is, the third is the size in
112 // bytes of this constant pool entry.
113 // The required alignment is specified on the basic block holding this MI.
114 //
115 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
116 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
117
118 // If this is the first entry of the pool, mark it.
119 if (!InConstantPool) {
120 OutStreamer.EmitDataRegion(MCDR_DataRegion);
121 InConstantPool = true;
122 }
123
124 OutStreamer.EmitLabel(GetCPISymbol(LabelId));
125
126 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
127 if (MCPE.isMachineConstantPoolEntry())
128 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
129 else
130 EmitGlobalConstant(MCPE.Val.ConstVal);
131 return;
132 }
133
Rafael Espindola14d02fe2014-01-25 15:06:56 +0000134
Akira Hatanaka5ac78682012-06-13 23:25:52 +0000135 MachineBasicBlock::const_instr_iterator I = MI;
136 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
137
138 do {
Akira Hatanaka556135d2013-02-06 21:50:15 +0000139 // Do any auto-generated pseudo lowerings.
140 if (emitPseudoExpansionLowering(OutStreamer, &*I))
141 continue;
Jack Carterc20a21b2012-08-28 19:07:39 +0000142
Reed Kotler76c9bcd2013-02-15 21:05:58 +0000143 // The inMips16Mode() test is not permanent.
144 // Some instructions are marked as pseudo right now which
145 // would make the test fail for the wrong reason but
146 // that will be fixed soon. We need this here because we are
147 // removing another test for this situation downstream in the
148 // callchain.
149 //
150 if (I->isPseudo() && !Subtarget->inMips16Mode())
151 llvm_unreachable("Pseudo opcode found in EmitInstruction()");
152
Akira Hatanaka556135d2013-02-06 21:50:15 +0000153 MCInst TmpInst0;
154 MCInstLowering.Lower(I, TmpInst0);
David Woodhousee6c13e42014-01-28 23:12:42 +0000155 EmitToStreamer(OutStreamer, TmpInst0);
Akira Hatanaka556135d2013-02-06 21:50:15 +0000156 } while ((++I != E) && I->isInsideBundle()); // Delay slot check
Akira Hatanakaddd12652011-07-07 20:10:52 +0000157}
158
Akira Hatanakae2489122011-04-15 21:51:11 +0000159//===----------------------------------------------------------------------===//
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000160//
161// Mips Asm Directives
162//
163// -- Frame directive "frame Stackpointer, Stacksize, RARegister"
164// Describe the stack frame.
165//
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000166// -- Mask directives "(f)mask bitmask, offset"
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000167// Tells the assembler which registers are saved and where.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000168// bitmask - contain a little endian bitset indicating which registers are
169// saved on function prologue (e.g. with a 0x80000000 mask, the
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000170// assembler knows the register 31 (RA) is saved at prologue.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000171// offset - the position before stack pointer subtraction indicating where
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000172// the first saved register on prologue is located. (e.g. with a
173//
174// Consider the following function prologue:
175//
Bill Wendling97925ec2008-02-27 06:33:05 +0000176// .frame $fp,48,$ra
177// .mask 0xc0000000,-8
178// addiu $sp, $sp, -48
179// sw $ra, 40($sp)
180// sw $fp, 36($sp)
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000181//
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000182// With a 0xc0000000 mask, the assembler knows the register 31 (RA) and
183// 30 (FP) are saved at prologue. As the save order on prologue is from
184// left to right, RA is saved first. A -8 offset means that after the
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000185// stack pointer subtration, the first register in the mask (RA) will be
186// saved at address 48-8=40.
187//
Akira Hatanakae2489122011-04-15 21:51:11 +0000188//===----------------------------------------------------------------------===//
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000189
Akira Hatanakae2489122011-04-15 21:51:11 +0000190//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000191// Mask directives
Akira Hatanakae2489122011-04-15 21:51:11 +0000192//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000193
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000194// Create a bitmask with all callee saved registers for CPU or Floating Point
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000195// registers. For CPU registers consider RA, GP and FP for saving if necessary.
Rafael Espindola25fa2912014-01-27 04:33:11 +0000196void MipsAsmPrinter::printSavedRegsBitmask() {
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000197 // CPU and FPU Saved Registers Bitmasks
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000198 unsigned CPUBitmask = 0, FPUBitmask = 0;
199 int CPUTopSavedRegOff, FPUTopSavedRegOff;
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000200
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000201 // Set the CPU and FPU Bitmasks
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000202 const MachineFrameInfo *MFI = MF->getFrameInfo();
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000203 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000204 // size of stack area to which FP callee-saved regs are saved.
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000205 unsigned CPURegSize = Mips::GPR32RegClass.getSize();
Craig Topperc7242e02012-04-20 07:30:17 +0000206 unsigned FGR32RegSize = Mips::FGR32RegClass.getSize();
207 unsigned AFGR64RegSize = Mips::AFGR64RegClass.getSize();
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000208 bool HasAFGR64Reg = false;
209 unsigned CSFPRegsSize = 0;
210 unsigned i, e = CSI.size();
211
212 // Set FPU Bitmask.
213 for (i = 0; i != e; ++i) {
Rafael Espindolaf2dffce2010-06-02 20:02:30 +0000214 unsigned Reg = CSI[i].getReg();
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000215 if (Mips::GPR32RegClass.contains(Reg))
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000216 break;
217
Akira Hatanaka5d6faed2012-12-10 20:04:40 +0000218 unsigned RegNum = TM.getRegisterInfo()->getEncodingValue(Reg);
Craig Topperc7242e02012-04-20 07:30:17 +0000219 if (Mips::AFGR64RegClass.contains(Reg)) {
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000220 FPUBitmask |= (3 << RegNum);
221 CSFPRegsSize += AFGR64RegSize;
222 HasAFGR64Reg = true;
223 continue;
224 }
225
226 FPUBitmask |= (1 << RegNum);
227 CSFPRegsSize += FGR32RegSize;
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000228 }
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000229
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000230 // Set CPU Bitmask.
231 for (; i != e; ++i) {
232 unsigned Reg = CSI[i].getReg();
Akira Hatanaka5d6faed2012-12-10 20:04:40 +0000233 unsigned RegNum = TM.getRegisterInfo()->getEncodingValue(Reg);
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000234 CPUBitmask |= (1 << RegNum);
235 }
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000236
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000237 // FP Regs are saved right below where the virtual frame pointer points to.
238 FPUTopSavedRegOff = FPUBitmask ?
239 (HasAFGR64Reg ? -AFGR64RegSize : -FGR32RegSize) : 0;
240
241 // CPU Regs are saved below FP Regs.
242 CPUTopSavedRegOff = CPUBitmask ? -CSFPRegsSize - CPURegSize : 0;
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000243
Rafael Espindola25fa2912014-01-27 04:33:11 +0000244 MipsTargetStreamer &TS = getTargetStreamer();
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000245 // Print CPUBitmask
Rafael Espindola25fa2912014-01-27 04:33:11 +0000246 TS.emitMask(CPUBitmask, CPUTopSavedRegOff);
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000247
248 // Print FPUBitmask
Rafael Espindola25fa2912014-01-27 04:33:11 +0000249 TS.emitFMask(FPUBitmask, FPUTopSavedRegOff);
Bruno Cardoso Lopesbcda5e22007-07-11 23:24:41 +0000250}
251
Akira Hatanakae2489122011-04-15 21:51:11 +0000252//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000253// Frame and Set directives
Akira Hatanakae2489122011-04-15 21:51:11 +0000254//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000255
256/// Frame Directive
Chris Lattner5e596182010-04-04 07:05:53 +0000257void MipsAsmPrinter::emitFrameDirective() {
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000258 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
259
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000260 unsigned stackReg = RI.getFrameRegister(*MF);
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000261 unsigned returnReg = RI.getRARegister();
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000262 unsigned stackSize = MF->getFrameInfo()->getStackSize();
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000263
Rafael Espindola054234f2014-01-27 03:53:56 +0000264 getTargetStreamer().emitFrame(stackReg, stackSize, returnReg);
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000265}
266
267/// Emit Set directives.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000268const char *MipsAsmPrinter::getCurrentABIString() const {
Chris Lattner5e596182010-04-04 07:05:53 +0000269 switch (Subtarget->getTargetABI()) {
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000270 case MipsSubtarget::O32: return "abi32";
Chris Lattner5e596182010-04-04 07:05:53 +0000271 case MipsSubtarget::N32: return "abiN32";
272 case MipsSubtarget::N64: return "abi64";
273 case MipsSubtarget::EABI: return "eabi32"; // TODO: handle eabi64
Dmitri Gribenkoca1e27b2012-09-10 21:26:47 +0000274 default: llvm_unreachable("Unknown Mips ABI");
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000275 }
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000276}
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000277
Chris Lattner5d9fb4b2010-01-27 23:23:58 +0000278void MipsAsmPrinter::EmitFunctionEntryLabel() {
Rafael Espindola6633d572014-01-14 18:57:12 +0000279 MipsTargetStreamer &TS = getTargetStreamer();
Sasa Stankovic8c5736b2014-02-28 10:00:38 +0000280
281 // NaCl sandboxing requires that indirect call instructions are masked.
282 // This means that function entry points should be bundle-aligned.
283 if (Subtarget->isTargetNaCl())
284 EmitAlignment(std::max(MF->getAlignment(), MIPS_NACL_BUNDLE_ALIGN));
285
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000286 if (Subtarget->inMicroMipsMode())
Rafael Espindola6633d572014-01-14 18:57:12 +0000287 TS.emitDirectiveSetMicroMips();
Matheus Almeidadc7e48e2014-04-16 11:46:59 +0000288 else
289 TS.emitDirectiveSetNoMicroMips();
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000290
Rafael Espindola6633d572014-01-14 18:57:12 +0000291 if (Subtarget->inMips16Mode())
292 TS.emitDirectiveSetMips16();
293 else
294 TS.emitDirectiveSetNoMips16();
Jack Carterab3cb422013-02-19 22:04:37 +0000295
Rafael Espindola6633d572014-01-14 18:57:12 +0000296 TS.emitDirectiveEnt(*CurrentFnSym);
Chris Lattner5d9fb4b2010-01-27 23:23:58 +0000297 OutStreamer.EmitLabel(CurrentFnSym);
298}
299
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000300/// EmitFunctionBodyStart - Targets can override this to emit stuff before
301/// the first basic block in the function.
302void MipsAsmPrinter::EmitFunctionBodyStart() {
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000303 MipsTargetStreamer &TS = getTargetStreamer();
304
Rafael Espindola7d78b2a2013-10-29 16:24:21 +0000305 MCInstLowering.Initialize(&MF->getContext());
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +0000306
Reed Kotler0f2b10e2013-05-03 23:17:24 +0000307 bool IsNakedFunction =
308 MF->getFunction()->
309 getAttributes().hasAttribute(AttributeSet::FunctionIndex,
310 Attribute::Naked);
311 if (!IsNakedFunction)
312 emitFrameDirective();
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000313
Rafael Espindola25fa2912014-01-27 04:33:11 +0000314 if (!IsNakedFunction)
315 printSavedRegsBitmask();
316
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000317 if (!Subtarget->inMips16Mode()) {
318 TS.emitDirectiveSetNoReorder();
319 TS.emitDirectiveSetNoMacro();
320 TS.emitDirectiveSetNoAt();
Akira Hatanaka8f3573032012-05-12 00:48:43 +0000321 }
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000322}
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000323
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000324/// EmitFunctionBodyEnd - Targets can override this to emit stuff after
325/// the last basic block in the function.
326void MipsAsmPrinter::EmitFunctionBodyEnd() {
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000327 MipsTargetStreamer &TS = getTargetStreamer();
328
Chris Lattnerfd97a332010-01-28 01:48:52 +0000329 // There are instruction for this macros, but they must
330 // always be at the function end, and we can't emit and
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000331 // break with BB logic.
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000332 if (!Subtarget->inMips16Mode()) {
333 TS.emitDirectiveSetAt();
334 TS.emitDirectiveSetMacro();
335 TS.emitDirectiveSetReorder();
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000336 }
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000337 TS.emitDirectiveEnd(CurrentFnSym->getName());
Reed Kotler91ae9822013-10-27 21:57:36 +0000338 // Make sure to terminate any constant pools that were at the end
339 // of the function.
340 if (!InConstantPool)
341 return;
342 InConstantPool = false;
343 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000344}
345
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000346/// isBlockOnlyReachableByFallthough - Return true if the basic block has
347/// exactly one predecessor and the control transfer mechanism between
348/// the predecessor and this block is a fall-through.
Akira Hatanakae2489122011-04-15 21:51:11 +0000349bool MipsAsmPrinter::isBlockOnlyReachableByFallthrough(const MachineBasicBlock*
350 MBB) const {
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000351 // The predecessor has to be immediately before this block.
352 const MachineBasicBlock *Pred = *MBB->pred_begin();
353
354 // If the predecessor is a switch statement, assume a jump table
355 // implementation, so it is not a fall through.
356 if (const BasicBlock *bb = Pred->getBasicBlock())
357 if (isa<SwitchInst>(bb->getTerminator()))
358 return false;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000359
Akira Hatanakae625ba42011-04-01 18:57:38 +0000360 // If this is a landing pad, it isn't a fall through. If it has no preds,
361 // then nothing falls through to it.
362 if (MBB->isLandingPad() || MBB->pred_empty())
363 return false;
364
365 // If there isn't exactly one predecessor, it can't be a fall through.
366 MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI;
367 ++PI2;
Jia Liuf54f60f2012-02-28 07:46:26 +0000368
Akira Hatanakae625ba42011-04-01 18:57:38 +0000369 if (PI2 != MBB->pred_end())
Jia Liuf54f60f2012-02-28 07:46:26 +0000370 return false;
Akira Hatanakae625ba42011-04-01 18:57:38 +0000371
372 // The predecessor has to be immediately before this block.
373 if (!Pred->isLayoutSuccessor(MBB))
374 return false;
Jia Liuf54f60f2012-02-28 07:46:26 +0000375
Akira Hatanakae625ba42011-04-01 18:57:38 +0000376 // If the block is completely empty, then it definitely does fall through.
377 if (Pred->empty())
378 return true;
Jia Liuf54f60f2012-02-28 07:46:26 +0000379
Akira Hatanakae625ba42011-04-01 18:57:38 +0000380 // Otherwise, check the last instruction.
381 // Check if the last terminator is an unconditional branch.
382 MachineBasicBlock::const_iterator I = Pred->end();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000383 while (I != Pred->begin() && !(--I)->isTerminator()) ;
Akira Hatanakae625ba42011-04-01 18:57:38 +0000384
Evan Cheng7f8e5632011-12-07 07:15:52 +0000385 return !I->isBarrier();
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000386}
387
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000388// Print out an operand for an inline asm expression.
Eric Christophered51b9e2012-05-10 21:48:22 +0000389bool MipsAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Chris Lattner3bb09762010-04-04 05:29:35 +0000390 unsigned AsmVariant,const char *ExtraCode,
391 raw_ostream &O) {
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000392 // Does this asm operand have a single letter operand modifier?
Eric Christophered51b9e2012-05-10 21:48:22 +0000393 if (ExtraCode && ExtraCode[0]) {
394 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000395
Eric Christophered51b9e2012-05-10 21:48:22 +0000396 const MachineOperand &MO = MI->getOperand(OpNum);
397 switch (ExtraCode[0]) {
Eric Christopherbc5d2492012-05-19 00:51:56 +0000398 default:
Jack Carterb2fd5f62012-06-21 17:14:46 +0000399 // See if this is a generic print operand
400 return AsmPrinter::PrintAsmOperand(MI,OpNum,AsmVariant,ExtraCode,O);
Eric Christopherbc5d2492012-05-19 00:51:56 +0000401 case 'X': // hex const int
402 if ((MO.getType()) != MachineOperand::MO_Immediate)
403 return true;
404 O << "0x" << StringRef(utohexstr(MO.getImm())).lower();
405 return false;
406 case 'x': // hex const int (low 16 bits)
407 if ((MO.getType()) != MachineOperand::MO_Immediate)
408 return true;
409 O << "0x" << StringRef(utohexstr(MO.getImm() & 0xffff)).lower();
410 return false;
411 case 'd': // decimal const int
412 if ((MO.getType()) != MachineOperand::MO_Immediate)
413 return true;
414 O << MO.getImm();
415 return false;
Eric Christopherf481ab32012-05-30 19:05:19 +0000416 case 'm': // decimal const int minus 1
417 if ((MO.getType()) != MachineOperand::MO_Immediate)
418 return true;
419 O << MO.getImm() - 1;
420 return false;
Jack Carter27747b52012-06-28 20:46:26 +0000421 case 'z': {
422 // $0 if zero, regular printing otherwise
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000423 if (MO.getType() != MachineOperand::MO_Immediate)
424 return true;
425 int64_t Val = MO.getImm();
426 if (Val)
427 O << Val;
428 else
429 O << "$0";
430 return false;
431 }
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000432 case 'D': // Second part of a double word register operand
433 case 'L': // Low order register of a double word register operand
Jack Cartera62ba822012-07-18 06:41:36 +0000434 case 'M': // High order register of a double word register operand
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000435 {
Jack Carterb2af5122012-07-05 23:58:21 +0000436 if (OpNum == 0)
437 return true;
438 const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
439 if (!FlagsOP.isImm())
440 return true;
441 unsigned Flags = FlagsOP.getImm();
442 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Jack Carter2ab73b12012-07-06 02:44:22 +0000443 // Number of registers represented by this operand. We are looking
444 // for 2 for 32 bit mode and 1 for 64 bit mode.
Jack Carterb2af5122012-07-05 23:58:21 +0000445 if (NumVals != 2) {
Jack Carter2ab73b12012-07-06 02:44:22 +0000446 if (Subtarget->isGP64bit() && NumVals == 1 && MO.isReg()) {
Jack Carterb2af5122012-07-05 23:58:21 +0000447 unsigned Reg = MO.getReg();
448 O << '$' << MipsInstPrinter::getRegisterName(Reg);
449 return false;
450 }
451 return true;
452 }
Jack Carter42ebf982012-07-11 21:41:49 +0000453
454 unsigned RegOp = OpNum;
455 if (!Subtarget->isGP64bit()){
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000456 // Endianess reverses which register holds the high or low value
Jack Cartera62ba822012-07-18 06:41:36 +0000457 // between M and L.
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000458 switch(ExtraCode[0]) {
Jack Cartera62ba822012-07-18 06:41:36 +0000459 case 'M':
460 RegOp = (Subtarget->isLittle()) ? OpNum + 1 : OpNum;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000461 break;
462 case 'L':
Jack Cartera62ba822012-07-18 06:41:36 +0000463 RegOp = (Subtarget->isLittle()) ? OpNum : OpNum + 1;
464 break;
465 case 'D': // Always the second part
466 RegOp = OpNum + 1;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000467 }
468 if (RegOp >= MI->getNumOperands())
469 return true;
470 const MachineOperand &MO = MI->getOperand(RegOp);
471 if (!MO.isReg())
472 return true;
473 unsigned Reg = MO.getReg();
474 O << '$' << MipsInstPrinter::getRegisterName(Reg);
475 return false;
Jack Carterb2af5122012-07-05 23:58:21 +0000476 }
Eric Christophered51b9e2012-05-10 21:48:22 +0000477 }
Daniel Sanders8b59af12013-11-12 12:56:01 +0000478 case 'w':
479 // Print MSA registers for the 'f' constraint
480 // In LLVM, the 'w' modifier doesn't need to do anything.
481 // We can just call printOperand as normal.
482 break;
Jack Carter2ab73b12012-07-06 02:44:22 +0000483 }
484 }
Eric Christophered51b9e2012-05-10 21:48:22 +0000485
486 printOperand(MI, OpNum, O);
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000487 return false;
488}
489
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000490bool MipsAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
491 unsigned OpNum, unsigned AsmVariant,
492 const char *ExtraCode,
493 raw_ostream &O) {
Jack Carterb04e3572013-04-09 23:19:50 +0000494 int Offset = 0;
495 // Currently we are expecting either no ExtraCode or 'D'
496 if (ExtraCode) {
497 if (ExtraCode[0] == 'D')
498 Offset = 4;
499 else
500 return true; // Unknown modifier.
501 }
Jia Liuf54f60f2012-02-28 07:46:26 +0000502
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000503 const MachineOperand &MO = MI->getOperand(OpNum);
504 assert(MO.isReg() && "unexpected inline asm memory operand");
Jack Carterb04e3572013-04-09 23:19:50 +0000505 O << Offset << "($" << MipsInstPrinter::getRegisterName(MO.getReg()) << ")";
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000506
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000507 return false;
508}
509
Chris Lattner76c564b2010-04-04 04:47:45 +0000510void MipsAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
511 raw_ostream &O) {
Rafael Espindola58873562014-01-03 19:21:54 +0000512 const DataLayout *DL = TM.getDataLayout();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000513 const MachineOperand &MO = MI->getOperand(opNum);
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +0000514 bool closeP = false;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000515
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000516 if (MO.getTargetFlags())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000517 closeP = true;
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000518
519 switch(MO.getTargetFlags()) {
520 case MipsII::MO_GPREL: O << "%gp_rel("; break;
521 case MipsII::MO_GOT_CALL: O << "%call16("; break;
Akira Hatanaka56d9ef52011-04-01 21:41:06 +0000522 case MipsII::MO_GOT: O << "%got("; break;
523 case MipsII::MO_ABS_HI: O << "%hi("; break;
524 case MipsII::MO_ABS_LO: O << "%lo("; break;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +0000525 case MipsII::MO_TLSGD: O << "%tlsgd("; break;
526 case MipsII::MO_GOTTPREL: O << "%gottprel("; break;
527 case MipsII::MO_TPREL_HI: O << "%tprel_hi("; break;
528 case MipsII::MO_TPREL_LO: O << "%tprel_lo("; break;
Akira Hatanaka25ce3642011-09-22 03:09:07 +0000529 case MipsII::MO_GPOFF_HI: O << "%hi(%neg(%gp_rel("; break;
530 case MipsII::MO_GPOFF_LO: O << "%lo(%neg(%gp_rel("; break;
531 case MipsII::MO_GOT_DISP: O << "%got_disp("; break;
532 case MipsII::MO_GOT_PAGE: O << "%got_page("; break;
533 case MipsII::MO_GOT_OFST: O << "%got_ofst("; break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000534 }
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000535
Chris Lattnereb2cc682009-09-13 20:31:40 +0000536 switch (MO.getType()) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000537 case MachineOperand::MO_Register:
Akira Hatanaka9c6028f2011-07-07 23:56:50 +0000538 O << '$'
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000539 << StringRef(MipsInstPrinter::getRegisterName(MO.getReg())).lower();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000540 break;
541
542 case MachineOperand::MO_Immediate:
Akira Hatanaka2db176c2011-05-24 21:22:21 +0000543 O << MO.getImm();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000544 break;
545
546 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner29bdac42010-03-13 21:04:28 +0000547 O << *MO.getMBB()->getSymbol();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000548 return;
549
550 case MachineOperand::MO_GlobalAddress:
Rafael Espindola79858aa2013-10-29 17:07:16 +0000551 O << *getSymbol(MO.getGlobal());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000552 break;
553
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000554 case MachineOperand::MO_BlockAddress: {
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000555 MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000556 O << BA->getName();
557 break;
558 }
559
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000560 case MachineOperand::MO_ConstantPoolIndex:
Rafael Espindola58873562014-01-03 19:21:54 +0000561 O << DL->getPrivateGlobalPrefix() << "CPI"
Chris Lattnera5bb3702007-12-30 23:10:15 +0000562 << getFunctionNumber() << "_" << MO.getIndex();
Bruno Cardoso Lopes4713b282009-11-19 06:06:13 +0000563 if (MO.getOffset())
564 O << "+" << MO.getOffset();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000565 break;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000566
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000567 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000568 llvm_unreachable("<unknown operand type>");
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000569 }
570
571 if (closeP) O << ")";
572}
573
Chris Lattner76c564b2010-04-04 04:47:45 +0000574void MipsAsmPrinter::printUnsignedImm(const MachineInstr *MI, int opNum,
575 raw_ostream &O) {
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000576 const MachineOperand &MO = MI->getOperand(opNum);
Devang Patel12f68552010-04-27 22:24:37 +0000577 if (MO.isImm())
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000578 O << (unsigned short int)MO.getImm();
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000579 else
Chris Lattner76c564b2010-04-04 04:47:45 +0000580 printOperand(MI, opNum, O);
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000581}
582
Daniel Sanders3f6eb542013-11-12 10:45:18 +0000583void MipsAsmPrinter::printUnsignedImm8(const MachineInstr *MI, int opNum,
584 raw_ostream &O) {
585 const MachineOperand &MO = MI->getOperand(opNum);
586 if (MO.isImm())
587 O << (unsigned short int)(unsigned char)MO.getImm();
588 else
589 printOperand(MI, opNum, O);
590}
591
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000592void MipsAsmPrinter::
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000593printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O) {
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000594 // Load/Store memory operands -- imm($reg)
595 // If PIC target the target is loaded as the
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +0000596 // pattern lw $25,%call16($28)
Chris Lattner76c564b2010-04-04 04:47:45 +0000597 printOperand(MI, opNum+1, O);
Akira Hatanaka2e766ed2011-07-07 18:57:00 +0000598 O << "(";
599 printOperand(MI, opNum, O);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000600 O << ")";
601}
602
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000603void MipsAsmPrinter::
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000604printMemOperandEA(const MachineInstr *MI, int opNum, raw_ostream &O) {
605 // when using stack locations for not load/store instructions
606 // print the same way as all normal 3 operand instructions.
607 printOperand(MI, opNum, O);
608 O << ", ";
609 printOperand(MI, opNum+1, O);
610 return;
611}
612
613void MipsAsmPrinter::
Chris Lattner76c564b2010-04-04 04:47:45 +0000614printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
615 const char *Modifier) {
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000616 const MachineOperand &MO = MI->getOperand(opNum);
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000617 O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000618}
619
Bob Wilsonb633d7a2009-09-30 22:06:26 +0000620void MipsAsmPrinter::EmitStartOfAsmFile(Module &M) {
Jack Carterf9f753c2013-06-18 19:47:15 +0000621 // TODO: Need to add -mabicalls and -mno-abicalls flags.
622 // Currently we assume that -mabicalls is the default.
Daniel Sanders16fa1db2014-04-16 13:58:57 +0000623 bool IsABICalls = true;
624 if (IsABICalls) {
625 getTargetStreamer().emitDirectiveAbiCalls();
626 Reloc::Model RM = Subtarget->getRelocationModel();
627 // FIXME: This condition should be a lot more complicated that it is here.
628 // Ideally it should test for properties of the ABI and not the ABI
629 // itself.
630 // For the moment, I'm only correcting enough to make MIPS-IV work.
631 if (RM == Reloc::Static && !Subtarget->isABI_N64())
632 getTargetStreamer().emitDirectiveOptionPic0();
633 }
Jack Carterf9f753c2013-06-18 19:47:15 +0000634
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000635 // Tell the assembler which ABI we are using
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000636 std::string SectionName = std::string(".mdebug.") + getCurrentABIString();
637 OutStreamer.SwitchSection(OutContext.getELFSection(
638 SectionName, ELF::SHT_PROGBITS, 0, SectionKind::getDataRel()));
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000639
640 // TODO: handle O64 ABI
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000641
642 if (Subtarget->isABI_EABI()) {
643 if (Subtarget->isGP32bit())
644 OutStreamer.SwitchSection(
645 OutContext.getELFSection(".gcc_compiled_long32", ELF::SHT_PROGBITS, 0,
646 SectionKind::getDataRel()));
647 else
648 OutStreamer.SwitchSection(
649 OutContext.getELFSection(".gcc_compiled_long64", ELF::SHT_PROGBITS, 0,
650 SectionKind::getDataRel()));
Benjamin Kramer0151d7b2010-04-05 10:17:15 +0000651 }
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000652}
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000653
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000654void MipsAsmPrinter::EmitJal(MCSymbol *Symbol) {
655 MCInst I;
656 I.setOpcode(Mips::JAL);
657 I.addOperand(
658 MCOperand::CreateExpr(MCSymbolRefExpr::Create(Symbol, OutContext)));
659 OutStreamer.EmitInstruction(I, getSubtargetInfo());
660}
661
662void MipsAsmPrinter::EmitInstrReg(unsigned Opcode, unsigned Reg) {
663 MCInst I;
664 I.setOpcode(Opcode);
665 I.addOperand(MCOperand::CreateReg(Reg));
666 OutStreamer.EmitInstruction(I, getSubtargetInfo());
667}
668
669void MipsAsmPrinter::EmitInstrRegReg(unsigned Opcode, unsigned Reg1,
670 unsigned Reg2) {
671 MCInst I;
672 //
673 // Because of the current td files for Mips32, the operands for MTC1
674 // appear backwards from their normal assembly order. It's not a trivial
675 // change to fix this in the td file so we adjust for it here.
676 //
677 if (Opcode == Mips::MTC1) {
678 unsigned Temp = Reg1;
679 Reg1 = Reg2;
680 Reg2 = Temp;
681 }
682 I.setOpcode(Opcode);
683 I.addOperand(MCOperand::CreateReg(Reg1));
684 I.addOperand(MCOperand::CreateReg(Reg2));
685 OutStreamer.EmitInstruction(I, getSubtargetInfo());
686}
687
688void MipsAsmPrinter::EmitInstrRegRegReg(unsigned Opcode, unsigned Reg1,
689 unsigned Reg2, unsigned Reg3) {
690 MCInst I;
691 I.setOpcode(Opcode);
692 I.addOperand(MCOperand::CreateReg(Reg1));
693 I.addOperand(MCOperand::CreateReg(Reg2));
694 I.addOperand(MCOperand::CreateReg(Reg3));
695 OutStreamer.EmitInstruction(I, getSubtargetInfo());
696}
697
698void MipsAsmPrinter::EmitMovFPIntPair(unsigned MovOpc, unsigned Reg1,
699 unsigned Reg2, unsigned FPReg1,
700 unsigned FPReg2, bool LE) {
701 if (!LE) {
702 unsigned temp = Reg1;
703 Reg1 = Reg2;
704 Reg2 = temp;
705 }
706 EmitInstrRegReg(MovOpc, Reg1, FPReg1);
707 EmitInstrRegReg(MovOpc, Reg2, FPReg2);
708}
709
710void MipsAsmPrinter::EmitSwapFPIntParams(Mips16HardFloatInfo::FPParamVariant PV,
711 bool LE, bool ToFP) {
712 using namespace Mips16HardFloatInfo;
713 unsigned MovOpc = ToFP ? Mips::MTC1 : Mips::MFC1;
714 switch (PV) {
715 case FSig:
716 EmitInstrRegReg(MovOpc, Mips::A0, Mips::F12);
717 break;
718 case FFSig:
719 EmitMovFPIntPair(MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F14, LE);
720 break;
721 case FDSig:
722 EmitInstrRegReg(MovOpc, Mips::A0, Mips::F12);
723 EmitMovFPIntPair(MovOpc, Mips::A2, Mips::A3, Mips::F14, Mips::F15, LE);
724 break;
725 case DSig:
726 EmitMovFPIntPair(MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
727 break;
728 case DDSig:
729 EmitMovFPIntPair(MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
730 EmitMovFPIntPair(MovOpc, Mips::A2, Mips::A3, Mips::F14, Mips::F15, LE);
731 break;
732 case DFSig:
733 EmitMovFPIntPair(MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
734 EmitInstrRegReg(MovOpc, Mips::A2, Mips::F14);
735 break;
736 case NoSig:
737 return;
738 }
739}
740
741void
742MipsAsmPrinter::EmitSwapFPIntRetval(Mips16HardFloatInfo::FPReturnVariant RV,
743 bool LE) {
744 using namespace Mips16HardFloatInfo;
745 unsigned MovOpc = Mips::MFC1;
746 switch (RV) {
747 case FRet:
748 EmitInstrRegReg(MovOpc, Mips::V0, Mips::F0);
749 break;
750 case DRet:
751 EmitMovFPIntPair(MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
752 break;
753 case CFRet:
754 EmitMovFPIntPair(MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
755 break;
756 case CDRet:
757 EmitMovFPIntPair(MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
758 EmitMovFPIntPair(MovOpc, Mips::A0, Mips::A1, Mips::F2, Mips::F3, LE);
759 break;
760 case NoFPRet:
761 break;
762 }
763}
764
765void MipsAsmPrinter::EmitFPCallStub(
766 const char *Symbol, const Mips16HardFloatInfo::FuncSignature *Signature) {
767 MCSymbol *MSymbol = OutContext.GetOrCreateSymbol(StringRef(Symbol));
768 using namespace Mips16HardFloatInfo;
769 bool LE = Subtarget->isLittle();
770 //
771 // .global xxxx
772 //
773 OutStreamer.EmitSymbolAttribute(MSymbol, MCSA_Global);
774 const char *RetType;
775 //
776 // make the comment field identifying the return and parameter
777 // types of the floating point stub
778 // # Stub function to call rettype xxxx (params)
779 //
780 switch (Signature->RetSig) {
781 case FRet:
782 RetType = "float";
783 break;
784 case DRet:
785 RetType = "double";
786 break;
787 case CFRet:
788 RetType = "complex";
789 break;
790 case CDRet:
791 RetType = "double complex";
792 break;
793 case NoFPRet:
794 RetType = "";
795 break;
796 }
797 const char *Parms;
798 switch (Signature->ParamSig) {
799 case FSig:
800 Parms = "float";
801 break;
802 case FFSig:
803 Parms = "float, float";
804 break;
805 case FDSig:
806 Parms = "float, double";
807 break;
808 case DSig:
809 Parms = "double";
810 break;
811 case DDSig:
812 Parms = "double, double";
813 break;
814 case DFSig:
815 Parms = "double, float";
816 break;
817 case NoSig:
818 Parms = "";
819 break;
820 }
821 OutStreamer.AddComment("\t# Stub function to call " + Twine(RetType) + " " +
822 Twine(Symbol) + " (" + Twine(Parms) + ")");
823 //
824 // probably not necessary but we save and restore the current section state
825 //
826 OutStreamer.PushSection();
827 //
828 // .section mips16.call.fpxxxx,"ax",@progbits
829 //
830 const MCSectionELF *M = OutContext.getELFSection(
831 ".mips16.call.fp." + std::string(Symbol), ELF::SHT_PROGBITS,
832 ELF::SHF_ALLOC | ELF::SHF_EXECINSTR, SectionKind::getText());
833 OutStreamer.SwitchSection(M, 0);
834 //
835 // .align 2
836 //
837 OutStreamer.EmitValueToAlignment(4);
838 MipsTargetStreamer &TS = getTargetStreamer();
839 //
840 // .set nomips16
841 // .set nomicromips
842 //
843 TS.emitDirectiveSetNoMips16();
844 TS.emitDirectiveSetNoMicroMips();
845 //
846 // .ent __call_stub_fp_xxxx
847 // .type __call_stub_fp_xxxx,@function
848 // __call_stub_fp_xxxx:
849 //
850 std::string x = "__call_stub_fp_" + std::string(Symbol);
851 MCSymbol *Stub = OutContext.GetOrCreateSymbol(StringRef(x));
852 TS.emitDirectiveEnt(*Stub);
853 MCSymbol *MType =
854 OutContext.GetOrCreateSymbol("__call_stub_fp_" + Twine(Symbol));
855 OutStreamer.EmitSymbolAttribute(MType, MCSA_ELF_TypeFunction);
856 OutStreamer.EmitLabel(Stub);
857 //
858 // we just handle non pic for now. these function will not be
859 // called otherwise. when the full stub generation is moved here
860 // we need to deal with pic.
861 //
862 if (Subtarget->getRelocationModel() == Reloc::PIC_)
863 llvm_unreachable("should not be here if we are compiling pic");
864 TS.emitDirectiveSetReorder();
865 //
866 // We need to add a MipsMCExpr class to MCTargetDesc to fully implement
867 // stubs without raw text but this current patch is for compiler generated
868 // functions and they all return some value.
869 // The calling sequence for non pic is different in that case and we need
870 // to implement %lo and %hi in order to handle the case of no return value
871 // See the corresponding method in Mips16HardFloat for details.
872 //
873 // mov the return address to S2.
874 // we have no stack space to store it and we are about to make another call.
875 // We need to make sure that the enclosing function knows to save S2
876 // This should have already been handled.
877 //
878 // Mov $18, $31
879
880 EmitInstrRegRegReg(Mips::ADDu, Mips::S2, Mips::RA, Mips::ZERO);
881
882 EmitSwapFPIntParams(Signature->ParamSig, LE, true);
883
884 // Jal xxxx
885 //
886 EmitJal(MSymbol);
887
888 // fix return values
889 EmitSwapFPIntRetval(Signature->RetSig, LE);
890 //
891 // do the return
892 // if (Signature->RetSig == NoFPRet)
893 // llvm_unreachable("should not be any stubs here with no return value");
894 // else
895 EmitInstrReg(Mips::JR, Mips::S2);
896
897 MCSymbol *Tmp = OutContext.CreateTempSymbol();
898 OutStreamer.EmitLabel(Tmp);
899 const MCSymbolRefExpr *E = MCSymbolRefExpr::Create(Stub, OutContext);
900 const MCSymbolRefExpr *T = MCSymbolRefExpr::Create(Tmp, OutContext);
901 const MCExpr *T_min_E = MCBinaryExpr::CreateSub(T, E, OutContext);
902 OutStreamer.EmitELFSize(Stub, T_min_E);
903 TS.emitDirectiveEnd(x);
904 OutStreamer.PopSection();
905}
906
907void MipsAsmPrinter::EmitEndOfAsmFile(Module &M) {
908 // Emit needed stubs
909 //
910 for (std::map<
911 const char *,
912 const llvm::Mips16HardFloatInfo::FuncSignature *>::const_iterator
913 it = StubsNeeded.begin();
914 it != StubsNeeded.end(); ++it) {
915 const char *Symbol = it->first;
916 const llvm::Mips16HardFloatInfo::FuncSignature *Signature = it->second;
917 EmitFPCallStub(Symbol, Signature);
918 }
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000919 // return to the text section
920 OutStreamer.SwitchSection(OutContext.getObjectFileInfo()->getTextSection());
Jack Carterc1b17ed2013-01-18 21:20:38 +0000921}
922
Akira Hatanakaf2bcad92011-07-01 01:04:43 +0000923void MipsAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
924 raw_ostream &OS) {
925 // TODO: implement
926}
927
Sasa Stankovic8c5736b2014-02-28 10:00:38 +0000928// Align all targets of indirect branches on bundle size. Used only if target
929// is NaCl.
930void MipsAsmPrinter::NaClAlignIndirectJumpTargets(MachineFunction &MF) {
931 // Align all blocks that are jumped to through jump table.
932 if (MachineJumpTableInfo *JtInfo = MF.getJumpTableInfo()) {
933 const std::vector<MachineJumpTableEntry> &JT = JtInfo->getJumpTables();
934 for (unsigned I = 0; I < JT.size(); ++I) {
935 const std::vector<MachineBasicBlock*> &MBBs = JT[I].MBBs;
936
937 for (unsigned J = 0; J < MBBs.size(); ++J)
938 MBBs[J]->setAlignment(MIPS_NACL_BUNDLE_ALIGN);
939 }
940 }
941
942 // If basic block address is taken, block can be target of indirect branch.
943 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
944 MBB != E; ++MBB) {
945 if (MBB->hasAddressTaken())
946 MBB->setAlignment(MIPS_NACL_BUNDLE_ALIGN);
947 }
948}
949
Bob Wilson5a495fe2009-06-23 23:59:40 +0000950// Force static initialization.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000951extern "C" void LLVMInitializeMipsAsmPrinter() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +0000952 RegisterAsmPrinter<MipsAsmPrinter> X(TheMipsTarget);
953 RegisterAsmPrinter<MipsAsmPrinter> Y(TheMipselTarget);
Akira Hatanaka3d673cc2011-09-21 03:00:58 +0000954 RegisterAsmPrinter<MipsAsmPrinter> A(TheMips64Target);
955 RegisterAsmPrinter<MipsAsmPrinter> B(TheMips64elTarget);
Daniel Dunbare8338102009-07-15 20:24:03 +0000956}