blob: b6afc174e834024056f310541efcd2dbb0ecdcdb [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"
Chris Lattner7b26fce2009-08-22 20:48:53 +000035#include "llvm/MC/MCAsmInfo.h"
Rafael Espindolaac4ad252013-10-05 16:42:21 +000036#include "llvm/MC/MCELFStreamer.h"
Akira Hatanaka9c6028f2011-07-07 23:56:50 +000037#include "llvm/MC/MCInst.h"
Chris Lattner4cd44982009-09-13 17:14:04 +000038#include "llvm/MC/MCSymbol.h"
Jack Carterab3cb422013-02-19 22:04:37 +000039#include "llvm/Support/ELF.h"
Jack Carterb2af5122012-07-05 23:58:21 +000040#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000041#include "llvm/Support/raw_ostream.h"
Chris Lattner0b822ab2010-03-12 21:19:23 +000042#include "llvm/Target/Mangler.h"
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +000043#include "llvm/Target/TargetLoweringObjectFile.h"
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +000044#include "llvm/Target/TargetOptions.h"
Akira Hatanakaf2bcad92011-07-01 01:04:43 +000045
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000046using namespace llvm;
47
Rafael Espindolaa17151a2013-10-08 13:08:17 +000048MipsTargetStreamer &MipsAsmPrinter::getTargetStreamer() {
49 return static_cast<MipsTargetStreamer &>(OutStreamer.getTargetStreamer());
50}
51
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000052bool MipsAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Reed Kotler1595f362013-04-09 19:46:01 +000053 // Initialize TargetLoweringObjectFile.
54 if (Subtarget->allowMixed16_32())
55 const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
56 .Initialize(OutContext, TM);
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000057 MipsFI = MF.getInfo<MipsFunctionInfo>();
58 AsmPrinter::runOnMachineFunction(MF);
59 return true;
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +000060}
61
Akira Hatanaka42a35242012-09-27 01:59:07 +000062bool MipsAsmPrinter::lowerOperand(const MachineOperand &MO, MCOperand &MCOp) {
63 MCOp = MCInstLowering.LowerOperand(MO);
64 return MCOp.isValid();
65}
66
67#include "MipsGenMCPseudoLowering.inc"
68
Akira Hatanakaddd12652011-07-07 20:10:52 +000069void MipsAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Akira Hatanakaddd12652011-07-07 20:10:52 +000070 if (MI->isDebugValue()) {
Bruno Cardoso Lopescd1d4472011-12-30 21:09:41 +000071 SmallString<128> Str;
72 raw_svector_ostream OS(Str);
73
Akira Hatanakaddd12652011-07-07 20:10:52 +000074 PrintDebugValueComment(MI, OS);
75 return;
76 }
77
Akira Hatanaka5ac78682012-06-13 23:25:52 +000078 MachineBasicBlock::const_instr_iterator I = MI;
79 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
80
81 do {
Akira Hatanaka556135d2013-02-06 21:50:15 +000082 // Do any auto-generated pseudo lowerings.
83 if (emitPseudoExpansionLowering(OutStreamer, &*I))
84 continue;
Jack Carterc20a21b2012-08-28 19:07:39 +000085
Reed Kotler76c9bcd2013-02-15 21:05:58 +000086 // The inMips16Mode() test is not permanent.
87 // Some instructions are marked as pseudo right now which
88 // would make the test fail for the wrong reason but
89 // that will be fixed soon. We need this here because we are
90 // removing another test for this situation downstream in the
91 // callchain.
92 //
93 if (I->isPseudo() && !Subtarget->inMips16Mode())
94 llvm_unreachable("Pseudo opcode found in EmitInstruction()");
95
Akira Hatanaka556135d2013-02-06 21:50:15 +000096 MCInst TmpInst0;
97 MCInstLowering.Lower(I, TmpInst0);
Akira Hatanaka5ac78682012-06-13 23:25:52 +000098 OutStreamer.EmitInstruction(TmpInst0);
Akira Hatanaka556135d2013-02-06 21:50:15 +000099 } while ((++I != E) && I->isInsideBundle()); // Delay slot check
Akira Hatanakaddd12652011-07-07 20:10:52 +0000100}
101
Akira Hatanakae2489122011-04-15 21:51:11 +0000102//===----------------------------------------------------------------------===//
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000103//
104// Mips Asm Directives
105//
106// -- Frame directive "frame Stackpointer, Stacksize, RARegister"
107// Describe the stack frame.
108//
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000109// -- Mask directives "(f)mask bitmask, offset"
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000110// Tells the assembler which registers are saved and where.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000111// bitmask - contain a little endian bitset indicating which registers are
112// saved on function prologue (e.g. with a 0x80000000 mask, the
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000113// assembler knows the register 31 (RA) is saved at prologue.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000114// offset - the position before stack pointer subtraction indicating where
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000115// the first saved register on prologue is located. (e.g. with a
116//
117// Consider the following function prologue:
118//
Bill Wendling97925ec2008-02-27 06:33:05 +0000119// .frame $fp,48,$ra
120// .mask 0xc0000000,-8
121// addiu $sp, $sp, -48
122// sw $ra, 40($sp)
123// sw $fp, 36($sp)
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000124//
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000125// With a 0xc0000000 mask, the assembler knows the register 31 (RA) and
126// 30 (FP) are saved at prologue. As the save order on prologue is from
127// left to right, RA is saved first. A -8 offset means that after the
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000128// stack pointer subtration, the first register in the mask (RA) will be
129// saved at address 48-8=40.
130//
Akira Hatanakae2489122011-04-15 21:51:11 +0000131//===----------------------------------------------------------------------===//
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000132
Akira Hatanakae2489122011-04-15 21:51:11 +0000133//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000134// Mask directives
Akira Hatanakae2489122011-04-15 21:51:11 +0000135//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000136
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000137// Create a bitmask with all callee saved registers for CPU or Floating Point
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000138// registers. For CPU registers consider RA, GP and FP for saving if necessary.
Chris Lattner76c564b2010-04-04 04:47:45 +0000139void MipsAsmPrinter::printSavedRegsBitmask(raw_ostream &O) {
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000140 // CPU and FPU Saved Registers Bitmasks
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000141 unsigned CPUBitmask = 0, FPUBitmask = 0;
142 int CPUTopSavedRegOff, FPUTopSavedRegOff;
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000143
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000144 // Set the CPU and FPU Bitmasks
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000145 const MachineFrameInfo *MFI = MF->getFrameInfo();
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000146 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000147 // size of stack area to which FP callee-saved regs are saved.
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000148 unsigned CPURegSize = Mips::GPR32RegClass.getSize();
Craig Topperc7242e02012-04-20 07:30:17 +0000149 unsigned FGR32RegSize = Mips::FGR32RegClass.getSize();
150 unsigned AFGR64RegSize = Mips::AFGR64RegClass.getSize();
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000151 bool HasAFGR64Reg = false;
152 unsigned CSFPRegsSize = 0;
153 unsigned i, e = CSI.size();
154
155 // Set FPU Bitmask.
156 for (i = 0; i != e; ++i) {
Rafael Espindolaf2dffce2010-06-02 20:02:30 +0000157 unsigned Reg = CSI[i].getReg();
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000158 if (Mips::GPR32RegClass.contains(Reg))
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000159 break;
160
Akira Hatanaka5d6faed2012-12-10 20:04:40 +0000161 unsigned RegNum = TM.getRegisterInfo()->getEncodingValue(Reg);
Craig Topperc7242e02012-04-20 07:30:17 +0000162 if (Mips::AFGR64RegClass.contains(Reg)) {
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000163 FPUBitmask |= (3 << RegNum);
164 CSFPRegsSize += AFGR64RegSize;
165 HasAFGR64Reg = true;
166 continue;
167 }
168
169 FPUBitmask |= (1 << RegNum);
170 CSFPRegsSize += FGR32RegSize;
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000171 }
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000172
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000173 // Set CPU Bitmask.
174 for (; i != e; ++i) {
175 unsigned Reg = CSI[i].getReg();
Akira Hatanaka5d6faed2012-12-10 20:04:40 +0000176 unsigned RegNum = TM.getRegisterInfo()->getEncodingValue(Reg);
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000177 CPUBitmask |= (1 << RegNum);
178 }
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000179
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000180 // FP Regs are saved right below where the virtual frame pointer points to.
181 FPUTopSavedRegOff = FPUBitmask ?
182 (HasAFGR64Reg ? -AFGR64RegSize : -FGR32RegSize) : 0;
183
184 // CPU Regs are saved below FP Regs.
185 CPUTopSavedRegOff = CPUBitmask ? -CSFPRegsSize - CPURegSize : 0;
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000186
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000187 // Print CPUBitmask
Chris Lattner76c564b2010-04-04 04:47:45 +0000188 O << "\t.mask \t"; printHex32(CPUBitmask, O);
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000189 O << ',' << CPUTopSavedRegOff << '\n';
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000190
191 // Print FPUBitmask
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000192 O << "\t.fmask\t"; printHex32(FPUBitmask, O);
193 O << "," << FPUTopSavedRegOff << '\n';
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000194}
195
196// Print a 32 bit hex number with all numbers.
Chris Lattner76c564b2010-04-04 04:47:45 +0000197void MipsAsmPrinter::printHex32(unsigned Value, raw_ostream &O) {
Owen Anderson93719642008-08-21 00:14:44 +0000198 O << "0x";
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000199 for (int i = 7; i >= 0; i--)
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000200 O.write_hex((Value & (0xF << (i*4))) >> (i*4));
Bruno Cardoso Lopesbcda5e22007-07-11 23:24:41 +0000201}
202
Akira Hatanakae2489122011-04-15 21:51:11 +0000203//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000204// Frame and Set directives
Akira Hatanakae2489122011-04-15 21:51:11 +0000205//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000206
207/// Frame Directive
Chris Lattner5e596182010-04-04 07:05:53 +0000208void MipsAsmPrinter::emitFrameDirective() {
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000209 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
210
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000211 unsigned stackReg = RI.getFrameRegister(*MF);
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000212 unsigned returnReg = RI.getRARegister();
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000213 unsigned stackSize = MF->getFrameInfo()->getStackSize();
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000214
Jia Liuf54f60f2012-02-28 07:46:26 +0000215 if (OutStreamer.hasRawTextSupport())
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000216 OutStreamer.EmitRawText("\t.frame\t$" +
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000217 StringRef(MipsInstPrinter::getRegisterName(stackReg)).lower() +
Akira Hatanaka9c6028f2011-07-07 23:56:50 +0000218 "," + Twine(stackSize) + ",$" +
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000219 StringRef(MipsInstPrinter::getRegisterName(returnReg)).lower());
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000220}
221
222/// Emit Set directives.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000223const char *MipsAsmPrinter::getCurrentABIString() const {
Chris Lattner5e596182010-04-04 07:05:53 +0000224 switch (Subtarget->getTargetABI()) {
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000225 case MipsSubtarget::O32: return "abi32";
Chris Lattner5e596182010-04-04 07:05:53 +0000226 case MipsSubtarget::N32: return "abiN32";
227 case MipsSubtarget::N64: return "abi64";
228 case MipsSubtarget::EABI: return "eabi32"; // TODO: handle eabi64
Dmitri Gribenkoca1e27b2012-09-10 21:26:47 +0000229 default: llvm_unreachable("Unknown Mips ABI");
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000230 }
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000231}
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000232
Chris Lattner5d9fb4b2010-01-27 23:23:58 +0000233void MipsAsmPrinter::EmitFunctionEntryLabel() {
Akira Hatanakaa649cc72012-05-24 18:37:43 +0000234 if (OutStreamer.hasRawTextSupport()) {
235 if (Subtarget->inMips16Mode())
236 OutStreamer.EmitRawText(StringRef("\t.set\tmips16"));
237 else
238 OutStreamer.EmitRawText(StringRef("\t.set\tnomips16"));
Akira Hatanaka5fa54122012-06-13 02:41:14 +0000239 // leave out until FSF available gas has micromips changes
240 // OutStreamer.EmitRawText(StringRef("\t.set\tnomicromips"));
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000241 OutStreamer.EmitRawText("\t.ent\t" + Twine(CurrentFnSym->getName()));
Akira Hatanakaa649cc72012-05-24 18:37:43 +0000242 }
Jack Carterab3cb422013-02-19 22:04:37 +0000243
244 if (Subtarget->inMicroMipsMode())
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000245 getTargetStreamer().emitMipsHackSTOCG(CurrentFnSym,
246 (unsigned)ELF::STO_MIPS_MICROMIPS);
Chris Lattner5d9fb4b2010-01-27 23:23:58 +0000247 OutStreamer.EmitLabel(CurrentFnSym);
248}
249
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000250/// EmitFunctionBodyStart - Targets can override this to emit stuff before
251/// the first basic block in the function.
252void MipsAsmPrinter::EmitFunctionBodyStart() {
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +0000253 MCInstLowering.Initialize(Mang, &MF->getContext());
254
Reed Kotler0f2b10e2013-05-03 23:17:24 +0000255 bool IsNakedFunction =
256 MF->getFunction()->
257 getAttributes().hasAttribute(AttributeSet::FunctionIndex,
258 Attribute::Naked);
259 if (!IsNakedFunction)
260 emitFrameDirective();
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000261
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000262 if (OutStreamer.hasRawTextSupport()) {
263 SmallString<128> Str;
264 raw_svector_ostream OS(Str);
Reed Kotler0f2b10e2013-05-03 23:17:24 +0000265 if (!IsNakedFunction)
266 printSavedRegsBitmask(OS);
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000267 OutStreamer.EmitRawText(OS.str());
Reed Kotlerf0221472013-02-15 01:04:38 +0000268 if (!Subtarget->inMips16Mode()) {
269 OutStreamer.EmitRawText(StringRef("\t.set\tnoreorder"));
270 OutStreamer.EmitRawText(StringRef("\t.set\tnomacro"));
271 OutStreamer.EmitRawText(StringRef("\t.set\tnoat"));
272 }
Akira Hatanaka8f3573032012-05-12 00:48:43 +0000273 }
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000274}
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000275
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000276/// EmitFunctionBodyEnd - Targets can override this to emit stuff after
277/// the last basic block in the function.
278void MipsAsmPrinter::EmitFunctionBodyEnd() {
Chris Lattnerfd97a332010-01-28 01:48:52 +0000279 // There are instruction for this macros, but they must
280 // always be at the function end, and we can't emit and
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000281 // break with BB logic.
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000282 if (OutStreamer.hasRawTextSupport()) {
Reed Kotlerf0221472013-02-15 01:04:38 +0000283 if (!Subtarget->inMips16Mode()) {
284 OutStreamer.EmitRawText(StringRef("\t.set\tat"));
285 OutStreamer.EmitRawText(StringRef("\t.set\tmacro"));
286 OutStreamer.EmitRawText(StringRef("\t.set\treorder"));
287 }
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000288 OutStreamer.EmitRawText("\t.end\t" + Twine(CurrentFnSym->getName()));
289 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000290}
291
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000292/// isBlockOnlyReachableByFallthough - Return true if the basic block has
293/// exactly one predecessor and the control transfer mechanism between
294/// the predecessor and this block is a fall-through.
Akira Hatanakae2489122011-04-15 21:51:11 +0000295bool MipsAsmPrinter::isBlockOnlyReachableByFallthrough(const MachineBasicBlock*
296 MBB) const {
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000297 // The predecessor has to be immediately before this block.
298 const MachineBasicBlock *Pred = *MBB->pred_begin();
299
300 // If the predecessor is a switch statement, assume a jump table
301 // implementation, so it is not a fall through.
302 if (const BasicBlock *bb = Pred->getBasicBlock())
303 if (isa<SwitchInst>(bb->getTerminator()))
304 return false;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000305
Akira Hatanakae625ba42011-04-01 18:57:38 +0000306 // If this is a landing pad, it isn't a fall through. If it has no preds,
307 // then nothing falls through to it.
308 if (MBB->isLandingPad() || MBB->pred_empty())
309 return false;
310
311 // If there isn't exactly one predecessor, it can't be a fall through.
312 MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI;
313 ++PI2;
Jia Liuf54f60f2012-02-28 07:46:26 +0000314
Akira Hatanakae625ba42011-04-01 18:57:38 +0000315 if (PI2 != MBB->pred_end())
Jia Liuf54f60f2012-02-28 07:46:26 +0000316 return false;
Akira Hatanakae625ba42011-04-01 18:57:38 +0000317
318 // The predecessor has to be immediately before this block.
319 if (!Pred->isLayoutSuccessor(MBB))
320 return false;
Jia Liuf54f60f2012-02-28 07:46:26 +0000321
Akira Hatanakae625ba42011-04-01 18:57:38 +0000322 // If the block is completely empty, then it definitely does fall through.
323 if (Pred->empty())
324 return true;
Jia Liuf54f60f2012-02-28 07:46:26 +0000325
Akira Hatanakae625ba42011-04-01 18:57:38 +0000326 // Otherwise, check the last instruction.
327 // Check if the last terminator is an unconditional branch.
328 MachineBasicBlock::const_iterator I = Pred->end();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000329 while (I != Pred->begin() && !(--I)->isTerminator()) ;
Akira Hatanakae625ba42011-04-01 18:57:38 +0000330
Evan Cheng7f8e5632011-12-07 07:15:52 +0000331 return !I->isBarrier();
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000332}
333
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000334// Print out an operand for an inline asm expression.
Eric Christophered51b9e2012-05-10 21:48:22 +0000335bool MipsAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Chris Lattner3bb09762010-04-04 05:29:35 +0000336 unsigned AsmVariant,const char *ExtraCode,
337 raw_ostream &O) {
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000338 // Does this asm operand have a single letter operand modifier?
Eric Christophered51b9e2012-05-10 21:48:22 +0000339 if (ExtraCode && ExtraCode[0]) {
340 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000341
Eric Christophered51b9e2012-05-10 21:48:22 +0000342 const MachineOperand &MO = MI->getOperand(OpNum);
343 switch (ExtraCode[0]) {
Eric Christopherbc5d2492012-05-19 00:51:56 +0000344 default:
Jack Carterb2fd5f62012-06-21 17:14:46 +0000345 // See if this is a generic print operand
346 return AsmPrinter::PrintAsmOperand(MI,OpNum,AsmVariant,ExtraCode,O);
Eric Christopherbc5d2492012-05-19 00:51:56 +0000347 case 'X': // hex const int
348 if ((MO.getType()) != MachineOperand::MO_Immediate)
349 return true;
350 O << "0x" << StringRef(utohexstr(MO.getImm())).lower();
351 return false;
352 case 'x': // hex const int (low 16 bits)
353 if ((MO.getType()) != MachineOperand::MO_Immediate)
354 return true;
355 O << "0x" << StringRef(utohexstr(MO.getImm() & 0xffff)).lower();
356 return false;
357 case 'd': // decimal const int
358 if ((MO.getType()) != MachineOperand::MO_Immediate)
359 return true;
360 O << MO.getImm();
361 return false;
Eric Christopherf481ab32012-05-30 19:05:19 +0000362 case 'm': // decimal const int minus 1
363 if ((MO.getType()) != MachineOperand::MO_Immediate)
364 return true;
365 O << MO.getImm() - 1;
366 return false;
Jack Carter27747b52012-06-28 20:46:26 +0000367 case 'z': {
368 // $0 if zero, regular printing otherwise
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000369 if (MO.getType() != MachineOperand::MO_Immediate)
370 return true;
371 int64_t Val = MO.getImm();
372 if (Val)
373 O << Val;
374 else
375 O << "$0";
376 return false;
377 }
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000378 case 'D': // Second part of a double word register operand
379 case 'L': // Low order register of a double word register operand
Jack Cartera62ba822012-07-18 06:41:36 +0000380 case 'M': // High order register of a double word register operand
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000381 {
Jack Carterb2af5122012-07-05 23:58:21 +0000382 if (OpNum == 0)
383 return true;
384 const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
385 if (!FlagsOP.isImm())
386 return true;
387 unsigned Flags = FlagsOP.getImm();
388 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Jack Carter2ab73b12012-07-06 02:44:22 +0000389 // Number of registers represented by this operand. We are looking
390 // for 2 for 32 bit mode and 1 for 64 bit mode.
Jack Carterb2af5122012-07-05 23:58:21 +0000391 if (NumVals != 2) {
Jack Carter2ab73b12012-07-06 02:44:22 +0000392 if (Subtarget->isGP64bit() && NumVals == 1 && MO.isReg()) {
Jack Carterb2af5122012-07-05 23:58:21 +0000393 unsigned Reg = MO.getReg();
394 O << '$' << MipsInstPrinter::getRegisterName(Reg);
395 return false;
396 }
397 return true;
398 }
Jack Carter42ebf982012-07-11 21:41:49 +0000399
400 unsigned RegOp = OpNum;
401 if (!Subtarget->isGP64bit()){
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000402 // Endianess reverses which register holds the high or low value
Jack Cartera62ba822012-07-18 06:41:36 +0000403 // between M and L.
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000404 switch(ExtraCode[0]) {
Jack Cartera62ba822012-07-18 06:41:36 +0000405 case 'M':
406 RegOp = (Subtarget->isLittle()) ? OpNum + 1 : OpNum;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000407 break;
408 case 'L':
Jack Cartera62ba822012-07-18 06:41:36 +0000409 RegOp = (Subtarget->isLittle()) ? OpNum : OpNum + 1;
410 break;
411 case 'D': // Always the second part
412 RegOp = OpNum + 1;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000413 }
414 if (RegOp >= MI->getNumOperands())
415 return true;
416 const MachineOperand &MO = MI->getOperand(RegOp);
417 if (!MO.isReg())
418 return true;
419 unsigned Reg = MO.getReg();
420 O << '$' << MipsInstPrinter::getRegisterName(Reg);
421 return false;
Jack Carterb2af5122012-07-05 23:58:21 +0000422 }
Eric Christophered51b9e2012-05-10 21:48:22 +0000423 }
Jack Carter2ab73b12012-07-06 02:44:22 +0000424 }
425 }
Eric Christophered51b9e2012-05-10 21:48:22 +0000426
427 printOperand(MI, OpNum, O);
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000428 return false;
429}
430
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000431bool MipsAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
432 unsigned OpNum, unsigned AsmVariant,
433 const char *ExtraCode,
434 raw_ostream &O) {
Jack Carterb04e3572013-04-09 23:19:50 +0000435 int Offset = 0;
436 // Currently we are expecting either no ExtraCode or 'D'
437 if (ExtraCode) {
438 if (ExtraCode[0] == 'D')
439 Offset = 4;
440 else
441 return true; // Unknown modifier.
442 }
Jia Liuf54f60f2012-02-28 07:46:26 +0000443
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000444 const MachineOperand &MO = MI->getOperand(OpNum);
445 assert(MO.isReg() && "unexpected inline asm memory operand");
Jack Carterb04e3572013-04-09 23:19:50 +0000446 O << Offset << "($" << MipsInstPrinter::getRegisterName(MO.getReg()) << ")";
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000447
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000448 return false;
449}
450
Chris Lattner76c564b2010-04-04 04:47:45 +0000451void MipsAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
452 raw_ostream &O) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000453 const MachineOperand &MO = MI->getOperand(opNum);
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +0000454 bool closeP = false;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000455
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000456 if (MO.getTargetFlags())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000457 closeP = true;
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000458
459 switch(MO.getTargetFlags()) {
460 case MipsII::MO_GPREL: O << "%gp_rel("; break;
461 case MipsII::MO_GOT_CALL: O << "%call16("; break;
Akira Hatanaka56d9ef52011-04-01 21:41:06 +0000462 case MipsII::MO_GOT: O << "%got("; break;
463 case MipsII::MO_ABS_HI: O << "%hi("; break;
464 case MipsII::MO_ABS_LO: O << "%lo("; break;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +0000465 case MipsII::MO_TLSGD: O << "%tlsgd("; break;
466 case MipsII::MO_GOTTPREL: O << "%gottprel("; break;
467 case MipsII::MO_TPREL_HI: O << "%tprel_hi("; break;
468 case MipsII::MO_TPREL_LO: O << "%tprel_lo("; break;
Akira Hatanaka25ce3642011-09-22 03:09:07 +0000469 case MipsII::MO_GPOFF_HI: O << "%hi(%neg(%gp_rel("; break;
470 case MipsII::MO_GPOFF_LO: O << "%lo(%neg(%gp_rel("; break;
471 case MipsII::MO_GOT_DISP: O << "%got_disp("; break;
472 case MipsII::MO_GOT_PAGE: O << "%got_page("; break;
473 case MipsII::MO_GOT_OFST: O << "%got_ofst("; break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000474 }
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000475
Chris Lattnereb2cc682009-09-13 20:31:40 +0000476 switch (MO.getType()) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000477 case MachineOperand::MO_Register:
Akira Hatanaka9c6028f2011-07-07 23:56:50 +0000478 O << '$'
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000479 << StringRef(MipsInstPrinter::getRegisterName(MO.getReg())).lower();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000480 break;
481
482 case MachineOperand::MO_Immediate:
Akira Hatanaka2db176c2011-05-24 21:22:21 +0000483 O << MO.getImm();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000484 break;
485
486 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner29bdac42010-03-13 21:04:28 +0000487 O << *MO.getMBB()->getSymbol();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000488 return;
489
490 case MachineOperand::MO_GlobalAddress:
Chris Lattner0b822ab2010-03-12 21:19:23 +0000491 O << *Mang->getSymbol(MO.getGlobal());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000492 break;
493
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000494 case MachineOperand::MO_BlockAddress: {
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000495 MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000496 O << BA->getName();
497 break;
498 }
499
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000500 case MachineOperand::MO_ExternalSymbol:
Chris Lattner8b5d55e2010-01-17 21:43:43 +0000501 O << *GetExternalSymbolSymbol(MO.getSymbolName());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000502 break;
503
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +0000504 case MachineOperand::MO_JumpTableIndex:
Chris Lattnere9a75a62009-08-22 21:43:10 +0000505 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner719e9082010-01-16 00:21:18 +0000506 << '_' << MO.getIndex();
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +0000507 break;
508
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000509 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnere9a75a62009-08-22 21:43:10 +0000510 O << MAI->getPrivateGlobalPrefix() << "CPI"
Chris Lattnera5bb3702007-12-30 23:10:15 +0000511 << getFunctionNumber() << "_" << MO.getIndex();
Bruno Cardoso Lopes4713b282009-11-19 06:06:13 +0000512 if (MO.getOffset())
513 O << "+" << MO.getOffset();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000514 break;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000515
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000516 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000517 llvm_unreachable("<unknown operand type>");
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000518 }
519
520 if (closeP) O << ")";
521}
522
Chris Lattner76c564b2010-04-04 04:47:45 +0000523void MipsAsmPrinter::printUnsignedImm(const MachineInstr *MI, int opNum,
524 raw_ostream &O) {
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000525 const MachineOperand &MO = MI->getOperand(opNum);
Devang Patel12f68552010-04-27 22:24:37 +0000526 if (MO.isImm())
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000527 O << (unsigned short int)MO.getImm();
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000528 else
Chris Lattner76c564b2010-04-04 04:47:45 +0000529 printOperand(MI, opNum, O);
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000530}
531
532void MipsAsmPrinter::
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000533printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O) {
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000534 // Load/Store memory operands -- imm($reg)
535 // If PIC target the target is loaded as the
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +0000536 // pattern lw $25,%call16($28)
Chris Lattner76c564b2010-04-04 04:47:45 +0000537 printOperand(MI, opNum+1, O);
Akira Hatanaka2e766ed2011-07-07 18:57:00 +0000538 O << "(";
539 printOperand(MI, opNum, O);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000540 O << ")";
541}
542
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000543void MipsAsmPrinter::
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000544printMemOperandEA(const MachineInstr *MI, int opNum, raw_ostream &O) {
545 // when using stack locations for not load/store instructions
546 // print the same way as all normal 3 operand instructions.
547 printOperand(MI, opNum, O);
548 O << ", ";
549 printOperand(MI, opNum+1, O);
550 return;
551}
552
553void MipsAsmPrinter::
Chris Lattner76c564b2010-04-04 04:47:45 +0000554printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
555 const char *Modifier) {
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000556 const MachineOperand &MO = MI->getOperand(opNum);
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000557 O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000558}
559
Bob Wilsonb633d7a2009-09-30 22:06:26 +0000560void MipsAsmPrinter::EmitStartOfAsmFile(Module &M) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000561 // FIXME: Use SwitchSection.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000562
Jack Carterf9f753c2013-06-18 19:47:15 +0000563 // TODO: Need to add -mabicalls and -mno-abicalls flags.
564 // Currently we assume that -mabicalls is the default.
565 if (OutStreamer.hasRawTextSupport()) {
566 OutStreamer.EmitRawText(StringRef("\t.abicalls"));
567 Reloc::Model RM = Subtarget->getRelocationModel();
Akira Hatanakac3114b32013-06-26 19:08:49 +0000568 if (RM == Reloc::Static && !Subtarget->hasMips64())
Jack Carterf9f753c2013-06-18 19:47:15 +0000569 OutStreamer.EmitRawText(StringRef("\t.option\tpic0"));
570 }
571
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000572 // Tell the assembler which ABI we are using
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000573 if (OutStreamer.hasRawTextSupport())
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000574 OutStreamer.EmitRawText("\t.section .mdebug." +
575 Twine(getCurrentABIString()));
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000576
577 // TODO: handle O64 ABI
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000578 if (OutStreamer.hasRawTextSupport()) {
579 if (Subtarget->isABI_EABI()) {
580 if (Subtarget->isGP32bit())
581 OutStreamer.EmitRawText(StringRef("\t.section .gcc_compiled_long32"));
582 else
583 OutStreamer.EmitRawText(StringRef("\t.section .gcc_compiled_long64"));
584 }
Benjamin Kramer0151d7b2010-04-05 10:17:15 +0000585 }
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000586
587 // return to previous section
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000588 if (OutStreamer.hasRawTextSupport())
589 OutStreamer.EmitRawText(StringRef("\t.previous"));
Jack Carterc1b17ed2013-01-18 21:20:38 +0000590
591}
592
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000593static void emitELFHeaderFlagsCG(MipsTargetStreamer &TargetStreamer,
594 const MipsSubtarget &Subtarget) {
Rafael Espindolaac4ad252013-10-05 16:42:21 +0000595 // Update e_header flags
596 unsigned EFlags = 0;
597
598 // TODO: Need to add -mabicalls and -mno-abicalls flags.
599 // Currently we assume that -mabicalls is the default.
600 EFlags |= ELF::EF_MIPS_CPIC;
601
602 if (Subtarget.inMips16Mode())
603 EFlags |= ELF::EF_MIPS_ARCH_ASE_M16;
604 else
605 EFlags |= ELF::EF_MIPS_NOREORDER;
606
607 // Architecture
608 if (Subtarget.hasMips64r2())
609 EFlags |= ELF::EF_MIPS_ARCH_64R2;
610 else if (Subtarget.hasMips64())
611 EFlags |= ELF::EF_MIPS_ARCH_64;
612 else if (Subtarget.hasMips32r2())
613 EFlags |= ELF::EF_MIPS_ARCH_32R2;
614 else
615 EFlags |= ELF::EF_MIPS_ARCH_32;
616
617 if (Subtarget.inMicroMipsMode())
618 EFlags |= ELF::EF_MIPS_MICROMIPS;
619
620 // ABI
621 if (Subtarget.isABI_O32())
622 EFlags |= ELF::EF_MIPS_ABI_O32;
623
624 // Relocation Model
625 Reloc::Model RM = Subtarget.getRelocationModel();
626 if (RM == Reloc::PIC_ || RM == Reloc::Default)
627 EFlags |= ELF::EF_MIPS_PIC;
628 else if (RM == Reloc::Static)
629 ; // Do nothing for Reloc::Static
630 else
631 llvm_unreachable("Unsupported relocation model for e_flags");
632
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000633 TargetStreamer.emitMipsHackELFFlags(EFlags);
Rafael Espindolaac4ad252013-10-05 16:42:21 +0000634}
635
Jack Carterc1b17ed2013-01-18 21:20:38 +0000636void 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);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000640 emitELFHeaderFlagsCG(getTargetStreamer(), *Subtarget);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000641}
642
Akira Hatanakaf2bcad92011-07-01 01:04:43 +0000643void MipsAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
644 raw_ostream &OS) {
645 // TODO: implement
646}
647
Bob Wilson5a495fe2009-06-23 23:59:40 +0000648// Force static initialization.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000649extern "C" void LLVMInitializeMipsAsmPrinter() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +0000650 RegisterAsmPrinter<MipsAsmPrinter> X(TheMipsTarget);
651 RegisterAsmPrinter<MipsAsmPrinter> Y(TheMipselTarget);
Akira Hatanaka3d673cc2011-09-21 03:00:58 +0000652 RegisterAsmPrinter<MipsAsmPrinter> A(TheMips64Target);
653 RegisterAsmPrinter<MipsAsmPrinter> B(TheMips64elTarget);
Daniel Dunbare8338102009-07-15 20:24:03 +0000654}