blob: debbb95b3926653ee2a7c89701f9d268b5614598 [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"
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"
Akira Hatanaka2fcc1cf2011-08-12 21:30:06 +000030#include "llvm/CodeGen/MachineMemOperand.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000031#include "llvm/IR/BasicBlock.h"
32#include "llvm/IR/DataLayout.h"
33#include "llvm/IR/InlineAsm.h"
34#include "llvm/IR/Instructions.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000035#include "llvm/IR/Mangler.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000036#include "llvm/MC/MCAsmInfo.h"
Rafael Espindola2ab7ea72014-01-27 01:33:33 +000037#include "llvm/MC/MCContext.h"
Rafael Espindolaac4ad252013-10-05 16:42:21 +000038#include "llvm/MC/MCELFStreamer.h"
Akira Hatanaka9c6028f2011-07-07 23:56:50 +000039#include "llvm/MC/MCInst.h"
Rafael Espindola2ab7ea72014-01-27 01:33:33 +000040#include "llvm/MC/MCSectionELF.h"
Chris Lattner4cd44982009-09-13 17:14:04 +000041#include "llvm/MC/MCSymbol.h"
Jack Carterab3cb422013-02-19 22:04:37 +000042#include "llvm/Support/ELF.h"
Jack Carterb2af5122012-07-05 23:58:21 +000043#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000044#include "llvm/Support/raw_ostream.h"
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +000045#include "llvm/Target/TargetLoweringObjectFile.h"
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +000046#include "llvm/Target/TargetOptions.h"
Akira Hatanakaf2bcad92011-07-01 01:04:43 +000047
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000048using namespace llvm;
49
Rafael Espindolaa17151a2013-10-08 13:08:17 +000050MipsTargetStreamer &MipsAsmPrinter::getTargetStreamer() {
Rafael Espindola4a1a3602014-01-14 01:21:46 +000051 return static_cast<MipsTargetStreamer &>(*OutStreamer.getTargetStreamer());
Rafael Espindolaa17151a2013-10-08 13:08:17 +000052}
53
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000054bool MipsAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Reed Kotler1595f362013-04-09 19:46:01 +000055 // Initialize TargetLoweringObjectFile.
56 if (Subtarget->allowMixed16_32())
57 const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
58 .Initialize(OutContext, TM);
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000059 MipsFI = MF.getInfo<MipsFunctionInfo>();
Reed Kotler91ae9822013-10-27 21:57:36 +000060 MCP = MF.getConstantPool();
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000061 AsmPrinter::runOnMachineFunction(MF);
62 return true;
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +000063}
64
Akira Hatanaka42a35242012-09-27 01:59:07 +000065bool MipsAsmPrinter::lowerOperand(const MachineOperand &MO, MCOperand &MCOp) {
66 MCOp = MCInstLowering.LowerOperand(MO);
67 return MCOp.isValid();
68}
69
70#include "MipsGenMCPseudoLowering.inc"
71
Akira Hatanakaddd12652011-07-07 20:10:52 +000072void MipsAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Akira Hatanakaddd12652011-07-07 20:10:52 +000073 if (MI->isDebugValue()) {
Bruno Cardoso Lopescd1d4472011-12-30 21:09:41 +000074 SmallString<128> Str;
75 raw_svector_ostream OS(Str);
76
Akira Hatanakaddd12652011-07-07 20:10:52 +000077 PrintDebugValueComment(MI, OS);
78 return;
79 }
80
Reed Kotler91ae9822013-10-27 21:57:36 +000081 // If we just ended a constant pool, mark it as such.
82 if (InConstantPool && MI->getOpcode() != Mips::CONSTPOOL_ENTRY) {
83 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
84 InConstantPool = false;
85 }
86 if (MI->getOpcode() == Mips::CONSTPOOL_ENTRY) {
87 // CONSTPOOL_ENTRY - This instruction represents a floating
88 //constant pool in the function. The first operand is the ID#
89 // for this instruction, the second is the index into the
90 // MachineConstantPool that this is, the third is the size in
91 // bytes of this constant pool entry.
92 // The required alignment is specified on the basic block holding this MI.
93 //
94 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
95 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
96
97 // If this is the first entry of the pool, mark it.
98 if (!InConstantPool) {
99 OutStreamer.EmitDataRegion(MCDR_DataRegion);
100 InConstantPool = true;
101 }
102
103 OutStreamer.EmitLabel(GetCPISymbol(LabelId));
104
105 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
106 if (MCPE.isMachineConstantPoolEntry())
107 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
108 else
109 EmitGlobalConstant(MCPE.Val.ConstVal);
110 return;
111 }
112
Rafael Espindola14d02fe2014-01-25 15:06:56 +0000113
Akira Hatanaka5ac78682012-06-13 23:25:52 +0000114 MachineBasicBlock::const_instr_iterator I = MI;
115 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
116
117 do {
Akira Hatanaka556135d2013-02-06 21:50:15 +0000118 // Do any auto-generated pseudo lowerings.
119 if (emitPseudoExpansionLowering(OutStreamer, &*I))
120 continue;
Jack Carterc20a21b2012-08-28 19:07:39 +0000121
Reed Kotler76c9bcd2013-02-15 21:05:58 +0000122 // The inMips16Mode() test is not permanent.
123 // Some instructions are marked as pseudo right now which
124 // would make the test fail for the wrong reason but
125 // that will be fixed soon. We need this here because we are
126 // removing another test for this situation downstream in the
127 // callchain.
128 //
129 if (I->isPseudo() && !Subtarget->inMips16Mode())
130 llvm_unreachable("Pseudo opcode found in EmitInstruction()");
131
Akira Hatanaka556135d2013-02-06 21:50:15 +0000132 MCInst TmpInst0;
133 MCInstLowering.Lower(I, TmpInst0);
Akira Hatanaka5ac78682012-06-13 23:25:52 +0000134 OutStreamer.EmitInstruction(TmpInst0);
Akira Hatanaka556135d2013-02-06 21:50:15 +0000135 } while ((++I != E) && I->isInsideBundle()); // Delay slot check
Akira Hatanakaddd12652011-07-07 20:10:52 +0000136}
137
Akira Hatanakae2489122011-04-15 21:51:11 +0000138//===----------------------------------------------------------------------===//
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000139//
140// Mips Asm Directives
141//
142// -- Frame directive "frame Stackpointer, Stacksize, RARegister"
143// Describe the stack frame.
144//
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000145// -- Mask directives "(f)mask bitmask, offset"
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000146// Tells the assembler which registers are saved and where.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000147// bitmask - contain a little endian bitset indicating which registers are
148// saved on function prologue (e.g. with a 0x80000000 mask, the
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000149// assembler knows the register 31 (RA) is saved at prologue.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000150// offset - the position before stack pointer subtraction indicating where
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000151// the first saved register on prologue is located. (e.g. with a
152//
153// Consider the following function prologue:
154//
Bill Wendling97925ec2008-02-27 06:33:05 +0000155// .frame $fp,48,$ra
156// .mask 0xc0000000,-8
157// addiu $sp, $sp, -48
158// sw $ra, 40($sp)
159// sw $fp, 36($sp)
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000160//
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000161// With a 0xc0000000 mask, the assembler knows the register 31 (RA) and
162// 30 (FP) are saved at prologue. As the save order on prologue is from
163// left to right, RA is saved first. A -8 offset means that after the
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000164// stack pointer subtration, the first register in the mask (RA) will be
165// saved at address 48-8=40.
166//
Akira Hatanakae2489122011-04-15 21:51:11 +0000167//===----------------------------------------------------------------------===//
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000168
Akira Hatanakae2489122011-04-15 21:51:11 +0000169//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000170// Mask directives
Akira Hatanakae2489122011-04-15 21:51:11 +0000171//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000172
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000173// Create a bitmask with all callee saved registers for CPU or Floating Point
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000174// registers. For CPU registers consider RA, GP and FP for saving if necessary.
Chris Lattner76c564b2010-04-04 04:47:45 +0000175void MipsAsmPrinter::printSavedRegsBitmask(raw_ostream &O) {
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000176 // CPU and FPU Saved Registers Bitmasks
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000177 unsigned CPUBitmask = 0, FPUBitmask = 0;
178 int CPUTopSavedRegOff, FPUTopSavedRegOff;
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000179
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000180 // Set the CPU and FPU Bitmasks
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000181 const MachineFrameInfo *MFI = MF->getFrameInfo();
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000182 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000183 // size of stack area to which FP callee-saved regs are saved.
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000184 unsigned CPURegSize = Mips::GPR32RegClass.getSize();
Craig Topperc7242e02012-04-20 07:30:17 +0000185 unsigned FGR32RegSize = Mips::FGR32RegClass.getSize();
186 unsigned AFGR64RegSize = Mips::AFGR64RegClass.getSize();
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000187 bool HasAFGR64Reg = false;
188 unsigned CSFPRegsSize = 0;
189 unsigned i, e = CSI.size();
190
191 // Set FPU Bitmask.
192 for (i = 0; i != e; ++i) {
Rafael Espindolaf2dffce2010-06-02 20:02:30 +0000193 unsigned Reg = CSI[i].getReg();
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000194 if (Mips::GPR32RegClass.contains(Reg))
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000195 break;
196
Akira Hatanaka5d6faed2012-12-10 20:04:40 +0000197 unsigned RegNum = TM.getRegisterInfo()->getEncodingValue(Reg);
Craig Topperc7242e02012-04-20 07:30:17 +0000198 if (Mips::AFGR64RegClass.contains(Reg)) {
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000199 FPUBitmask |= (3 << RegNum);
200 CSFPRegsSize += AFGR64RegSize;
201 HasAFGR64Reg = true;
202 continue;
203 }
204
205 FPUBitmask |= (1 << RegNum);
206 CSFPRegsSize += FGR32RegSize;
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000207 }
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000208
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000209 // Set CPU Bitmask.
210 for (; i != e; ++i) {
211 unsigned Reg = CSI[i].getReg();
Akira Hatanaka5d6faed2012-12-10 20:04:40 +0000212 unsigned RegNum = TM.getRegisterInfo()->getEncodingValue(Reg);
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000213 CPUBitmask |= (1 << RegNum);
214 }
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000215
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000216 // FP Regs are saved right below where the virtual frame pointer points to.
217 FPUTopSavedRegOff = FPUBitmask ?
218 (HasAFGR64Reg ? -AFGR64RegSize : -FGR32RegSize) : 0;
219
220 // CPU Regs are saved below FP Regs.
221 CPUTopSavedRegOff = CPUBitmask ? -CSFPRegsSize - CPURegSize : 0;
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000222
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000223 // Print CPUBitmask
Chris Lattner76c564b2010-04-04 04:47:45 +0000224 O << "\t.mask \t"; printHex32(CPUBitmask, O);
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000225 O << ',' << CPUTopSavedRegOff << '\n';
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000226
227 // Print FPUBitmask
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000228 O << "\t.fmask\t"; printHex32(FPUBitmask, O);
229 O << "," << FPUTopSavedRegOff << '\n';
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000230}
231
232// Print a 32 bit hex number with all numbers.
Chris Lattner76c564b2010-04-04 04:47:45 +0000233void MipsAsmPrinter::printHex32(unsigned Value, raw_ostream &O) {
Owen Anderson93719642008-08-21 00:14:44 +0000234 O << "0x";
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000235 for (int i = 7; i >= 0; i--)
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000236 O.write_hex((Value & (0xF << (i*4))) >> (i*4));
Bruno Cardoso Lopesbcda5e22007-07-11 23:24:41 +0000237}
238
Akira Hatanakae2489122011-04-15 21:51:11 +0000239//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000240// Frame and Set directives
Akira Hatanakae2489122011-04-15 21:51:11 +0000241//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000242
243/// Frame Directive
Chris Lattner5e596182010-04-04 07:05:53 +0000244void MipsAsmPrinter::emitFrameDirective() {
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000245 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
246
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000247 unsigned stackReg = RI.getFrameRegister(*MF);
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000248 unsigned returnReg = RI.getRARegister();
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000249 unsigned stackSize = MF->getFrameInfo()->getStackSize();
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000250
Rafael Espindola054234f2014-01-27 03:53:56 +0000251 getTargetStreamer().emitFrame(stackReg, stackSize, returnReg);
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000252}
253
254/// Emit Set directives.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000255const char *MipsAsmPrinter::getCurrentABIString() const {
Chris Lattner5e596182010-04-04 07:05:53 +0000256 switch (Subtarget->getTargetABI()) {
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000257 case MipsSubtarget::O32: return "abi32";
Chris Lattner5e596182010-04-04 07:05:53 +0000258 case MipsSubtarget::N32: return "abiN32";
259 case MipsSubtarget::N64: return "abi64";
260 case MipsSubtarget::EABI: return "eabi32"; // TODO: handle eabi64
Dmitri Gribenkoca1e27b2012-09-10 21:26:47 +0000261 default: llvm_unreachable("Unknown Mips ABI");
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000262 }
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000263}
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000264
Chris Lattner5d9fb4b2010-01-27 23:23:58 +0000265void MipsAsmPrinter::EmitFunctionEntryLabel() {
Rafael Espindola6633d572014-01-14 18:57:12 +0000266 MipsTargetStreamer &TS = getTargetStreamer();
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000267 if (Subtarget->inMicroMipsMode())
Rafael Espindola6633d572014-01-14 18:57:12 +0000268 TS.emitDirectiveSetMicroMips();
269 // leave out until FSF available gas has micromips changes
270 // else
271 // TS.emitDirectiveSetNoMicroMips();
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000272
Rafael Espindola6633d572014-01-14 18:57:12 +0000273 if (Subtarget->inMips16Mode())
274 TS.emitDirectiveSetMips16();
275 else
276 TS.emitDirectiveSetNoMips16();
Jack Carterab3cb422013-02-19 22:04:37 +0000277
Rafael Espindola6633d572014-01-14 18:57:12 +0000278 TS.emitDirectiveEnt(*CurrentFnSym);
Chris Lattner5d9fb4b2010-01-27 23:23:58 +0000279 OutStreamer.EmitLabel(CurrentFnSym);
280}
281
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000282/// EmitFunctionBodyStart - Targets can override this to emit stuff before
283/// the first basic block in the function.
284void MipsAsmPrinter::EmitFunctionBodyStart() {
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000285 MipsTargetStreamer &TS = getTargetStreamer();
286
Rafael Espindola7d78b2a2013-10-29 16:24:21 +0000287 MCInstLowering.Initialize(&MF->getContext());
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +0000288
Reed Kotler0f2b10e2013-05-03 23:17:24 +0000289 bool IsNakedFunction =
290 MF->getFunction()->
291 getAttributes().hasAttribute(AttributeSet::FunctionIndex,
292 Attribute::Naked);
293 if (!IsNakedFunction)
294 emitFrameDirective();
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000295
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000296 if (OutStreamer.hasRawTextSupport()) {
297 SmallString<128> Str;
298 raw_svector_ostream OS(Str);
Reed Kotler0f2b10e2013-05-03 23:17:24 +0000299 if (!IsNakedFunction)
300 printSavedRegsBitmask(OS);
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000301 OutStreamer.EmitRawText(OS.str());
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000302 }
303 if (!Subtarget->inMips16Mode()) {
304 TS.emitDirectiveSetNoReorder();
305 TS.emitDirectiveSetNoMacro();
306 TS.emitDirectiveSetNoAt();
Akira Hatanaka8f3573032012-05-12 00:48:43 +0000307 }
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000308}
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000309
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000310/// EmitFunctionBodyEnd - Targets can override this to emit stuff after
311/// the last basic block in the function.
312void MipsAsmPrinter::EmitFunctionBodyEnd() {
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000313 MipsTargetStreamer &TS = getTargetStreamer();
314
Chris Lattnerfd97a332010-01-28 01:48:52 +0000315 // There are instruction for this macros, but they must
316 // always be at the function end, and we can't emit and
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000317 // break with BB logic.
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000318 if (!Subtarget->inMips16Mode()) {
319 TS.emitDirectiveSetAt();
320 TS.emitDirectiveSetMacro();
321 TS.emitDirectiveSetReorder();
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000322 }
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000323 TS.emitDirectiveEnd(CurrentFnSym->getName());
Reed Kotler91ae9822013-10-27 21:57:36 +0000324 // Make sure to terminate any constant pools that were at the end
325 // of the function.
326 if (!InConstantPool)
327 return;
328 InConstantPool = false;
329 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000330}
331
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000332/// isBlockOnlyReachableByFallthough - Return true if the basic block has
333/// exactly one predecessor and the control transfer mechanism between
334/// the predecessor and this block is a fall-through.
Akira Hatanakae2489122011-04-15 21:51:11 +0000335bool MipsAsmPrinter::isBlockOnlyReachableByFallthrough(const MachineBasicBlock*
336 MBB) const {
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000337 // The predecessor has to be immediately before this block.
338 const MachineBasicBlock *Pred = *MBB->pred_begin();
339
340 // If the predecessor is a switch statement, assume a jump table
341 // implementation, so it is not a fall through.
342 if (const BasicBlock *bb = Pred->getBasicBlock())
343 if (isa<SwitchInst>(bb->getTerminator()))
344 return false;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000345
Akira Hatanakae625ba42011-04-01 18:57:38 +0000346 // If this is a landing pad, it isn't a fall through. If it has no preds,
347 // then nothing falls through to it.
348 if (MBB->isLandingPad() || MBB->pred_empty())
349 return false;
350
351 // If there isn't exactly one predecessor, it can't be a fall through.
352 MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI;
353 ++PI2;
Jia Liuf54f60f2012-02-28 07:46:26 +0000354
Akira Hatanakae625ba42011-04-01 18:57:38 +0000355 if (PI2 != MBB->pred_end())
Jia Liuf54f60f2012-02-28 07:46:26 +0000356 return false;
Akira Hatanakae625ba42011-04-01 18:57:38 +0000357
358 // The predecessor has to be immediately before this block.
359 if (!Pred->isLayoutSuccessor(MBB))
360 return false;
Jia Liuf54f60f2012-02-28 07:46:26 +0000361
Akira Hatanakae625ba42011-04-01 18:57:38 +0000362 // If the block is completely empty, then it definitely does fall through.
363 if (Pred->empty())
364 return true;
Jia Liuf54f60f2012-02-28 07:46:26 +0000365
Akira Hatanakae625ba42011-04-01 18:57:38 +0000366 // Otherwise, check the last instruction.
367 // Check if the last terminator is an unconditional branch.
368 MachineBasicBlock::const_iterator I = Pred->end();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000369 while (I != Pred->begin() && !(--I)->isTerminator()) ;
Akira Hatanakae625ba42011-04-01 18:57:38 +0000370
Evan Cheng7f8e5632011-12-07 07:15:52 +0000371 return !I->isBarrier();
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000372}
373
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000374// Print out an operand for an inline asm expression.
Eric Christophered51b9e2012-05-10 21:48:22 +0000375bool MipsAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Chris Lattner3bb09762010-04-04 05:29:35 +0000376 unsigned AsmVariant,const char *ExtraCode,
377 raw_ostream &O) {
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000378 // Does this asm operand have a single letter operand modifier?
Eric Christophered51b9e2012-05-10 21:48:22 +0000379 if (ExtraCode && ExtraCode[0]) {
380 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000381
Eric Christophered51b9e2012-05-10 21:48:22 +0000382 const MachineOperand &MO = MI->getOperand(OpNum);
383 switch (ExtraCode[0]) {
Eric Christopherbc5d2492012-05-19 00:51:56 +0000384 default:
Jack Carterb2fd5f62012-06-21 17:14:46 +0000385 // See if this is a generic print operand
386 return AsmPrinter::PrintAsmOperand(MI,OpNum,AsmVariant,ExtraCode,O);
Eric Christopherbc5d2492012-05-19 00:51:56 +0000387 case 'X': // hex const int
388 if ((MO.getType()) != MachineOperand::MO_Immediate)
389 return true;
390 O << "0x" << StringRef(utohexstr(MO.getImm())).lower();
391 return false;
392 case 'x': // hex const int (low 16 bits)
393 if ((MO.getType()) != MachineOperand::MO_Immediate)
394 return true;
395 O << "0x" << StringRef(utohexstr(MO.getImm() & 0xffff)).lower();
396 return false;
397 case 'd': // decimal const int
398 if ((MO.getType()) != MachineOperand::MO_Immediate)
399 return true;
400 O << MO.getImm();
401 return false;
Eric Christopherf481ab32012-05-30 19:05:19 +0000402 case 'm': // decimal const int minus 1
403 if ((MO.getType()) != MachineOperand::MO_Immediate)
404 return true;
405 O << MO.getImm() - 1;
406 return false;
Jack Carter27747b52012-06-28 20:46:26 +0000407 case 'z': {
408 // $0 if zero, regular printing otherwise
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000409 if (MO.getType() != MachineOperand::MO_Immediate)
410 return true;
411 int64_t Val = MO.getImm();
412 if (Val)
413 O << Val;
414 else
415 O << "$0";
416 return false;
417 }
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000418 case 'D': // Second part of a double word register operand
419 case 'L': // Low order register of a double word register operand
Jack Cartera62ba822012-07-18 06:41:36 +0000420 case 'M': // High order register of a double word register operand
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000421 {
Jack Carterb2af5122012-07-05 23:58:21 +0000422 if (OpNum == 0)
423 return true;
424 const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
425 if (!FlagsOP.isImm())
426 return true;
427 unsigned Flags = FlagsOP.getImm();
428 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Jack Carter2ab73b12012-07-06 02:44:22 +0000429 // Number of registers represented by this operand. We are looking
430 // for 2 for 32 bit mode and 1 for 64 bit mode.
Jack Carterb2af5122012-07-05 23:58:21 +0000431 if (NumVals != 2) {
Jack Carter2ab73b12012-07-06 02:44:22 +0000432 if (Subtarget->isGP64bit() && NumVals == 1 && MO.isReg()) {
Jack Carterb2af5122012-07-05 23:58:21 +0000433 unsigned Reg = MO.getReg();
434 O << '$' << MipsInstPrinter::getRegisterName(Reg);
435 return false;
436 }
437 return true;
438 }
Jack Carter42ebf982012-07-11 21:41:49 +0000439
440 unsigned RegOp = OpNum;
441 if (!Subtarget->isGP64bit()){
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000442 // Endianess reverses which register holds the high or low value
Jack Cartera62ba822012-07-18 06:41:36 +0000443 // between M and L.
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000444 switch(ExtraCode[0]) {
Jack Cartera62ba822012-07-18 06:41:36 +0000445 case 'M':
446 RegOp = (Subtarget->isLittle()) ? OpNum + 1 : OpNum;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000447 break;
448 case 'L':
Jack Cartera62ba822012-07-18 06:41:36 +0000449 RegOp = (Subtarget->isLittle()) ? OpNum : OpNum + 1;
450 break;
451 case 'D': // Always the second part
452 RegOp = OpNum + 1;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000453 }
454 if (RegOp >= MI->getNumOperands())
455 return true;
456 const MachineOperand &MO = MI->getOperand(RegOp);
457 if (!MO.isReg())
458 return true;
459 unsigned Reg = MO.getReg();
460 O << '$' << MipsInstPrinter::getRegisterName(Reg);
461 return false;
Jack Carterb2af5122012-07-05 23:58:21 +0000462 }
Eric Christophered51b9e2012-05-10 21:48:22 +0000463 }
Daniel Sanders8b59af12013-11-12 12:56:01 +0000464 case 'w':
465 // Print MSA registers for the 'f' constraint
466 // In LLVM, the 'w' modifier doesn't need to do anything.
467 // We can just call printOperand as normal.
468 break;
Jack Carter2ab73b12012-07-06 02:44:22 +0000469 }
470 }
Eric Christophered51b9e2012-05-10 21:48:22 +0000471
472 printOperand(MI, OpNum, O);
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000473 return false;
474}
475
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000476bool MipsAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
477 unsigned OpNum, unsigned AsmVariant,
478 const char *ExtraCode,
479 raw_ostream &O) {
Jack Carterb04e3572013-04-09 23:19:50 +0000480 int Offset = 0;
481 // Currently we are expecting either no ExtraCode or 'D'
482 if (ExtraCode) {
483 if (ExtraCode[0] == 'D')
484 Offset = 4;
485 else
486 return true; // Unknown modifier.
487 }
Jia Liuf54f60f2012-02-28 07:46:26 +0000488
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000489 const MachineOperand &MO = MI->getOperand(OpNum);
490 assert(MO.isReg() && "unexpected inline asm memory operand");
Jack Carterb04e3572013-04-09 23:19:50 +0000491 O << Offset << "($" << MipsInstPrinter::getRegisterName(MO.getReg()) << ")";
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000492
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000493 return false;
494}
495
Chris Lattner76c564b2010-04-04 04:47:45 +0000496void MipsAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
497 raw_ostream &O) {
Rafael Espindola58873562014-01-03 19:21:54 +0000498 const DataLayout *DL = TM.getDataLayout();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000499 const MachineOperand &MO = MI->getOperand(opNum);
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +0000500 bool closeP = false;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000501
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000502 if (MO.getTargetFlags())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000503 closeP = true;
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000504
505 switch(MO.getTargetFlags()) {
506 case MipsII::MO_GPREL: O << "%gp_rel("; break;
507 case MipsII::MO_GOT_CALL: O << "%call16("; break;
Akira Hatanaka56d9ef52011-04-01 21:41:06 +0000508 case MipsII::MO_GOT: O << "%got("; break;
509 case MipsII::MO_ABS_HI: O << "%hi("; break;
510 case MipsII::MO_ABS_LO: O << "%lo("; break;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +0000511 case MipsII::MO_TLSGD: O << "%tlsgd("; break;
512 case MipsII::MO_GOTTPREL: O << "%gottprel("; break;
513 case MipsII::MO_TPREL_HI: O << "%tprel_hi("; break;
514 case MipsII::MO_TPREL_LO: O << "%tprel_lo("; break;
Akira Hatanaka25ce3642011-09-22 03:09:07 +0000515 case MipsII::MO_GPOFF_HI: O << "%hi(%neg(%gp_rel("; break;
516 case MipsII::MO_GPOFF_LO: O << "%lo(%neg(%gp_rel("; break;
517 case MipsII::MO_GOT_DISP: O << "%got_disp("; break;
518 case MipsII::MO_GOT_PAGE: O << "%got_page("; break;
519 case MipsII::MO_GOT_OFST: O << "%got_ofst("; break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000520 }
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000521
Chris Lattnereb2cc682009-09-13 20:31:40 +0000522 switch (MO.getType()) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000523 case MachineOperand::MO_Register:
Akira Hatanaka9c6028f2011-07-07 23:56:50 +0000524 O << '$'
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000525 << StringRef(MipsInstPrinter::getRegisterName(MO.getReg())).lower();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000526 break;
527
528 case MachineOperand::MO_Immediate:
Akira Hatanaka2db176c2011-05-24 21:22:21 +0000529 O << MO.getImm();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000530 break;
531
532 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner29bdac42010-03-13 21:04:28 +0000533 O << *MO.getMBB()->getSymbol();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000534 return;
535
536 case MachineOperand::MO_GlobalAddress:
Rafael Espindola79858aa2013-10-29 17:07:16 +0000537 O << *getSymbol(MO.getGlobal());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000538 break;
539
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000540 case MachineOperand::MO_BlockAddress: {
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000541 MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000542 O << BA->getName();
543 break;
544 }
545
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000546 case MachineOperand::MO_ConstantPoolIndex:
Rafael Espindola58873562014-01-03 19:21:54 +0000547 O << DL->getPrivateGlobalPrefix() << "CPI"
Chris Lattnera5bb3702007-12-30 23:10:15 +0000548 << getFunctionNumber() << "_" << MO.getIndex();
Bruno Cardoso Lopes4713b282009-11-19 06:06:13 +0000549 if (MO.getOffset())
550 O << "+" << MO.getOffset();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000551 break;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000552
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000553 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000554 llvm_unreachable("<unknown operand type>");
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000555 }
556
557 if (closeP) O << ")";
558}
559
Chris Lattner76c564b2010-04-04 04:47:45 +0000560void MipsAsmPrinter::printUnsignedImm(const MachineInstr *MI, int opNum,
561 raw_ostream &O) {
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000562 const MachineOperand &MO = MI->getOperand(opNum);
Devang Patel12f68552010-04-27 22:24:37 +0000563 if (MO.isImm())
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000564 O << (unsigned short int)MO.getImm();
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000565 else
Chris Lattner76c564b2010-04-04 04:47:45 +0000566 printOperand(MI, opNum, O);
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000567}
568
Daniel Sanders3f6eb542013-11-12 10:45:18 +0000569void MipsAsmPrinter::printUnsignedImm8(const MachineInstr *MI, int opNum,
570 raw_ostream &O) {
571 const MachineOperand &MO = MI->getOperand(opNum);
572 if (MO.isImm())
573 O << (unsigned short int)(unsigned char)MO.getImm();
574 else
575 printOperand(MI, opNum, O);
576}
577
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000578void MipsAsmPrinter::
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000579printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O) {
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000580 // Load/Store memory operands -- imm($reg)
581 // If PIC target the target is loaded as the
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +0000582 // pattern lw $25,%call16($28)
Chris Lattner76c564b2010-04-04 04:47:45 +0000583 printOperand(MI, opNum+1, O);
Akira Hatanaka2e766ed2011-07-07 18:57:00 +0000584 O << "(";
585 printOperand(MI, opNum, O);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000586 O << ")";
587}
588
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000589void MipsAsmPrinter::
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000590printMemOperandEA(const MachineInstr *MI, int opNum, raw_ostream &O) {
591 // when using stack locations for not load/store instructions
592 // print the same way as all normal 3 operand instructions.
593 printOperand(MI, opNum, O);
594 O << ", ";
595 printOperand(MI, opNum+1, O);
596 return;
597}
598
599void MipsAsmPrinter::
Chris Lattner76c564b2010-04-04 04:47:45 +0000600printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
601 const char *Modifier) {
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000602 const MachineOperand &MO = MI->getOperand(opNum);
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000603 O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000604}
605
Bob Wilsonb633d7a2009-09-30 22:06:26 +0000606void MipsAsmPrinter::EmitStartOfAsmFile(Module &M) {
Jack Carterf9f753c2013-06-18 19:47:15 +0000607 // TODO: Need to add -mabicalls and -mno-abicalls flags.
608 // Currently we assume that -mabicalls is the default.
Jack Carter0cd3c192014-01-06 23:27:31 +0000609 getTargetStreamer().emitDirectiveAbiCalls();
610 Reloc::Model RM = Subtarget->getRelocationModel();
611 if (RM == Reloc::Static && !Subtarget->hasMips64())
612 getTargetStreamer().emitDirectiveOptionPic0();
Jack Carterf9f753c2013-06-18 19:47:15 +0000613
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000614 // Tell the assembler which ABI we are using
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000615 std::string SectionName = std::string(".mdebug.") + getCurrentABIString();
616 OutStreamer.SwitchSection(OutContext.getELFSection(
617 SectionName, ELF::SHT_PROGBITS, 0, SectionKind::getDataRel()));
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000618
619 // TODO: handle O64 ABI
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000620
621 if (Subtarget->isABI_EABI()) {
622 if (Subtarget->isGP32bit())
623 OutStreamer.SwitchSection(
624 OutContext.getELFSection(".gcc_compiled_long32", ELF::SHT_PROGBITS, 0,
625 SectionKind::getDataRel()));
626 else
627 OutStreamer.SwitchSection(
628 OutContext.getELFSection(".gcc_compiled_long64", ELF::SHT_PROGBITS, 0,
629 SectionKind::getDataRel()));
Benjamin Kramer0151d7b2010-04-05 10:17:15 +0000630 }
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000631
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000632 // return to the text section
633 OutStreamer.SwitchSection(OutContext.getObjectFileInfo()->getTextSection());
Jack Carterc1b17ed2013-01-18 21:20:38 +0000634}
635
636void MipsAsmPrinter::EmitEndOfAsmFile(Module &M) {
Jack Carterc1b17ed2013-01-18 21:20:38 +0000637 // Emit Mips ELF register info
638 Subtarget->getMReginfo().emitMipsReginfoSectionCG(
639 OutStreamer, getObjFileLowering(), *Subtarget);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000640}
641
Akira Hatanakaf2bcad92011-07-01 01:04:43 +0000642void MipsAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
643 raw_ostream &OS) {
644 // TODO: implement
645}
646
Bob Wilson5a495fe2009-06-23 23:59:40 +0000647// Force static initialization.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000648extern "C" void LLVMInitializeMipsAsmPrinter() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +0000649 RegisterAsmPrinter<MipsAsmPrinter> X(TheMipsTarget);
650 RegisterAsmPrinter<MipsAsmPrinter> Y(TheMipselTarget);
Akira Hatanaka3d673cc2011-09-21 03:00:58 +0000651 RegisterAsmPrinter<MipsAsmPrinter> A(TheMips64Target);
652 RegisterAsmPrinter<MipsAsmPrinter> B(TheMips64elTarget);
Daniel Dunbare8338102009-07-15 20:24:03 +0000653}