blob: 2f49e74ab8245c51315f10c830c175ee943e0f01 [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 Espindolaac4ad252013-10-05 16:42:21 +000037#include "llvm/MC/MCELFStreamer.h"
Akira Hatanaka9c6028f2011-07-07 23:56:50 +000038#include "llvm/MC/MCInst.h"
Chris Lattner4cd44982009-09-13 17:14:04 +000039#include "llvm/MC/MCSymbol.h"
Jack Carterab3cb422013-02-19 22:04:37 +000040#include "llvm/Support/ELF.h"
Jack Carterb2af5122012-07-05 23:58:21 +000041#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000042#include "llvm/Support/raw_ostream.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() {
Rafael Espindola4a1a3602014-01-14 01:21:46 +000049 return static_cast<MipsTargetStreamer &>(*OutStreamer.getTargetStreamer());
Rafael Espindolaa17151a2013-10-08 13:08:17 +000050}
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>();
Reed Kotler91ae9822013-10-27 21:57:36 +000058 MCP = MF.getConstantPool();
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000059 AsmPrinter::runOnMachineFunction(MF);
60 return true;
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +000061}
62
Akira Hatanaka42a35242012-09-27 01:59:07 +000063bool MipsAsmPrinter::lowerOperand(const MachineOperand &MO, MCOperand &MCOp) {
64 MCOp = MCInstLowering.LowerOperand(MO);
65 return MCOp.isValid();
66}
67
68#include "MipsGenMCPseudoLowering.inc"
69
Akira Hatanakaddd12652011-07-07 20:10:52 +000070void MipsAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Akira Hatanakaddd12652011-07-07 20:10:52 +000071 if (MI->isDebugValue()) {
Bruno Cardoso Lopescd1d4472011-12-30 21:09:41 +000072 SmallString<128> Str;
73 raw_svector_ostream OS(Str);
74
Akira Hatanakaddd12652011-07-07 20:10:52 +000075 PrintDebugValueComment(MI, OS);
76 return;
77 }
78
Reed Kotler91ae9822013-10-27 21:57:36 +000079 // If we just ended a constant pool, mark it as such.
80 if (InConstantPool && MI->getOpcode() != Mips::CONSTPOOL_ENTRY) {
81 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
82 InConstantPool = false;
83 }
84 if (MI->getOpcode() == Mips::CONSTPOOL_ENTRY) {
85 // CONSTPOOL_ENTRY - This instruction represents a floating
86 //constant pool in the function. The first operand is the ID#
87 // for this instruction, the second is the index into the
88 // MachineConstantPool that this is, the third is the size in
89 // bytes of this constant pool entry.
90 // The required alignment is specified on the basic block holding this MI.
91 //
92 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
93 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
94
95 // If this is the first entry of the pool, mark it.
96 if (!InConstantPool) {
97 OutStreamer.EmitDataRegion(MCDR_DataRegion);
98 InConstantPool = true;
99 }
100
101 OutStreamer.EmitLabel(GetCPISymbol(LabelId));
102
103 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
104 if (MCPE.isMachineConstantPoolEntry())
105 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
106 else
107 EmitGlobalConstant(MCPE.Val.ConstVal);
108 return;
109 }
110
Rafael Espindola14d02fe2014-01-25 15:06:56 +0000111
Akira Hatanaka5ac78682012-06-13 23:25:52 +0000112 MachineBasicBlock::const_instr_iterator I = MI;
113 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
114
115 do {
Akira Hatanaka556135d2013-02-06 21:50:15 +0000116 // Do any auto-generated pseudo lowerings.
117 if (emitPseudoExpansionLowering(OutStreamer, &*I))
118 continue;
Jack Carterc20a21b2012-08-28 19:07:39 +0000119
Reed Kotler76c9bcd2013-02-15 21:05:58 +0000120 // The inMips16Mode() test is not permanent.
121 // Some instructions are marked as pseudo right now which
122 // would make the test fail for the wrong reason but
123 // that will be fixed soon. We need this here because we are
124 // removing another test for this situation downstream in the
125 // callchain.
126 //
127 if (I->isPseudo() && !Subtarget->inMips16Mode())
128 llvm_unreachable("Pseudo opcode found in EmitInstruction()");
129
Akira Hatanaka556135d2013-02-06 21:50:15 +0000130 MCInst TmpInst0;
131 MCInstLowering.Lower(I, TmpInst0);
Akira Hatanaka5ac78682012-06-13 23:25:52 +0000132 OutStreamer.EmitInstruction(TmpInst0);
Akira Hatanaka556135d2013-02-06 21:50:15 +0000133 } while ((++I != E) && I->isInsideBundle()); // Delay slot check
Akira Hatanakaddd12652011-07-07 20:10:52 +0000134}
135
Akira Hatanakae2489122011-04-15 21:51:11 +0000136//===----------------------------------------------------------------------===//
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000137//
138// Mips Asm Directives
139//
140// -- Frame directive "frame Stackpointer, Stacksize, RARegister"
141// Describe the stack frame.
142//
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000143// -- Mask directives "(f)mask bitmask, offset"
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000144// Tells the assembler which registers are saved and where.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000145// bitmask - contain a little endian bitset indicating which registers are
146// saved on function prologue (e.g. with a 0x80000000 mask, the
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000147// assembler knows the register 31 (RA) is saved at prologue.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000148// offset - the position before stack pointer subtraction indicating where
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000149// the first saved register on prologue is located. (e.g. with a
150//
151// Consider the following function prologue:
152//
Bill Wendling97925ec2008-02-27 06:33:05 +0000153// .frame $fp,48,$ra
154// .mask 0xc0000000,-8
155// addiu $sp, $sp, -48
156// sw $ra, 40($sp)
157// sw $fp, 36($sp)
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000158//
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000159// With a 0xc0000000 mask, the assembler knows the register 31 (RA) and
160// 30 (FP) are saved at prologue. As the save order on prologue is from
161// left to right, RA is saved first. A -8 offset means that after the
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000162// stack pointer subtration, the first register in the mask (RA) will be
163// saved at address 48-8=40.
164//
Akira Hatanakae2489122011-04-15 21:51:11 +0000165//===----------------------------------------------------------------------===//
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000166
Akira Hatanakae2489122011-04-15 21:51:11 +0000167//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000168// Mask directives
Akira Hatanakae2489122011-04-15 21:51:11 +0000169//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000170
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000171// Create a bitmask with all callee saved registers for CPU or Floating Point
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000172// registers. For CPU registers consider RA, GP and FP for saving if necessary.
Chris Lattner76c564b2010-04-04 04:47:45 +0000173void MipsAsmPrinter::printSavedRegsBitmask(raw_ostream &O) {
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000174 // CPU and FPU Saved Registers Bitmasks
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000175 unsigned CPUBitmask = 0, FPUBitmask = 0;
176 int CPUTopSavedRegOff, FPUTopSavedRegOff;
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000177
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000178 // Set the CPU and FPU Bitmasks
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000179 const MachineFrameInfo *MFI = MF->getFrameInfo();
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000180 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000181 // size of stack area to which FP callee-saved regs are saved.
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000182 unsigned CPURegSize = Mips::GPR32RegClass.getSize();
Craig Topperc7242e02012-04-20 07:30:17 +0000183 unsigned FGR32RegSize = Mips::FGR32RegClass.getSize();
184 unsigned AFGR64RegSize = Mips::AFGR64RegClass.getSize();
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000185 bool HasAFGR64Reg = false;
186 unsigned CSFPRegsSize = 0;
187 unsigned i, e = CSI.size();
188
189 // Set FPU Bitmask.
190 for (i = 0; i != e; ++i) {
Rafael Espindolaf2dffce2010-06-02 20:02:30 +0000191 unsigned Reg = CSI[i].getReg();
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000192 if (Mips::GPR32RegClass.contains(Reg))
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000193 break;
194
Akira Hatanaka5d6faed2012-12-10 20:04:40 +0000195 unsigned RegNum = TM.getRegisterInfo()->getEncodingValue(Reg);
Craig Topperc7242e02012-04-20 07:30:17 +0000196 if (Mips::AFGR64RegClass.contains(Reg)) {
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000197 FPUBitmask |= (3 << RegNum);
198 CSFPRegsSize += AFGR64RegSize;
199 HasAFGR64Reg = true;
200 continue;
201 }
202
203 FPUBitmask |= (1 << RegNum);
204 CSFPRegsSize += FGR32RegSize;
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000205 }
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000206
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000207 // Set CPU Bitmask.
208 for (; i != e; ++i) {
209 unsigned Reg = CSI[i].getReg();
Akira Hatanaka5d6faed2012-12-10 20:04:40 +0000210 unsigned RegNum = TM.getRegisterInfo()->getEncodingValue(Reg);
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000211 CPUBitmask |= (1 << RegNum);
212 }
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000213
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000214 // FP Regs are saved right below where the virtual frame pointer points to.
215 FPUTopSavedRegOff = FPUBitmask ?
216 (HasAFGR64Reg ? -AFGR64RegSize : -FGR32RegSize) : 0;
217
218 // CPU Regs are saved below FP Regs.
219 CPUTopSavedRegOff = CPUBitmask ? -CSFPRegsSize - CPURegSize : 0;
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000220
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000221 // Print CPUBitmask
Chris Lattner76c564b2010-04-04 04:47:45 +0000222 O << "\t.mask \t"; printHex32(CPUBitmask, O);
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000223 O << ',' << CPUTopSavedRegOff << '\n';
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000224
225 // Print FPUBitmask
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000226 O << "\t.fmask\t"; printHex32(FPUBitmask, O);
227 O << "," << FPUTopSavedRegOff << '\n';
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000228}
229
230// Print a 32 bit hex number with all numbers.
Chris Lattner76c564b2010-04-04 04:47:45 +0000231void MipsAsmPrinter::printHex32(unsigned Value, raw_ostream &O) {
Owen Anderson93719642008-08-21 00:14:44 +0000232 O << "0x";
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000233 for (int i = 7; i >= 0; i--)
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000234 O.write_hex((Value & (0xF << (i*4))) >> (i*4));
Bruno Cardoso Lopesbcda5e22007-07-11 23:24:41 +0000235}
236
Akira Hatanakae2489122011-04-15 21:51:11 +0000237//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000238// Frame and Set directives
Akira Hatanakae2489122011-04-15 21:51:11 +0000239//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000240
241/// Frame Directive
Chris Lattner5e596182010-04-04 07:05:53 +0000242void MipsAsmPrinter::emitFrameDirective() {
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000243 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
244
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000245 unsigned stackReg = RI.getFrameRegister(*MF);
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000246 unsigned returnReg = RI.getRARegister();
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000247 unsigned stackSize = MF->getFrameInfo()->getStackSize();
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000248
Jia Liuf54f60f2012-02-28 07:46:26 +0000249 if (OutStreamer.hasRawTextSupport())
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000250 OutStreamer.EmitRawText("\t.frame\t$" +
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000251 StringRef(MipsInstPrinter::getRegisterName(stackReg)).lower() +
Akira Hatanaka9c6028f2011-07-07 23:56:50 +0000252 "," + Twine(stackSize) + ",$" +
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000253 StringRef(MipsInstPrinter::getRegisterName(returnReg)).lower());
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000254}
255
256/// Emit Set directives.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000257const char *MipsAsmPrinter::getCurrentABIString() const {
Chris Lattner5e596182010-04-04 07:05:53 +0000258 switch (Subtarget->getTargetABI()) {
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000259 case MipsSubtarget::O32: return "abi32";
Chris Lattner5e596182010-04-04 07:05:53 +0000260 case MipsSubtarget::N32: return "abiN32";
261 case MipsSubtarget::N64: return "abi64";
262 case MipsSubtarget::EABI: return "eabi32"; // TODO: handle eabi64
Dmitri Gribenkoca1e27b2012-09-10 21:26:47 +0000263 default: llvm_unreachable("Unknown Mips ABI");
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000264 }
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000265}
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000266
Chris Lattner5d9fb4b2010-01-27 23:23:58 +0000267void MipsAsmPrinter::EmitFunctionEntryLabel() {
Rafael Espindola6633d572014-01-14 18:57:12 +0000268 MipsTargetStreamer &TS = getTargetStreamer();
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000269 if (Subtarget->inMicroMipsMode())
Rafael Espindola6633d572014-01-14 18:57:12 +0000270 TS.emitDirectiveSetMicroMips();
271 // leave out until FSF available gas has micromips changes
272 // else
273 // TS.emitDirectiveSetNoMicroMips();
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000274
Rafael Espindola6633d572014-01-14 18:57:12 +0000275 if (Subtarget->inMips16Mode())
276 TS.emitDirectiveSetMips16();
277 else
278 TS.emitDirectiveSetNoMips16();
Jack Carterab3cb422013-02-19 22:04:37 +0000279
Rafael Espindola6633d572014-01-14 18:57:12 +0000280 TS.emitDirectiveEnt(*CurrentFnSym);
Chris Lattner5d9fb4b2010-01-27 23:23:58 +0000281 OutStreamer.EmitLabel(CurrentFnSym);
282}
283
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000284/// EmitFunctionBodyStart - Targets can override this to emit stuff before
285/// the first basic block in the function.
286void MipsAsmPrinter::EmitFunctionBodyStart() {
Rafael Espindola7d78b2a2013-10-29 16:24:21 +0000287 MCInstLowering.Initialize(&MF->getContext());
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +0000288
Reed Kotler0f2b10e2013-05-03 23:17:24 +0000289 bool IsNakedFunction =
290 MF->getFunction()->
291 getAttributes().hasAttribute(AttributeSet::FunctionIndex,
292 Attribute::Naked);
293 if (!IsNakedFunction)
294 emitFrameDirective();
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000295
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000296 if (OutStreamer.hasRawTextSupport()) {
297 SmallString<128> Str;
298 raw_svector_ostream OS(Str);
Reed Kotler0f2b10e2013-05-03 23:17:24 +0000299 if (!IsNakedFunction)
300 printSavedRegsBitmask(OS);
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000301 OutStreamer.EmitRawText(OS.str());
Reed Kotlerf0221472013-02-15 01:04:38 +0000302 if (!Subtarget->inMips16Mode()) {
303 OutStreamer.EmitRawText(StringRef("\t.set\tnoreorder"));
304 OutStreamer.EmitRawText(StringRef("\t.set\tnomacro"));
305 OutStreamer.EmitRawText(StringRef("\t.set\tnoat"));
306 }
Akira Hatanaka8f3573032012-05-12 00:48:43 +0000307 }
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000308}
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000309
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000310/// EmitFunctionBodyEnd - Targets can override this to emit stuff after
311/// the last basic block in the function.
312void MipsAsmPrinter::EmitFunctionBodyEnd() {
Chris Lattnerfd97a332010-01-28 01:48:52 +0000313 // There are instruction for this macros, but they must
314 // always be at the function end, and we can't emit and
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000315 // break with BB logic.
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000316 if (OutStreamer.hasRawTextSupport()) {
Reed Kotlerf0221472013-02-15 01:04:38 +0000317 if (!Subtarget->inMips16Mode()) {
318 OutStreamer.EmitRawText(StringRef("\t.set\tat"));
319 OutStreamer.EmitRawText(StringRef("\t.set\tmacro"));
320 OutStreamer.EmitRawText(StringRef("\t.set\treorder"));
321 }
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000322 OutStreamer.EmitRawText("\t.end\t" + Twine(CurrentFnSym->getName()));
323 }
Reed Kotler91ae9822013-10-27 21:57:36 +0000324 // Make sure to terminate any constant pools that were at the end
325 // of the function.
326 if (!InConstantPool)
327 return;
328 InConstantPool = false;
329 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000330}
331
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000332/// isBlockOnlyReachableByFallthough - Return true if the basic block has
333/// exactly one predecessor and the control transfer mechanism between
334/// the predecessor and this block is a fall-through.
Akira Hatanakae2489122011-04-15 21:51:11 +0000335bool MipsAsmPrinter::isBlockOnlyReachableByFallthrough(const MachineBasicBlock*
336 MBB) const {
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000337 // The predecessor has to be immediately before this block.
338 const MachineBasicBlock *Pred = *MBB->pred_begin();
339
340 // If the predecessor is a switch statement, assume a jump table
341 // implementation, so it is not a fall through.
342 if (const BasicBlock *bb = Pred->getBasicBlock())
343 if (isa<SwitchInst>(bb->getTerminator()))
344 return false;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000345
Akira Hatanakae625ba42011-04-01 18:57:38 +0000346 // If this is a landing pad, it isn't a fall through. If it has no preds,
347 // then nothing falls through to it.
348 if (MBB->isLandingPad() || MBB->pred_empty())
349 return false;
350
351 // If there isn't exactly one predecessor, it can't be a fall through.
352 MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI;
353 ++PI2;
Jia Liuf54f60f2012-02-28 07:46:26 +0000354
Akira Hatanakae625ba42011-04-01 18:57:38 +0000355 if (PI2 != MBB->pred_end())
Jia Liuf54f60f2012-02-28 07:46:26 +0000356 return false;
Akira Hatanakae625ba42011-04-01 18:57:38 +0000357
358 // The predecessor has to be immediately before this block.
359 if (!Pred->isLayoutSuccessor(MBB))
360 return false;
Jia Liuf54f60f2012-02-28 07:46:26 +0000361
Akira Hatanakae625ba42011-04-01 18:57:38 +0000362 // If the block is completely empty, then it definitely does fall through.
363 if (Pred->empty())
364 return true;
Jia Liuf54f60f2012-02-28 07:46:26 +0000365
Akira Hatanakae625ba42011-04-01 18:57:38 +0000366 // Otherwise, check the last instruction.
367 // Check if the last terminator is an unconditional branch.
368 MachineBasicBlock::const_iterator I = Pred->end();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000369 while (I != Pred->begin() && !(--I)->isTerminator()) ;
Akira Hatanakae625ba42011-04-01 18:57:38 +0000370
Evan Cheng7f8e5632011-12-07 07:15:52 +0000371 return !I->isBarrier();
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000372}
373
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000374// Print out an operand for an inline asm expression.
Eric Christophered51b9e2012-05-10 21:48:22 +0000375bool MipsAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Chris Lattner3bb09762010-04-04 05:29:35 +0000376 unsigned AsmVariant,const char *ExtraCode,
377 raw_ostream &O) {
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000378 // Does this asm operand have a single letter operand modifier?
Eric Christophered51b9e2012-05-10 21:48:22 +0000379 if (ExtraCode && ExtraCode[0]) {
380 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000381
Eric Christophered51b9e2012-05-10 21:48:22 +0000382 const MachineOperand &MO = MI->getOperand(OpNum);
383 switch (ExtraCode[0]) {
Eric Christopherbc5d2492012-05-19 00:51:56 +0000384 default:
Jack Carterb2fd5f62012-06-21 17:14:46 +0000385 // See if this is a generic print operand
386 return AsmPrinter::PrintAsmOperand(MI,OpNum,AsmVariant,ExtraCode,O);
Eric Christopherbc5d2492012-05-19 00:51:56 +0000387 case 'X': // hex const int
388 if ((MO.getType()) != MachineOperand::MO_Immediate)
389 return true;
390 O << "0x" << StringRef(utohexstr(MO.getImm())).lower();
391 return false;
392 case 'x': // hex const int (low 16 bits)
393 if ((MO.getType()) != MachineOperand::MO_Immediate)
394 return true;
395 O << "0x" << StringRef(utohexstr(MO.getImm() & 0xffff)).lower();
396 return false;
397 case 'd': // decimal const int
398 if ((MO.getType()) != MachineOperand::MO_Immediate)
399 return true;
400 O << MO.getImm();
401 return false;
Eric Christopherf481ab32012-05-30 19:05:19 +0000402 case 'm': // decimal const int minus 1
403 if ((MO.getType()) != MachineOperand::MO_Immediate)
404 return true;
405 O << MO.getImm() - 1;
406 return false;
Jack Carter27747b52012-06-28 20:46:26 +0000407 case 'z': {
408 // $0 if zero, regular printing otherwise
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000409 if (MO.getType() != MachineOperand::MO_Immediate)
410 return true;
411 int64_t Val = MO.getImm();
412 if (Val)
413 O << Val;
414 else
415 O << "$0";
416 return false;
417 }
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000418 case 'D': // Second part of a double word register operand
419 case 'L': // Low order register of a double word register operand
Jack Cartera62ba822012-07-18 06:41:36 +0000420 case 'M': // High order register of a double word register operand
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000421 {
Jack Carterb2af5122012-07-05 23:58:21 +0000422 if (OpNum == 0)
423 return true;
424 const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
425 if (!FlagsOP.isImm())
426 return true;
427 unsigned Flags = FlagsOP.getImm();
428 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Jack Carter2ab73b12012-07-06 02:44:22 +0000429 // Number of registers represented by this operand. We are looking
430 // for 2 for 32 bit mode and 1 for 64 bit mode.
Jack Carterb2af5122012-07-05 23:58:21 +0000431 if (NumVals != 2) {
Jack Carter2ab73b12012-07-06 02:44:22 +0000432 if (Subtarget->isGP64bit() && NumVals == 1 && MO.isReg()) {
Jack Carterb2af5122012-07-05 23:58:21 +0000433 unsigned Reg = MO.getReg();
434 O << '$' << MipsInstPrinter::getRegisterName(Reg);
435 return false;
436 }
437 return true;
438 }
Jack Carter42ebf982012-07-11 21:41:49 +0000439
440 unsigned RegOp = OpNum;
441 if (!Subtarget->isGP64bit()){
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000442 // Endianess reverses which register holds the high or low value
Jack Cartera62ba822012-07-18 06:41:36 +0000443 // between M and L.
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000444 switch(ExtraCode[0]) {
Jack Cartera62ba822012-07-18 06:41:36 +0000445 case 'M':
446 RegOp = (Subtarget->isLittle()) ? OpNum + 1 : OpNum;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000447 break;
448 case 'L':
Jack Cartera62ba822012-07-18 06:41:36 +0000449 RegOp = (Subtarget->isLittle()) ? OpNum : OpNum + 1;
450 break;
451 case 'D': // Always the second part
452 RegOp = OpNum + 1;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000453 }
454 if (RegOp >= MI->getNumOperands())
455 return true;
456 const MachineOperand &MO = MI->getOperand(RegOp);
457 if (!MO.isReg())
458 return true;
459 unsigned Reg = MO.getReg();
460 O << '$' << MipsInstPrinter::getRegisterName(Reg);
461 return false;
Jack Carterb2af5122012-07-05 23:58:21 +0000462 }
Eric Christophered51b9e2012-05-10 21:48:22 +0000463 }
Daniel Sanders8b59af12013-11-12 12:56:01 +0000464 case 'w':
465 // Print MSA registers for the 'f' constraint
466 // In LLVM, the 'w' modifier doesn't need to do anything.
467 // We can just call printOperand as normal.
468 break;
Jack Carter2ab73b12012-07-06 02:44:22 +0000469 }
470 }
Eric Christophered51b9e2012-05-10 21:48:22 +0000471
472 printOperand(MI, OpNum, O);
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000473 return false;
474}
475
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000476bool MipsAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
477 unsigned OpNum, unsigned AsmVariant,
478 const char *ExtraCode,
479 raw_ostream &O) {
Jack Carterb04e3572013-04-09 23:19:50 +0000480 int Offset = 0;
481 // Currently we are expecting either no ExtraCode or 'D'
482 if (ExtraCode) {
483 if (ExtraCode[0] == 'D')
484 Offset = 4;
485 else
486 return true; // Unknown modifier.
487 }
Jia Liuf54f60f2012-02-28 07:46:26 +0000488
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000489 const MachineOperand &MO = MI->getOperand(OpNum);
490 assert(MO.isReg() && "unexpected inline asm memory operand");
Jack Carterb04e3572013-04-09 23:19:50 +0000491 O << Offset << "($" << MipsInstPrinter::getRegisterName(MO.getReg()) << ")";
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000492
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000493 return false;
494}
495
Chris Lattner76c564b2010-04-04 04:47:45 +0000496void MipsAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
497 raw_ostream &O) {
Rafael Espindola58873562014-01-03 19:21:54 +0000498 const DataLayout *DL = TM.getDataLayout();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000499 const MachineOperand &MO = MI->getOperand(opNum);
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +0000500 bool closeP = false;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000501
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000502 if (MO.getTargetFlags())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000503 closeP = true;
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000504
505 switch(MO.getTargetFlags()) {
506 case MipsII::MO_GPREL: O << "%gp_rel("; break;
507 case MipsII::MO_GOT_CALL: O << "%call16("; break;
Akira Hatanaka56d9ef52011-04-01 21:41:06 +0000508 case MipsII::MO_GOT: O << "%got("; break;
509 case MipsII::MO_ABS_HI: O << "%hi("; break;
510 case MipsII::MO_ABS_LO: O << "%lo("; break;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +0000511 case MipsII::MO_TLSGD: O << "%tlsgd("; break;
512 case MipsII::MO_GOTTPREL: O << "%gottprel("; break;
513 case MipsII::MO_TPREL_HI: O << "%tprel_hi("; break;
514 case MipsII::MO_TPREL_LO: O << "%tprel_lo("; break;
Akira Hatanaka25ce3642011-09-22 03:09:07 +0000515 case MipsII::MO_GPOFF_HI: O << "%hi(%neg(%gp_rel("; break;
516 case MipsII::MO_GPOFF_LO: O << "%lo(%neg(%gp_rel("; break;
517 case MipsII::MO_GOT_DISP: O << "%got_disp("; break;
518 case MipsII::MO_GOT_PAGE: O << "%got_page("; break;
519 case MipsII::MO_GOT_OFST: O << "%got_ofst("; break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000520 }
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000521
Chris Lattnereb2cc682009-09-13 20:31:40 +0000522 switch (MO.getType()) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000523 case MachineOperand::MO_Register:
Akira Hatanaka9c6028f2011-07-07 23:56:50 +0000524 O << '$'
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000525 << StringRef(MipsInstPrinter::getRegisterName(MO.getReg())).lower();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000526 break;
527
528 case MachineOperand::MO_Immediate:
Akira Hatanaka2db176c2011-05-24 21:22:21 +0000529 O << MO.getImm();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000530 break;
531
532 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner29bdac42010-03-13 21:04:28 +0000533 O << *MO.getMBB()->getSymbol();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000534 return;
535
536 case MachineOperand::MO_GlobalAddress:
Rafael Espindola79858aa2013-10-29 17:07:16 +0000537 O << *getSymbol(MO.getGlobal());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000538 break;
539
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000540 case MachineOperand::MO_BlockAddress: {
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000541 MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000542 O << BA->getName();
543 break;
544 }
545
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000546 case MachineOperand::MO_ConstantPoolIndex:
Rafael Espindola58873562014-01-03 19:21:54 +0000547 O << DL->getPrivateGlobalPrefix() << "CPI"
Chris Lattnera5bb3702007-12-30 23:10:15 +0000548 << getFunctionNumber() << "_" << MO.getIndex();
Bruno Cardoso Lopes4713b282009-11-19 06:06:13 +0000549 if (MO.getOffset())
550 O << "+" << MO.getOffset();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000551 break;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000552
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000553 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000554 llvm_unreachable("<unknown operand type>");
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000555 }
556
557 if (closeP) O << ")";
558}
559
Chris Lattner76c564b2010-04-04 04:47:45 +0000560void MipsAsmPrinter::printUnsignedImm(const MachineInstr *MI, int opNum,
561 raw_ostream &O) {
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000562 const MachineOperand &MO = MI->getOperand(opNum);
Devang Patel12f68552010-04-27 22:24:37 +0000563 if (MO.isImm())
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000564 O << (unsigned short int)MO.getImm();
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000565 else
Chris Lattner76c564b2010-04-04 04:47:45 +0000566 printOperand(MI, opNum, O);
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000567}
568
Daniel Sanders3f6eb542013-11-12 10:45:18 +0000569void MipsAsmPrinter::printUnsignedImm8(const MachineInstr *MI, int opNum,
570 raw_ostream &O) {
571 const MachineOperand &MO = MI->getOperand(opNum);
572 if (MO.isImm())
573 O << (unsigned short int)(unsigned char)MO.getImm();
574 else
575 printOperand(MI, opNum, O);
576}
577
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000578void MipsAsmPrinter::
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000579printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O) {
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000580 // Load/Store memory operands -- imm($reg)
581 // If PIC target the target is loaded as the
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +0000582 // pattern lw $25,%call16($28)
Chris Lattner76c564b2010-04-04 04:47:45 +0000583 printOperand(MI, opNum+1, O);
Akira Hatanaka2e766ed2011-07-07 18:57:00 +0000584 O << "(";
585 printOperand(MI, opNum, O);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000586 O << ")";
587}
588
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000589void MipsAsmPrinter::
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000590printMemOperandEA(const MachineInstr *MI, int opNum, raw_ostream &O) {
591 // when using stack locations for not load/store instructions
592 // print the same way as all normal 3 operand instructions.
593 printOperand(MI, opNum, O);
594 O << ", ";
595 printOperand(MI, opNum+1, O);
596 return;
597}
598
599void MipsAsmPrinter::
Chris Lattner76c564b2010-04-04 04:47:45 +0000600printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
601 const char *Modifier) {
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000602 const MachineOperand &MO = MI->getOperand(opNum);
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000603 O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000604}
605
Bob Wilsonb633d7a2009-09-30 22:06:26 +0000606void MipsAsmPrinter::EmitStartOfAsmFile(Module &M) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000607 // FIXME: Use SwitchSection.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000608
Jack Carterf9f753c2013-06-18 19:47:15 +0000609 // TODO: Need to add -mabicalls and -mno-abicalls flags.
610 // Currently we assume that -mabicalls is the default.
Jack Carter0cd3c192014-01-06 23:27:31 +0000611 getTargetStreamer().emitDirectiveAbiCalls();
612 Reloc::Model RM = Subtarget->getRelocationModel();
613 if (RM == Reloc::Static && !Subtarget->hasMips64())
614 getTargetStreamer().emitDirectiveOptionPic0();
Jack Carterf9f753c2013-06-18 19:47:15 +0000615
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000616 // Tell the assembler which ABI we are using
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000617 if (OutStreamer.hasRawTextSupport())
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000618 OutStreamer.EmitRawText("\t.section .mdebug." +
619 Twine(getCurrentABIString()));
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000620
621 // TODO: handle O64 ABI
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000622 if (OutStreamer.hasRawTextSupport()) {
623 if (Subtarget->isABI_EABI()) {
624 if (Subtarget->isGP32bit())
625 OutStreamer.EmitRawText(StringRef("\t.section .gcc_compiled_long32"));
626 else
627 OutStreamer.EmitRawText(StringRef("\t.section .gcc_compiled_long64"));
628 }
Benjamin Kramer0151d7b2010-04-05 10:17:15 +0000629 }
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000630
631 // return to previous section
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000632 if (OutStreamer.hasRawTextSupport())
633 OutStreamer.EmitRawText(StringRef("\t.previous"));
Jack Carterc1b17ed2013-01-18 21:20:38 +0000634
635}
636
Rafael Espindola14d02fe2014-01-25 15:06:56 +0000637static void emitELFHeaderFlagsCG(MipsTargetStreamer &TargetStreamer,
638 const MipsSubtarget &Subtarget) {
Rafael Espindolaac4ad252013-10-05 16:42:21 +0000639 // Update e_header flags
640 unsigned EFlags = 0;
641
642 // TODO: Need to add -mabicalls and -mno-abicalls flags.
643 // Currently we assume that -mabicalls is the default.
644 EFlags |= ELF::EF_MIPS_CPIC;
645
Rafael Espindola14d02fe2014-01-25 15:06:56 +0000646 if (Subtarget.inMips16Mode())
Rafael Espindolaac4ad252013-10-05 16:42:21 +0000647 EFlags |= ELF::EF_MIPS_ARCH_ASE_M16;
648 else
649 EFlags |= ELF::EF_MIPS_NOREORDER;
650
651 // Architecture
Rafael Espindola14d02fe2014-01-25 15:06:56 +0000652 if (Subtarget.hasMips64r2())
Rafael Espindolaac4ad252013-10-05 16:42:21 +0000653 EFlags |= ELF::EF_MIPS_ARCH_64R2;
Rafael Espindola14d02fe2014-01-25 15:06:56 +0000654 else if (Subtarget.hasMips64())
Rafael Espindolaac4ad252013-10-05 16:42:21 +0000655 EFlags |= ELF::EF_MIPS_ARCH_64;
Rafael Espindola14d02fe2014-01-25 15:06:56 +0000656 else if (Subtarget.hasMips32r2())
Rafael Espindolaac4ad252013-10-05 16:42:21 +0000657 EFlags |= ELF::EF_MIPS_ARCH_32R2;
658 else
659 EFlags |= ELF::EF_MIPS_ARCH_32;
660
Rafael Espindola14d02fe2014-01-25 15:06:56 +0000661 if (Subtarget.inMicroMipsMode())
Rafael Espindolaac4ad252013-10-05 16:42:21 +0000662 EFlags |= ELF::EF_MIPS_MICROMIPS;
663
664 // ABI
Rafael Espindola14d02fe2014-01-25 15:06:56 +0000665 if (Subtarget.isABI_O32())
Rafael Espindolaac4ad252013-10-05 16:42:21 +0000666 EFlags |= ELF::EF_MIPS_ABI_O32;
667
668 // Relocation Model
Rafael Espindola14d02fe2014-01-25 15:06:56 +0000669 Reloc::Model RM = Subtarget.getRelocationModel();
Rafael Espindolaac4ad252013-10-05 16:42:21 +0000670 if (RM == Reloc::PIC_ || RM == Reloc::Default)
671 EFlags |= ELF::EF_MIPS_PIC;
672 else if (RM == Reloc::Static)
673 ; // Do nothing for Reloc::Static
674 else
675 llvm_unreachable("Unsupported relocation model for e_flags");
676
Rafael Espindola14d02fe2014-01-25 15:06:56 +0000677 TargetStreamer.emitMipsHackELFFlags(EFlags);
Rafael Espindolaac4ad252013-10-05 16:42:21 +0000678}
679
Jack Carterc1b17ed2013-01-18 21:20:38 +0000680void MipsAsmPrinter::EmitEndOfAsmFile(Module &M) {
Jack Carterc1b17ed2013-01-18 21:20:38 +0000681 // Emit Mips ELF register info
682 Subtarget->getMReginfo().emitMipsReginfoSectionCG(
683 OutStreamer, getObjFileLowering(), *Subtarget);
Rafael Espindola14d02fe2014-01-25 15:06:56 +0000684 emitELFHeaderFlagsCG(getTargetStreamer(), *Subtarget);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000685}
686
Akira Hatanakaf2bcad92011-07-01 01:04:43 +0000687void MipsAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
688 raw_ostream &OS) {
689 // TODO: implement
690}
691
Bob Wilson5a495fe2009-06-23 23:59:40 +0000692// Force static initialization.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000693extern "C" void LLVMInitializeMipsAsmPrinter() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +0000694 RegisterAsmPrinter<MipsAsmPrinter> X(TheMipsTarget);
695 RegisterAsmPrinter<MipsAsmPrinter> Y(TheMipselTarget);
Akira Hatanaka3d673cc2011-09-21 03:00:58 +0000696 RegisterAsmPrinter<MipsAsmPrinter> A(TheMips64Target);
697 RegisterAsmPrinter<MipsAsmPrinter> B(TheMips64elTarget);
Daniel Dunbare8338102009-07-15 20:24:03 +0000698}