blob: 40a30293738e9bb5c0176448aae535136f1049a1 [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.
Rafael Espindola25fa2912014-01-27 04:33:11 +0000175void MipsAsmPrinter::printSavedRegsBitmask() {
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
Rafael Espindola25fa2912014-01-27 04:33:11 +0000223 MipsTargetStreamer &TS = getTargetStreamer();
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000224 // Print CPUBitmask
Rafael Espindola25fa2912014-01-27 04:33:11 +0000225 TS.emitMask(CPUBitmask, CPUTopSavedRegOff);
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000226
227 // Print FPUBitmask
Rafael Espindola25fa2912014-01-27 04:33:11 +0000228 TS.emitFMask(FPUBitmask, FPUTopSavedRegOff);
Bruno Cardoso Lopesbcda5e22007-07-11 23:24:41 +0000229}
230
Akira Hatanakae2489122011-04-15 21:51:11 +0000231//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000232// Frame and Set directives
Akira Hatanakae2489122011-04-15 21:51:11 +0000233//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000234
235/// Frame Directive
Chris Lattner5e596182010-04-04 07:05:53 +0000236void MipsAsmPrinter::emitFrameDirective() {
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000237 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
238
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000239 unsigned stackReg = RI.getFrameRegister(*MF);
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000240 unsigned returnReg = RI.getRARegister();
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000241 unsigned stackSize = MF->getFrameInfo()->getStackSize();
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000242
Rafael Espindola054234f2014-01-27 03:53:56 +0000243 getTargetStreamer().emitFrame(stackReg, stackSize, returnReg);
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000244}
245
246/// Emit Set directives.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000247const char *MipsAsmPrinter::getCurrentABIString() const {
Chris Lattner5e596182010-04-04 07:05:53 +0000248 switch (Subtarget->getTargetABI()) {
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000249 case MipsSubtarget::O32: return "abi32";
Chris Lattner5e596182010-04-04 07:05:53 +0000250 case MipsSubtarget::N32: return "abiN32";
251 case MipsSubtarget::N64: return "abi64";
252 case MipsSubtarget::EABI: return "eabi32"; // TODO: handle eabi64
Dmitri Gribenkoca1e27b2012-09-10 21:26:47 +0000253 default: llvm_unreachable("Unknown Mips ABI");
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000254 }
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000255}
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000256
Chris Lattner5d9fb4b2010-01-27 23:23:58 +0000257void MipsAsmPrinter::EmitFunctionEntryLabel() {
Rafael Espindola6633d572014-01-14 18:57:12 +0000258 MipsTargetStreamer &TS = getTargetStreamer();
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000259 if (Subtarget->inMicroMipsMode())
Rafael Espindola6633d572014-01-14 18:57:12 +0000260 TS.emitDirectiveSetMicroMips();
261 // leave out until FSF available gas has micromips changes
262 // else
263 // TS.emitDirectiveSetNoMicroMips();
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000264
Rafael Espindola6633d572014-01-14 18:57:12 +0000265 if (Subtarget->inMips16Mode())
266 TS.emitDirectiveSetMips16();
267 else
268 TS.emitDirectiveSetNoMips16();
Jack Carterab3cb422013-02-19 22:04:37 +0000269
Rafael Espindola6633d572014-01-14 18:57:12 +0000270 TS.emitDirectiveEnt(*CurrentFnSym);
Chris Lattner5d9fb4b2010-01-27 23:23:58 +0000271 OutStreamer.EmitLabel(CurrentFnSym);
272}
273
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000274/// EmitFunctionBodyStart - Targets can override this to emit stuff before
275/// the first basic block in the function.
276void MipsAsmPrinter::EmitFunctionBodyStart() {
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000277 MipsTargetStreamer &TS = getTargetStreamer();
278
Rafael Espindola7d78b2a2013-10-29 16:24:21 +0000279 MCInstLowering.Initialize(&MF->getContext());
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +0000280
Reed Kotler0f2b10e2013-05-03 23:17:24 +0000281 bool IsNakedFunction =
282 MF->getFunction()->
283 getAttributes().hasAttribute(AttributeSet::FunctionIndex,
284 Attribute::Naked);
285 if (!IsNakedFunction)
286 emitFrameDirective();
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000287
Rafael Espindola25fa2912014-01-27 04:33:11 +0000288 if (!IsNakedFunction)
289 printSavedRegsBitmask();
290
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000291 if (!Subtarget->inMips16Mode()) {
292 TS.emitDirectiveSetNoReorder();
293 TS.emitDirectiveSetNoMacro();
294 TS.emitDirectiveSetNoAt();
Akira Hatanaka8f3573032012-05-12 00:48:43 +0000295 }
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000296}
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000297
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000298/// EmitFunctionBodyEnd - Targets can override this to emit stuff after
299/// the last basic block in the function.
300void MipsAsmPrinter::EmitFunctionBodyEnd() {
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000301 MipsTargetStreamer &TS = getTargetStreamer();
302
Chris Lattnerfd97a332010-01-28 01:48:52 +0000303 // There are instruction for this macros, but they must
304 // always be at the function end, and we can't emit and
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000305 // break with BB logic.
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000306 if (!Subtarget->inMips16Mode()) {
307 TS.emitDirectiveSetAt();
308 TS.emitDirectiveSetMacro();
309 TS.emitDirectiveSetReorder();
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000310 }
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000311 TS.emitDirectiveEnd(CurrentFnSym->getName());
Reed Kotler91ae9822013-10-27 21:57:36 +0000312 // Make sure to terminate any constant pools that were at the end
313 // of the function.
314 if (!InConstantPool)
315 return;
316 InConstantPool = false;
317 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000318}
319
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000320/// isBlockOnlyReachableByFallthough - Return true if the basic block has
321/// exactly one predecessor and the control transfer mechanism between
322/// the predecessor and this block is a fall-through.
Akira Hatanakae2489122011-04-15 21:51:11 +0000323bool MipsAsmPrinter::isBlockOnlyReachableByFallthrough(const MachineBasicBlock*
324 MBB) const {
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000325 // The predecessor has to be immediately before this block.
326 const MachineBasicBlock *Pred = *MBB->pred_begin();
327
328 // If the predecessor is a switch statement, assume a jump table
329 // implementation, so it is not a fall through.
330 if (const BasicBlock *bb = Pred->getBasicBlock())
331 if (isa<SwitchInst>(bb->getTerminator()))
332 return false;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000333
Akira Hatanakae625ba42011-04-01 18:57:38 +0000334 // If this is a landing pad, it isn't a fall through. If it has no preds,
335 // then nothing falls through to it.
336 if (MBB->isLandingPad() || MBB->pred_empty())
337 return false;
338
339 // If there isn't exactly one predecessor, it can't be a fall through.
340 MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI;
341 ++PI2;
Jia Liuf54f60f2012-02-28 07:46:26 +0000342
Akira Hatanakae625ba42011-04-01 18:57:38 +0000343 if (PI2 != MBB->pred_end())
Jia Liuf54f60f2012-02-28 07:46:26 +0000344 return false;
Akira Hatanakae625ba42011-04-01 18:57:38 +0000345
346 // The predecessor has to be immediately before this block.
347 if (!Pred->isLayoutSuccessor(MBB))
348 return false;
Jia Liuf54f60f2012-02-28 07:46:26 +0000349
Akira Hatanakae625ba42011-04-01 18:57:38 +0000350 // If the block is completely empty, then it definitely does fall through.
351 if (Pred->empty())
352 return true;
Jia Liuf54f60f2012-02-28 07:46:26 +0000353
Akira Hatanakae625ba42011-04-01 18:57:38 +0000354 // Otherwise, check the last instruction.
355 // Check if the last terminator is an unconditional branch.
356 MachineBasicBlock::const_iterator I = Pred->end();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000357 while (I != Pred->begin() && !(--I)->isTerminator()) ;
Akira Hatanakae625ba42011-04-01 18:57:38 +0000358
Evan Cheng7f8e5632011-12-07 07:15:52 +0000359 return !I->isBarrier();
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000360}
361
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000362// Print out an operand for an inline asm expression.
Eric Christophered51b9e2012-05-10 21:48:22 +0000363bool MipsAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Chris Lattner3bb09762010-04-04 05:29:35 +0000364 unsigned AsmVariant,const char *ExtraCode,
365 raw_ostream &O) {
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000366 // Does this asm operand have a single letter operand modifier?
Eric Christophered51b9e2012-05-10 21:48:22 +0000367 if (ExtraCode && ExtraCode[0]) {
368 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000369
Eric Christophered51b9e2012-05-10 21:48:22 +0000370 const MachineOperand &MO = MI->getOperand(OpNum);
371 switch (ExtraCode[0]) {
Eric Christopherbc5d2492012-05-19 00:51:56 +0000372 default:
Jack Carterb2fd5f62012-06-21 17:14:46 +0000373 // See if this is a generic print operand
374 return AsmPrinter::PrintAsmOperand(MI,OpNum,AsmVariant,ExtraCode,O);
Eric Christopherbc5d2492012-05-19 00:51:56 +0000375 case 'X': // hex const int
376 if ((MO.getType()) != MachineOperand::MO_Immediate)
377 return true;
378 O << "0x" << StringRef(utohexstr(MO.getImm())).lower();
379 return false;
380 case 'x': // hex const int (low 16 bits)
381 if ((MO.getType()) != MachineOperand::MO_Immediate)
382 return true;
383 O << "0x" << StringRef(utohexstr(MO.getImm() & 0xffff)).lower();
384 return false;
385 case 'd': // decimal const int
386 if ((MO.getType()) != MachineOperand::MO_Immediate)
387 return true;
388 O << MO.getImm();
389 return false;
Eric Christopherf481ab32012-05-30 19:05:19 +0000390 case 'm': // decimal const int minus 1
391 if ((MO.getType()) != MachineOperand::MO_Immediate)
392 return true;
393 O << MO.getImm() - 1;
394 return false;
Jack Carter27747b52012-06-28 20:46:26 +0000395 case 'z': {
396 // $0 if zero, regular printing otherwise
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000397 if (MO.getType() != MachineOperand::MO_Immediate)
398 return true;
399 int64_t Val = MO.getImm();
400 if (Val)
401 O << Val;
402 else
403 O << "$0";
404 return false;
405 }
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000406 case 'D': // Second part of a double word register operand
407 case 'L': // Low order register of a double word register operand
Jack Cartera62ba822012-07-18 06:41:36 +0000408 case 'M': // High order register of a double word register operand
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000409 {
Jack Carterb2af5122012-07-05 23:58:21 +0000410 if (OpNum == 0)
411 return true;
412 const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
413 if (!FlagsOP.isImm())
414 return true;
415 unsigned Flags = FlagsOP.getImm();
416 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Jack Carter2ab73b12012-07-06 02:44:22 +0000417 // Number of registers represented by this operand. We are looking
418 // for 2 for 32 bit mode and 1 for 64 bit mode.
Jack Carterb2af5122012-07-05 23:58:21 +0000419 if (NumVals != 2) {
Jack Carter2ab73b12012-07-06 02:44:22 +0000420 if (Subtarget->isGP64bit() && NumVals == 1 && MO.isReg()) {
Jack Carterb2af5122012-07-05 23:58:21 +0000421 unsigned Reg = MO.getReg();
422 O << '$' << MipsInstPrinter::getRegisterName(Reg);
423 return false;
424 }
425 return true;
426 }
Jack Carter42ebf982012-07-11 21:41:49 +0000427
428 unsigned RegOp = OpNum;
429 if (!Subtarget->isGP64bit()){
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000430 // Endianess reverses which register holds the high or low value
Jack Cartera62ba822012-07-18 06:41:36 +0000431 // between M and L.
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000432 switch(ExtraCode[0]) {
Jack Cartera62ba822012-07-18 06:41:36 +0000433 case 'M':
434 RegOp = (Subtarget->isLittle()) ? OpNum + 1 : OpNum;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000435 break;
436 case 'L':
Jack Cartera62ba822012-07-18 06:41:36 +0000437 RegOp = (Subtarget->isLittle()) ? OpNum : OpNum + 1;
438 break;
439 case 'D': // Always the second part
440 RegOp = OpNum + 1;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000441 }
442 if (RegOp >= MI->getNumOperands())
443 return true;
444 const MachineOperand &MO = MI->getOperand(RegOp);
445 if (!MO.isReg())
446 return true;
447 unsigned Reg = MO.getReg();
448 O << '$' << MipsInstPrinter::getRegisterName(Reg);
449 return false;
Jack Carterb2af5122012-07-05 23:58:21 +0000450 }
Eric Christophered51b9e2012-05-10 21:48:22 +0000451 }
Daniel Sanders8b59af12013-11-12 12:56:01 +0000452 case 'w':
453 // Print MSA registers for the 'f' constraint
454 // In LLVM, the 'w' modifier doesn't need to do anything.
455 // We can just call printOperand as normal.
456 break;
Jack Carter2ab73b12012-07-06 02:44:22 +0000457 }
458 }
Eric Christophered51b9e2012-05-10 21:48:22 +0000459
460 printOperand(MI, OpNum, O);
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000461 return false;
462}
463
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000464bool MipsAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
465 unsigned OpNum, unsigned AsmVariant,
466 const char *ExtraCode,
467 raw_ostream &O) {
Jack Carterb04e3572013-04-09 23:19:50 +0000468 int Offset = 0;
469 // Currently we are expecting either no ExtraCode or 'D'
470 if (ExtraCode) {
471 if (ExtraCode[0] == 'D')
472 Offset = 4;
473 else
474 return true; // Unknown modifier.
475 }
Jia Liuf54f60f2012-02-28 07:46:26 +0000476
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000477 const MachineOperand &MO = MI->getOperand(OpNum);
478 assert(MO.isReg() && "unexpected inline asm memory operand");
Jack Carterb04e3572013-04-09 23:19:50 +0000479 O << Offset << "($" << MipsInstPrinter::getRegisterName(MO.getReg()) << ")";
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000480
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000481 return false;
482}
483
Chris Lattner76c564b2010-04-04 04:47:45 +0000484void MipsAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
485 raw_ostream &O) {
Rafael Espindola58873562014-01-03 19:21:54 +0000486 const DataLayout *DL = TM.getDataLayout();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000487 const MachineOperand &MO = MI->getOperand(opNum);
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +0000488 bool closeP = false;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000489
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000490 if (MO.getTargetFlags())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000491 closeP = true;
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000492
493 switch(MO.getTargetFlags()) {
494 case MipsII::MO_GPREL: O << "%gp_rel("; break;
495 case MipsII::MO_GOT_CALL: O << "%call16("; break;
Akira Hatanaka56d9ef52011-04-01 21:41:06 +0000496 case MipsII::MO_GOT: O << "%got("; break;
497 case MipsII::MO_ABS_HI: O << "%hi("; break;
498 case MipsII::MO_ABS_LO: O << "%lo("; break;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +0000499 case MipsII::MO_TLSGD: O << "%tlsgd("; break;
500 case MipsII::MO_GOTTPREL: O << "%gottprel("; break;
501 case MipsII::MO_TPREL_HI: O << "%tprel_hi("; break;
502 case MipsII::MO_TPREL_LO: O << "%tprel_lo("; break;
Akira Hatanaka25ce3642011-09-22 03:09:07 +0000503 case MipsII::MO_GPOFF_HI: O << "%hi(%neg(%gp_rel("; break;
504 case MipsII::MO_GPOFF_LO: O << "%lo(%neg(%gp_rel("; break;
505 case MipsII::MO_GOT_DISP: O << "%got_disp("; break;
506 case MipsII::MO_GOT_PAGE: O << "%got_page("; break;
507 case MipsII::MO_GOT_OFST: O << "%got_ofst("; break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000508 }
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000509
Chris Lattnereb2cc682009-09-13 20:31:40 +0000510 switch (MO.getType()) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000511 case MachineOperand::MO_Register:
Akira Hatanaka9c6028f2011-07-07 23:56:50 +0000512 O << '$'
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000513 << StringRef(MipsInstPrinter::getRegisterName(MO.getReg())).lower();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000514 break;
515
516 case MachineOperand::MO_Immediate:
Akira Hatanaka2db176c2011-05-24 21:22:21 +0000517 O << MO.getImm();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000518 break;
519
520 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner29bdac42010-03-13 21:04:28 +0000521 O << *MO.getMBB()->getSymbol();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000522 return;
523
524 case MachineOperand::MO_GlobalAddress:
Rafael Espindola79858aa2013-10-29 17:07:16 +0000525 O << *getSymbol(MO.getGlobal());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000526 break;
527
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000528 case MachineOperand::MO_BlockAddress: {
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000529 MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000530 O << BA->getName();
531 break;
532 }
533
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000534 case MachineOperand::MO_ConstantPoolIndex:
Rafael Espindola58873562014-01-03 19:21:54 +0000535 O << DL->getPrivateGlobalPrefix() << "CPI"
Chris Lattnera5bb3702007-12-30 23:10:15 +0000536 << getFunctionNumber() << "_" << MO.getIndex();
Bruno Cardoso Lopes4713b282009-11-19 06:06:13 +0000537 if (MO.getOffset())
538 O << "+" << MO.getOffset();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000539 break;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000540
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000541 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000542 llvm_unreachable("<unknown operand type>");
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000543 }
544
545 if (closeP) O << ")";
546}
547
Chris Lattner76c564b2010-04-04 04:47:45 +0000548void MipsAsmPrinter::printUnsignedImm(const MachineInstr *MI, int opNum,
549 raw_ostream &O) {
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000550 const MachineOperand &MO = MI->getOperand(opNum);
Devang Patel12f68552010-04-27 22:24:37 +0000551 if (MO.isImm())
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000552 O << (unsigned short int)MO.getImm();
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000553 else
Chris Lattner76c564b2010-04-04 04:47:45 +0000554 printOperand(MI, opNum, O);
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000555}
556
Daniel Sanders3f6eb542013-11-12 10:45:18 +0000557void MipsAsmPrinter::printUnsignedImm8(const MachineInstr *MI, int opNum,
558 raw_ostream &O) {
559 const MachineOperand &MO = MI->getOperand(opNum);
560 if (MO.isImm())
561 O << (unsigned short int)(unsigned char)MO.getImm();
562 else
563 printOperand(MI, opNum, O);
564}
565
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000566void MipsAsmPrinter::
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000567printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O) {
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000568 // Load/Store memory operands -- imm($reg)
569 // If PIC target the target is loaded as the
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +0000570 // pattern lw $25,%call16($28)
Chris Lattner76c564b2010-04-04 04:47:45 +0000571 printOperand(MI, opNum+1, O);
Akira Hatanaka2e766ed2011-07-07 18:57:00 +0000572 O << "(";
573 printOperand(MI, opNum, O);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000574 O << ")";
575}
576
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000577void MipsAsmPrinter::
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000578printMemOperandEA(const MachineInstr *MI, int opNum, raw_ostream &O) {
579 // when using stack locations for not load/store instructions
580 // print the same way as all normal 3 operand instructions.
581 printOperand(MI, opNum, O);
582 O << ", ";
583 printOperand(MI, opNum+1, O);
584 return;
585}
586
587void MipsAsmPrinter::
Chris Lattner76c564b2010-04-04 04:47:45 +0000588printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
589 const char *Modifier) {
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000590 const MachineOperand &MO = MI->getOperand(opNum);
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000591 O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000592}
593
Bob Wilsonb633d7a2009-09-30 22:06:26 +0000594void MipsAsmPrinter::EmitStartOfAsmFile(Module &M) {
Jack Carterf9f753c2013-06-18 19:47:15 +0000595 // TODO: Need to add -mabicalls and -mno-abicalls flags.
596 // Currently we assume that -mabicalls is the default.
Jack Carter0cd3c192014-01-06 23:27:31 +0000597 getTargetStreamer().emitDirectiveAbiCalls();
598 Reloc::Model RM = Subtarget->getRelocationModel();
599 if (RM == Reloc::Static && !Subtarget->hasMips64())
600 getTargetStreamer().emitDirectiveOptionPic0();
Jack Carterf9f753c2013-06-18 19:47:15 +0000601
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000602 // Tell the assembler which ABI we are using
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000603 std::string SectionName = std::string(".mdebug.") + getCurrentABIString();
604 OutStreamer.SwitchSection(OutContext.getELFSection(
605 SectionName, ELF::SHT_PROGBITS, 0, SectionKind::getDataRel()));
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000606
607 // TODO: handle O64 ABI
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000608
609 if (Subtarget->isABI_EABI()) {
610 if (Subtarget->isGP32bit())
611 OutStreamer.SwitchSection(
612 OutContext.getELFSection(".gcc_compiled_long32", ELF::SHT_PROGBITS, 0,
613 SectionKind::getDataRel()));
614 else
615 OutStreamer.SwitchSection(
616 OutContext.getELFSection(".gcc_compiled_long64", ELF::SHT_PROGBITS, 0,
617 SectionKind::getDataRel()));
Benjamin Kramer0151d7b2010-04-05 10:17:15 +0000618 }
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000619
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000620 // return to the text section
621 OutStreamer.SwitchSection(OutContext.getObjectFileInfo()->getTextSection());
Jack Carterc1b17ed2013-01-18 21:20:38 +0000622}
623
624void MipsAsmPrinter::EmitEndOfAsmFile(Module &M) {
Jack Carterc1b17ed2013-01-18 21:20:38 +0000625 // Emit Mips ELF register info
626 Subtarget->getMReginfo().emitMipsReginfoSectionCG(
627 OutStreamer, getObjFileLowering(), *Subtarget);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000628}
629
Akira Hatanakaf2bcad92011-07-01 01:04:43 +0000630void MipsAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
631 raw_ostream &OS) {
632 // TODO: implement
633}
634
Bob Wilson5a495fe2009-06-23 23:59:40 +0000635// Force static initialization.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000636extern "C" void LLVMInitializeMipsAsmPrinter() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +0000637 RegisterAsmPrinter<MipsAsmPrinter> X(TheMipsTarget);
638 RegisterAsmPrinter<MipsAsmPrinter> Y(TheMipselTarget);
Akira Hatanaka3d673cc2011-09-21 03:00:58 +0000639 RegisterAsmPrinter<MipsAsmPrinter> A(TheMips64Target);
640 RegisterAsmPrinter<MipsAsmPrinter> B(TheMips64elTarget);
Daniel Dunbare8338102009-07-15 20:24:03 +0000641}