blob: b32c2597c5803f3aa91fd93763d73e6b1f545cea [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"
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +000022#include "llvm/ADT/SmallString.h"
23#include "llvm/ADT/StringExtras.h"
24#include "llvm/ADT/Twine.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000025#include "llvm/CodeGen/MachineConstantPool.h"
Bruno Cardoso Lopesbcda5e22007-07-11 23:24:41 +000026#include "llvm/CodeGen/MachineFrameInfo.h"
Jack Carterb2af5122012-07-05 23:58:21 +000027#include "llvm/CodeGen/MachineFunctionPass.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000028#include "llvm/CodeGen/MachineInstr.h"
Akira Hatanaka2fcc1cf2011-08-12 21:30:06 +000029#include "llvm/CodeGen/MachineMemOperand.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/BasicBlock.h"
31#include "llvm/IR/DataLayout.h"
32#include "llvm/IR/InlineAsm.h"
33#include "llvm/IR/Instructions.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000034#include "llvm/MC/MCAsmInfo.h"
Rafael Espindolaac4ad252013-10-05 16:42:21 +000035#include "llvm/MC/MCELFStreamer.h"
Akira Hatanaka9c6028f2011-07-07 23:56:50 +000036#include "llvm/MC/MCInst.h"
Chris Lattner4cd44982009-09-13 17:14:04 +000037#include "llvm/MC/MCSymbol.h"
Jack Carterab3cb422013-02-19 22:04:37 +000038#include "llvm/Support/ELF.h"
Jack Carterb2af5122012-07-05 23:58:21 +000039#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000040#include "llvm/Support/raw_ostream.h"
Chris Lattner0b822ab2010-03-12 21:19:23 +000041#include "llvm/Target/Mangler.h"
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +000042#include "llvm/Target/TargetLoweringObjectFile.h"
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +000043#include "llvm/Target/TargetOptions.h"
Akira Hatanakaf2bcad92011-07-01 01:04:43 +000044
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000045using namespace llvm;
46
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000047bool MipsAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Reed Kotler1595f362013-04-09 19:46:01 +000048 // Initialize TargetLoweringObjectFile.
49 if (Subtarget->allowMixed16_32())
50 const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
51 .Initialize(OutContext, TM);
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000052 MipsFI = MF.getInfo<MipsFunctionInfo>();
53 AsmPrinter::runOnMachineFunction(MF);
54 return true;
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +000055}
56
Akira Hatanaka42a35242012-09-27 01:59:07 +000057bool MipsAsmPrinter::lowerOperand(const MachineOperand &MO, MCOperand &MCOp) {
58 MCOp = MCInstLowering.LowerOperand(MO);
59 return MCOp.isValid();
60}
61
62#include "MipsGenMCPseudoLowering.inc"
63
Akira Hatanakaddd12652011-07-07 20:10:52 +000064void MipsAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Akira Hatanakaddd12652011-07-07 20:10:52 +000065 if (MI->isDebugValue()) {
Bruno Cardoso Lopescd1d4472011-12-30 21:09:41 +000066 SmallString<128> Str;
67 raw_svector_ostream OS(Str);
68
Akira Hatanakaddd12652011-07-07 20:10:52 +000069 PrintDebugValueComment(MI, OS);
70 return;
71 }
72
Akira Hatanaka5ac78682012-06-13 23:25:52 +000073 MachineBasicBlock::const_instr_iterator I = MI;
74 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
75
76 do {
Akira Hatanaka556135d2013-02-06 21:50:15 +000077 // Do any auto-generated pseudo lowerings.
78 if (emitPseudoExpansionLowering(OutStreamer, &*I))
79 continue;
Jack Carterc20a21b2012-08-28 19:07:39 +000080
Reed Kotler76c9bcd2013-02-15 21:05:58 +000081 // The inMips16Mode() test is not permanent.
82 // Some instructions are marked as pseudo right now which
83 // would make the test fail for the wrong reason but
84 // that will be fixed soon. We need this here because we are
85 // removing another test for this situation downstream in the
86 // callchain.
87 //
88 if (I->isPseudo() && !Subtarget->inMips16Mode())
89 llvm_unreachable("Pseudo opcode found in EmitInstruction()");
90
Akira Hatanaka556135d2013-02-06 21:50:15 +000091 MCInst TmpInst0;
92 MCInstLowering.Lower(I, TmpInst0);
Akira Hatanaka5ac78682012-06-13 23:25:52 +000093 OutStreamer.EmitInstruction(TmpInst0);
Akira Hatanaka556135d2013-02-06 21:50:15 +000094 } while ((++I != E) && I->isInsideBundle()); // Delay slot check
Akira Hatanakaddd12652011-07-07 20:10:52 +000095}
96
Akira Hatanakae2489122011-04-15 21:51:11 +000097//===----------------------------------------------------------------------===//
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +000098//
99// Mips Asm Directives
100//
101// -- Frame directive "frame Stackpointer, Stacksize, RARegister"
102// Describe the stack frame.
103//
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000104// -- Mask directives "(f)mask bitmask, offset"
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000105// Tells the assembler which registers are saved and where.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000106// bitmask - contain a little endian bitset indicating which registers are
107// saved on function prologue (e.g. with a 0x80000000 mask, the
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000108// assembler knows the register 31 (RA) is saved at prologue.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000109// offset - the position before stack pointer subtraction indicating where
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000110// the first saved register on prologue is located. (e.g. with a
111//
112// Consider the following function prologue:
113//
Bill Wendling97925ec2008-02-27 06:33:05 +0000114// .frame $fp,48,$ra
115// .mask 0xc0000000,-8
116// addiu $sp, $sp, -48
117// sw $ra, 40($sp)
118// sw $fp, 36($sp)
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000119//
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000120// With a 0xc0000000 mask, the assembler knows the register 31 (RA) and
121// 30 (FP) are saved at prologue. As the save order on prologue is from
122// left to right, RA is saved first. A -8 offset means that after the
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000123// stack pointer subtration, the first register in the mask (RA) will be
124// saved at address 48-8=40.
125//
Akira Hatanakae2489122011-04-15 21:51:11 +0000126//===----------------------------------------------------------------------===//
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000127
Akira Hatanakae2489122011-04-15 21:51:11 +0000128//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000129// Mask directives
Akira Hatanakae2489122011-04-15 21:51:11 +0000130//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000131
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000132// Create a bitmask with all callee saved registers for CPU or Floating Point
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000133// registers. For CPU registers consider RA, GP and FP for saving if necessary.
Chris Lattner76c564b2010-04-04 04:47:45 +0000134void MipsAsmPrinter::printSavedRegsBitmask(raw_ostream &O) {
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000135 // CPU and FPU Saved Registers Bitmasks
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000136 unsigned CPUBitmask = 0, FPUBitmask = 0;
137 int CPUTopSavedRegOff, FPUTopSavedRegOff;
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000138
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000139 // Set the CPU and FPU Bitmasks
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000140 const MachineFrameInfo *MFI = MF->getFrameInfo();
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000141 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000142 // size of stack area to which FP callee-saved regs are saved.
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000143 unsigned CPURegSize = Mips::GPR32RegClass.getSize();
Craig Topperc7242e02012-04-20 07:30:17 +0000144 unsigned FGR32RegSize = Mips::FGR32RegClass.getSize();
145 unsigned AFGR64RegSize = Mips::AFGR64RegClass.getSize();
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000146 bool HasAFGR64Reg = false;
147 unsigned CSFPRegsSize = 0;
148 unsigned i, e = CSI.size();
149
150 // Set FPU Bitmask.
151 for (i = 0; i != e; ++i) {
Rafael Espindolaf2dffce2010-06-02 20:02:30 +0000152 unsigned Reg = CSI[i].getReg();
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000153 if (Mips::GPR32RegClass.contains(Reg))
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000154 break;
155
Akira Hatanaka5d6faed2012-12-10 20:04:40 +0000156 unsigned RegNum = TM.getRegisterInfo()->getEncodingValue(Reg);
Craig Topperc7242e02012-04-20 07:30:17 +0000157 if (Mips::AFGR64RegClass.contains(Reg)) {
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000158 FPUBitmask |= (3 << RegNum);
159 CSFPRegsSize += AFGR64RegSize;
160 HasAFGR64Reg = true;
161 continue;
162 }
163
164 FPUBitmask |= (1 << RegNum);
165 CSFPRegsSize += FGR32RegSize;
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000166 }
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000167
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000168 // Set CPU Bitmask.
169 for (; i != e; ++i) {
170 unsigned Reg = CSI[i].getReg();
Akira Hatanaka5d6faed2012-12-10 20:04:40 +0000171 unsigned RegNum = TM.getRegisterInfo()->getEncodingValue(Reg);
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000172 CPUBitmask |= (1 << RegNum);
173 }
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000174
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000175 // FP Regs are saved right below where the virtual frame pointer points to.
176 FPUTopSavedRegOff = FPUBitmask ?
177 (HasAFGR64Reg ? -AFGR64RegSize : -FGR32RegSize) : 0;
178
179 // CPU Regs are saved below FP Regs.
180 CPUTopSavedRegOff = CPUBitmask ? -CSFPRegsSize - CPURegSize : 0;
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000181
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000182 // Print CPUBitmask
Chris Lattner76c564b2010-04-04 04:47:45 +0000183 O << "\t.mask \t"; printHex32(CPUBitmask, O);
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000184 O << ',' << CPUTopSavedRegOff << '\n';
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000185
186 // Print FPUBitmask
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000187 O << "\t.fmask\t"; printHex32(FPUBitmask, O);
188 O << "," << FPUTopSavedRegOff << '\n';
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000189}
190
191// Print a 32 bit hex number with all numbers.
Chris Lattner76c564b2010-04-04 04:47:45 +0000192void MipsAsmPrinter::printHex32(unsigned Value, raw_ostream &O) {
Owen Anderson93719642008-08-21 00:14:44 +0000193 O << "0x";
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000194 for (int i = 7; i >= 0; i--)
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000195 O.write_hex((Value & (0xF << (i*4))) >> (i*4));
Bruno Cardoso Lopesbcda5e22007-07-11 23:24:41 +0000196}
197
Akira Hatanakae2489122011-04-15 21:51:11 +0000198//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000199// Frame and Set directives
Akira Hatanakae2489122011-04-15 21:51:11 +0000200//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000201
202/// Frame Directive
Chris Lattner5e596182010-04-04 07:05:53 +0000203void MipsAsmPrinter::emitFrameDirective() {
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000204 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
205
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000206 unsigned stackReg = RI.getFrameRegister(*MF);
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000207 unsigned returnReg = RI.getRARegister();
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000208 unsigned stackSize = MF->getFrameInfo()->getStackSize();
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000209
Jia Liuf54f60f2012-02-28 07:46:26 +0000210 if (OutStreamer.hasRawTextSupport())
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000211 OutStreamer.EmitRawText("\t.frame\t$" +
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000212 StringRef(MipsInstPrinter::getRegisterName(stackReg)).lower() +
Akira Hatanaka9c6028f2011-07-07 23:56:50 +0000213 "," + Twine(stackSize) + ",$" +
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000214 StringRef(MipsInstPrinter::getRegisterName(returnReg)).lower());
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000215}
216
217/// Emit Set directives.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000218const char *MipsAsmPrinter::getCurrentABIString() const {
Chris Lattner5e596182010-04-04 07:05:53 +0000219 switch (Subtarget->getTargetABI()) {
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000220 case MipsSubtarget::O32: return "abi32";
Chris Lattner5e596182010-04-04 07:05:53 +0000221 case MipsSubtarget::N32: return "abiN32";
222 case MipsSubtarget::N64: return "abi64";
223 case MipsSubtarget::EABI: return "eabi32"; // TODO: handle eabi64
Dmitri Gribenkoca1e27b2012-09-10 21:26:47 +0000224 default: llvm_unreachable("Unknown Mips ABI");
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000225 }
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000226}
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000227
Chris Lattner5d9fb4b2010-01-27 23:23:58 +0000228void MipsAsmPrinter::EmitFunctionEntryLabel() {
Akira Hatanakaa649cc72012-05-24 18:37:43 +0000229 if (OutStreamer.hasRawTextSupport()) {
230 if (Subtarget->inMips16Mode())
231 OutStreamer.EmitRawText(StringRef("\t.set\tmips16"));
232 else
233 OutStreamer.EmitRawText(StringRef("\t.set\tnomips16"));
Akira Hatanaka5fa54122012-06-13 02:41:14 +0000234 // leave out until FSF available gas has micromips changes
235 // OutStreamer.EmitRawText(StringRef("\t.set\tnomicromips"));
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000236 OutStreamer.EmitRawText("\t.ent\t" + Twine(CurrentFnSym->getName()));
Akira Hatanakaa649cc72012-05-24 18:37:43 +0000237 }
Jack Carterab3cb422013-02-19 22:04:37 +0000238
239 if (Subtarget->inMicroMipsMode())
Rafael Espindolaac4ad252013-10-05 16:42:21 +0000240 OutStreamer.emitMipsHackSTOCG(CurrentFnSym,
241 (unsigned)ELF::STO_MIPS_MICROMIPS);
Chris Lattner5d9fb4b2010-01-27 23:23:58 +0000242 OutStreamer.EmitLabel(CurrentFnSym);
243}
244
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000245/// EmitFunctionBodyStart - Targets can override this to emit stuff before
246/// the first basic block in the function.
247void MipsAsmPrinter::EmitFunctionBodyStart() {
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +0000248 MCInstLowering.Initialize(Mang, &MF->getContext());
249
Reed Kotler0f2b10e2013-05-03 23:17:24 +0000250 bool IsNakedFunction =
251 MF->getFunction()->
252 getAttributes().hasAttribute(AttributeSet::FunctionIndex,
253 Attribute::Naked);
254 if (!IsNakedFunction)
255 emitFrameDirective();
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000256
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000257 if (OutStreamer.hasRawTextSupport()) {
258 SmallString<128> Str;
259 raw_svector_ostream OS(Str);
Reed Kotler0f2b10e2013-05-03 23:17:24 +0000260 if (!IsNakedFunction)
261 printSavedRegsBitmask(OS);
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000262 OutStreamer.EmitRawText(OS.str());
Reed Kotlerf0221472013-02-15 01:04:38 +0000263 if (!Subtarget->inMips16Mode()) {
264 OutStreamer.EmitRawText(StringRef("\t.set\tnoreorder"));
265 OutStreamer.EmitRawText(StringRef("\t.set\tnomacro"));
266 OutStreamer.EmitRawText(StringRef("\t.set\tnoat"));
267 }
Akira Hatanaka8f3573032012-05-12 00:48:43 +0000268 }
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000269}
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000270
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000271/// EmitFunctionBodyEnd - Targets can override this to emit stuff after
272/// the last basic block in the function.
273void MipsAsmPrinter::EmitFunctionBodyEnd() {
Chris Lattnerfd97a332010-01-28 01:48:52 +0000274 // There are instruction for this macros, but they must
275 // always be at the function end, and we can't emit and
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000276 // break with BB logic.
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000277 if (OutStreamer.hasRawTextSupport()) {
Reed Kotlerf0221472013-02-15 01:04:38 +0000278 if (!Subtarget->inMips16Mode()) {
279 OutStreamer.EmitRawText(StringRef("\t.set\tat"));
280 OutStreamer.EmitRawText(StringRef("\t.set\tmacro"));
281 OutStreamer.EmitRawText(StringRef("\t.set\treorder"));
282 }
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000283 OutStreamer.EmitRawText("\t.end\t" + Twine(CurrentFnSym->getName()));
284 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000285}
286
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000287/// isBlockOnlyReachableByFallthough - Return true if the basic block has
288/// exactly one predecessor and the control transfer mechanism between
289/// the predecessor and this block is a fall-through.
Akira Hatanakae2489122011-04-15 21:51:11 +0000290bool MipsAsmPrinter::isBlockOnlyReachableByFallthrough(const MachineBasicBlock*
291 MBB) const {
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000292 // The predecessor has to be immediately before this block.
293 const MachineBasicBlock *Pred = *MBB->pred_begin();
294
295 // If the predecessor is a switch statement, assume a jump table
296 // implementation, so it is not a fall through.
297 if (const BasicBlock *bb = Pred->getBasicBlock())
298 if (isa<SwitchInst>(bb->getTerminator()))
299 return false;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000300
Akira Hatanakae625ba42011-04-01 18:57:38 +0000301 // If this is a landing pad, it isn't a fall through. If it has no preds,
302 // then nothing falls through to it.
303 if (MBB->isLandingPad() || MBB->pred_empty())
304 return false;
305
306 // If there isn't exactly one predecessor, it can't be a fall through.
307 MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI;
308 ++PI2;
Jia Liuf54f60f2012-02-28 07:46:26 +0000309
Akira Hatanakae625ba42011-04-01 18:57:38 +0000310 if (PI2 != MBB->pred_end())
Jia Liuf54f60f2012-02-28 07:46:26 +0000311 return false;
Akira Hatanakae625ba42011-04-01 18:57:38 +0000312
313 // The predecessor has to be immediately before this block.
314 if (!Pred->isLayoutSuccessor(MBB))
315 return false;
Jia Liuf54f60f2012-02-28 07:46:26 +0000316
Akira Hatanakae625ba42011-04-01 18:57:38 +0000317 // If the block is completely empty, then it definitely does fall through.
318 if (Pred->empty())
319 return true;
Jia Liuf54f60f2012-02-28 07:46:26 +0000320
Akira Hatanakae625ba42011-04-01 18:57:38 +0000321 // Otherwise, check the last instruction.
322 // Check if the last terminator is an unconditional branch.
323 MachineBasicBlock::const_iterator I = Pred->end();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000324 while (I != Pred->begin() && !(--I)->isTerminator()) ;
Akira Hatanakae625ba42011-04-01 18:57:38 +0000325
Evan Cheng7f8e5632011-12-07 07:15:52 +0000326 return !I->isBarrier();
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000327}
328
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000329// Print out an operand for an inline asm expression.
Eric Christophered51b9e2012-05-10 21:48:22 +0000330bool MipsAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Chris Lattner3bb09762010-04-04 05:29:35 +0000331 unsigned AsmVariant,const char *ExtraCode,
332 raw_ostream &O) {
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000333 // Does this asm operand have a single letter operand modifier?
Eric Christophered51b9e2012-05-10 21:48:22 +0000334 if (ExtraCode && ExtraCode[0]) {
335 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000336
Eric Christophered51b9e2012-05-10 21:48:22 +0000337 const MachineOperand &MO = MI->getOperand(OpNum);
338 switch (ExtraCode[0]) {
Eric Christopherbc5d2492012-05-19 00:51:56 +0000339 default:
Jack Carterb2fd5f62012-06-21 17:14:46 +0000340 // See if this is a generic print operand
341 return AsmPrinter::PrintAsmOperand(MI,OpNum,AsmVariant,ExtraCode,O);
Eric Christopherbc5d2492012-05-19 00:51:56 +0000342 case 'X': // hex const int
343 if ((MO.getType()) != MachineOperand::MO_Immediate)
344 return true;
345 O << "0x" << StringRef(utohexstr(MO.getImm())).lower();
346 return false;
347 case 'x': // hex const int (low 16 bits)
348 if ((MO.getType()) != MachineOperand::MO_Immediate)
349 return true;
350 O << "0x" << StringRef(utohexstr(MO.getImm() & 0xffff)).lower();
351 return false;
352 case 'd': // decimal const int
353 if ((MO.getType()) != MachineOperand::MO_Immediate)
354 return true;
355 O << MO.getImm();
356 return false;
Eric Christopherf481ab32012-05-30 19:05:19 +0000357 case 'm': // decimal const int minus 1
358 if ((MO.getType()) != MachineOperand::MO_Immediate)
359 return true;
360 O << MO.getImm() - 1;
361 return false;
Jack Carter27747b52012-06-28 20:46:26 +0000362 case 'z': {
363 // $0 if zero, regular printing otherwise
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000364 if (MO.getType() != MachineOperand::MO_Immediate)
365 return true;
366 int64_t Val = MO.getImm();
367 if (Val)
368 O << Val;
369 else
370 O << "$0";
371 return false;
372 }
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000373 case 'D': // Second part of a double word register operand
374 case 'L': // Low order register of a double word register operand
Jack Cartera62ba822012-07-18 06:41:36 +0000375 case 'M': // High order register of a double word register operand
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000376 {
Jack Carterb2af5122012-07-05 23:58:21 +0000377 if (OpNum == 0)
378 return true;
379 const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
380 if (!FlagsOP.isImm())
381 return true;
382 unsigned Flags = FlagsOP.getImm();
383 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Jack Carter2ab73b12012-07-06 02:44:22 +0000384 // Number of registers represented by this operand. We are looking
385 // for 2 for 32 bit mode and 1 for 64 bit mode.
Jack Carterb2af5122012-07-05 23:58:21 +0000386 if (NumVals != 2) {
Jack Carter2ab73b12012-07-06 02:44:22 +0000387 if (Subtarget->isGP64bit() && NumVals == 1 && MO.isReg()) {
Jack Carterb2af5122012-07-05 23:58:21 +0000388 unsigned Reg = MO.getReg();
389 O << '$' << MipsInstPrinter::getRegisterName(Reg);
390 return false;
391 }
392 return true;
393 }
Jack Carter42ebf982012-07-11 21:41:49 +0000394
395 unsigned RegOp = OpNum;
396 if (!Subtarget->isGP64bit()){
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000397 // Endianess reverses which register holds the high or low value
Jack Cartera62ba822012-07-18 06:41:36 +0000398 // between M and L.
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000399 switch(ExtraCode[0]) {
Jack Cartera62ba822012-07-18 06:41:36 +0000400 case 'M':
401 RegOp = (Subtarget->isLittle()) ? OpNum + 1 : OpNum;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000402 break;
403 case 'L':
Jack Cartera62ba822012-07-18 06:41:36 +0000404 RegOp = (Subtarget->isLittle()) ? OpNum : OpNum + 1;
405 break;
406 case 'D': // Always the second part
407 RegOp = OpNum + 1;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000408 }
409 if (RegOp >= MI->getNumOperands())
410 return true;
411 const MachineOperand &MO = MI->getOperand(RegOp);
412 if (!MO.isReg())
413 return true;
414 unsigned Reg = MO.getReg();
415 O << '$' << MipsInstPrinter::getRegisterName(Reg);
416 return false;
Jack Carterb2af5122012-07-05 23:58:21 +0000417 }
Eric Christophered51b9e2012-05-10 21:48:22 +0000418 }
Jack Carter2ab73b12012-07-06 02:44:22 +0000419 }
420 }
Eric Christophered51b9e2012-05-10 21:48:22 +0000421
422 printOperand(MI, OpNum, O);
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000423 return false;
424}
425
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000426bool MipsAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
427 unsigned OpNum, unsigned AsmVariant,
428 const char *ExtraCode,
429 raw_ostream &O) {
Jack Carterb04e3572013-04-09 23:19:50 +0000430 int Offset = 0;
431 // Currently we are expecting either no ExtraCode or 'D'
432 if (ExtraCode) {
433 if (ExtraCode[0] == 'D')
434 Offset = 4;
435 else
436 return true; // Unknown modifier.
437 }
Jia Liuf54f60f2012-02-28 07:46:26 +0000438
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000439 const MachineOperand &MO = MI->getOperand(OpNum);
440 assert(MO.isReg() && "unexpected inline asm memory operand");
Jack Carterb04e3572013-04-09 23:19:50 +0000441 O << Offset << "($" << MipsInstPrinter::getRegisterName(MO.getReg()) << ")";
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000442
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000443 return false;
444}
445
Chris Lattner76c564b2010-04-04 04:47:45 +0000446void MipsAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
447 raw_ostream &O) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000448 const MachineOperand &MO = MI->getOperand(opNum);
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +0000449 bool closeP = false;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000450
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000451 if (MO.getTargetFlags())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000452 closeP = true;
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000453
454 switch(MO.getTargetFlags()) {
455 case MipsII::MO_GPREL: O << "%gp_rel("; break;
456 case MipsII::MO_GOT_CALL: O << "%call16("; break;
Akira Hatanaka56d9ef52011-04-01 21:41:06 +0000457 case MipsII::MO_GOT: O << "%got("; break;
458 case MipsII::MO_ABS_HI: O << "%hi("; break;
459 case MipsII::MO_ABS_LO: O << "%lo("; break;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +0000460 case MipsII::MO_TLSGD: O << "%tlsgd("; break;
461 case MipsII::MO_GOTTPREL: O << "%gottprel("; break;
462 case MipsII::MO_TPREL_HI: O << "%tprel_hi("; break;
463 case MipsII::MO_TPREL_LO: O << "%tprel_lo("; break;
Akira Hatanaka25ce3642011-09-22 03:09:07 +0000464 case MipsII::MO_GPOFF_HI: O << "%hi(%neg(%gp_rel("; break;
465 case MipsII::MO_GPOFF_LO: O << "%lo(%neg(%gp_rel("; break;
466 case MipsII::MO_GOT_DISP: O << "%got_disp("; break;
467 case MipsII::MO_GOT_PAGE: O << "%got_page("; break;
468 case MipsII::MO_GOT_OFST: O << "%got_ofst("; break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000469 }
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000470
Chris Lattnereb2cc682009-09-13 20:31:40 +0000471 switch (MO.getType()) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000472 case MachineOperand::MO_Register:
Akira Hatanaka9c6028f2011-07-07 23:56:50 +0000473 O << '$'
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000474 << StringRef(MipsInstPrinter::getRegisterName(MO.getReg())).lower();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000475 break;
476
477 case MachineOperand::MO_Immediate:
Akira Hatanaka2db176c2011-05-24 21:22:21 +0000478 O << MO.getImm();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000479 break;
480
481 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner29bdac42010-03-13 21:04:28 +0000482 O << *MO.getMBB()->getSymbol();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000483 return;
484
485 case MachineOperand::MO_GlobalAddress:
Chris Lattner0b822ab2010-03-12 21:19:23 +0000486 O << *Mang->getSymbol(MO.getGlobal());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000487 break;
488
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000489 case MachineOperand::MO_BlockAddress: {
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000490 MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000491 O << BA->getName();
492 break;
493 }
494
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000495 case MachineOperand::MO_ExternalSymbol:
Chris Lattner8b5d55e2010-01-17 21:43:43 +0000496 O << *GetExternalSymbolSymbol(MO.getSymbolName());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000497 break;
498
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +0000499 case MachineOperand::MO_JumpTableIndex:
Chris Lattnere9a75a62009-08-22 21:43:10 +0000500 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner719e9082010-01-16 00:21:18 +0000501 << '_' << MO.getIndex();
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +0000502 break;
503
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000504 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnere9a75a62009-08-22 21:43:10 +0000505 O << MAI->getPrivateGlobalPrefix() << "CPI"
Chris Lattnera5bb3702007-12-30 23:10:15 +0000506 << getFunctionNumber() << "_" << MO.getIndex();
Bruno Cardoso Lopes4713b282009-11-19 06:06:13 +0000507 if (MO.getOffset())
508 O << "+" << MO.getOffset();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000509 break;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000510
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000511 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000512 llvm_unreachable("<unknown operand type>");
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000513 }
514
515 if (closeP) O << ")";
516}
517
Chris Lattner76c564b2010-04-04 04:47:45 +0000518void MipsAsmPrinter::printUnsignedImm(const MachineInstr *MI, int opNum,
519 raw_ostream &O) {
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000520 const MachineOperand &MO = MI->getOperand(opNum);
Devang Patel12f68552010-04-27 22:24:37 +0000521 if (MO.isImm())
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000522 O << (unsigned short int)MO.getImm();
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000523 else
Chris Lattner76c564b2010-04-04 04:47:45 +0000524 printOperand(MI, opNum, O);
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000525}
526
527void MipsAsmPrinter::
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000528printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O) {
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000529 // Load/Store memory operands -- imm($reg)
530 // If PIC target the target is loaded as the
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +0000531 // pattern lw $25,%call16($28)
Chris Lattner76c564b2010-04-04 04:47:45 +0000532 printOperand(MI, opNum+1, O);
Akira Hatanaka2e766ed2011-07-07 18:57:00 +0000533 O << "(";
534 printOperand(MI, opNum, O);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000535 O << ")";
536}
537
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000538void MipsAsmPrinter::
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000539printMemOperandEA(const MachineInstr *MI, int opNum, raw_ostream &O) {
540 // when using stack locations for not load/store instructions
541 // print the same way as all normal 3 operand instructions.
542 printOperand(MI, opNum, O);
543 O << ", ";
544 printOperand(MI, opNum+1, O);
545 return;
546}
547
548void MipsAsmPrinter::
Chris Lattner76c564b2010-04-04 04:47:45 +0000549printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
550 const char *Modifier) {
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000551 const MachineOperand &MO = MI->getOperand(opNum);
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000552 O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000553}
554
Bob Wilsonb633d7a2009-09-30 22:06:26 +0000555void MipsAsmPrinter::EmitStartOfAsmFile(Module &M) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000556 // FIXME: Use SwitchSection.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000557
Jack Carterf9f753c2013-06-18 19:47:15 +0000558 // TODO: Need to add -mabicalls and -mno-abicalls flags.
559 // Currently we assume that -mabicalls is the default.
560 if (OutStreamer.hasRawTextSupport()) {
561 OutStreamer.EmitRawText(StringRef("\t.abicalls"));
562 Reloc::Model RM = Subtarget->getRelocationModel();
Akira Hatanakac3114b32013-06-26 19:08:49 +0000563 if (RM == Reloc::Static && !Subtarget->hasMips64())
Jack Carterf9f753c2013-06-18 19:47:15 +0000564 OutStreamer.EmitRawText(StringRef("\t.option\tpic0"));
565 }
566
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000567 // Tell the assembler which ABI we are using
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000568 if (OutStreamer.hasRawTextSupport())
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000569 OutStreamer.EmitRawText("\t.section .mdebug." +
570 Twine(getCurrentABIString()));
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000571
572 // TODO: handle O64 ABI
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000573 if (OutStreamer.hasRawTextSupport()) {
574 if (Subtarget->isABI_EABI()) {
575 if (Subtarget->isGP32bit())
576 OutStreamer.EmitRawText(StringRef("\t.section .gcc_compiled_long32"));
577 else
578 OutStreamer.EmitRawText(StringRef("\t.section .gcc_compiled_long64"));
579 }
Benjamin Kramer0151d7b2010-04-05 10:17:15 +0000580 }
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000581
582 // return to previous section
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000583 if (OutStreamer.hasRawTextSupport())
584 OutStreamer.EmitRawText(StringRef("\t.previous"));
Jack Carterc1b17ed2013-01-18 21:20:38 +0000585
586}
587
Rafael Espindolaac4ad252013-10-05 16:42:21 +0000588static void
589emitELFHeaderFlagsCG(MCStreamer &Streamer, const MipsSubtarget &Subtarget) {
590 // Update e_header flags
591 unsigned EFlags = 0;
592
593 // TODO: Need to add -mabicalls and -mno-abicalls flags.
594 // Currently we assume that -mabicalls is the default.
595 EFlags |= ELF::EF_MIPS_CPIC;
596
597 if (Subtarget.inMips16Mode())
598 EFlags |= ELF::EF_MIPS_ARCH_ASE_M16;
599 else
600 EFlags |= ELF::EF_MIPS_NOREORDER;
601
602 // Architecture
603 if (Subtarget.hasMips64r2())
604 EFlags |= ELF::EF_MIPS_ARCH_64R2;
605 else if (Subtarget.hasMips64())
606 EFlags |= ELF::EF_MIPS_ARCH_64;
607 else if (Subtarget.hasMips32r2())
608 EFlags |= ELF::EF_MIPS_ARCH_32R2;
609 else
610 EFlags |= ELF::EF_MIPS_ARCH_32;
611
612 if (Subtarget.inMicroMipsMode())
613 EFlags |= ELF::EF_MIPS_MICROMIPS;
614
615 // ABI
616 if (Subtarget.isABI_O32())
617 EFlags |= ELF::EF_MIPS_ABI_O32;
618
619 // Relocation Model
620 Reloc::Model RM = Subtarget.getRelocationModel();
621 if (RM == Reloc::PIC_ || RM == Reloc::Default)
622 EFlags |= ELF::EF_MIPS_PIC;
623 else if (RM == Reloc::Static)
624 ; // Do nothing for Reloc::Static
625 else
626 llvm_unreachable("Unsupported relocation model for e_flags");
627
628 Streamer.emitMipsHackELFFlags(EFlags);
629}
630
Jack Carterc1b17ed2013-01-18 21:20:38 +0000631void MipsAsmPrinter::EmitEndOfAsmFile(Module &M) {
Jack Carterc1b17ed2013-01-18 21:20:38 +0000632 // Emit Mips ELF register info
633 Subtarget->getMReginfo().emitMipsReginfoSectionCG(
634 OutStreamer, getObjFileLowering(), *Subtarget);
Rafael Espindolaac4ad252013-10-05 16:42:21 +0000635 emitELFHeaderFlagsCG(OutStreamer, *Subtarget);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000636}
637
Akira Hatanakaf2bcad92011-07-01 01:04:43 +0000638void MipsAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
639 raw_ostream &OS) {
640 // TODO: implement
641}
642
Bob Wilson5a495fe2009-06-23 23:59:40 +0000643// Force static initialization.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000644extern "C" void LLVMInitializeMipsAsmPrinter() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +0000645 RegisterAsmPrinter<MipsAsmPrinter> X(TheMipsTarget);
646 RegisterAsmPrinter<MipsAsmPrinter> Y(TheMipselTarget);
Akira Hatanaka3d673cc2011-09-21 03:00:58 +0000647 RegisterAsmPrinter<MipsAsmPrinter> A(TheMips64Target);
648 RegisterAsmPrinter<MipsAsmPrinter> B(TheMips64elTarget);
Daniel Dunbare8338102009-07-15 20:24:03 +0000649}