blob: 8ffc0731abcb42b66eb1d6f2e69f418b68053584 [file] [log] [blame]
Eugene Zelenko79220eae2017-08-03 22:12:30 +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
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "MipsAsmPrinter.h"
Akira Hatanaka9c6028f2011-07-07 23:56:50 +000016#include "InstPrinter/MipsInstPrinter.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000017#include "MCTargetDesc/MipsABIInfo.h"
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000018#include "MCTargetDesc/MipsBaseInfo.h"
Sasa Stankovic8c5736b2014-02-28 10:00:38 +000019#include "MCTargetDesc/MipsMCNaCl.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000020#include "MCTargetDesc/MipsMCTargetDesc.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "Mips.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "MipsMCInstLower.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000023#include "MipsMachineFunction.h"
24#include "MipsSubtarget.h"
Eric Christophera5762812015-01-26 17:33:46 +000025#include "MipsTargetMachine.h"
Rafael Espindolaa17151a2013-10-08 13:08:17 +000026#include "MipsTargetStreamer.h"
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +000027#include "llvm/ADT/SmallString.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000028#include "llvm/ADT/StringRef.h"
29#include "llvm/ADT/Triple.h"
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +000030#include "llvm/ADT/Twine.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000031#include "llvm/BinaryFormat/ELF.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000032#include "llvm/CodeGen/MachineBasicBlock.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000033#include "llvm/CodeGen/MachineConstantPool.h"
Bruno Cardoso Lopesbcda5e22007-07-11 23:24:41 +000034#include "llvm/CodeGen/MachineFrameInfo.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000035#include "llvm/CodeGen/MachineFunction.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000036#include "llvm/CodeGen/MachineInstr.h"
Sasa Stankovic8c5736b2014-02-28 10:00:38 +000037#include "llvm/CodeGen/MachineJumpTableInfo.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000038#include "llvm/CodeGen/MachineOperand.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000039#include "llvm/CodeGen/TargetRegisterInfo.h"
40#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000041#include "llvm/IR/Attributes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000042#include "llvm/IR/BasicBlock.h"
43#include "llvm/IR/DataLayout.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000044#include "llvm/IR/Function.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000045#include "llvm/IR/InlineAsm.h"
46#include "llvm/IR/Instructions.h"
Rafael Espindola2ab7ea72014-01-27 01:33:33 +000047#include "llvm/MC/MCContext.h"
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000048#include "llvm/MC/MCExpr.h"
Akira Hatanaka9c6028f2011-07-07 23:56:50 +000049#include "llvm/MC/MCInst.h"
Sagar Thakurec657922017-02-15 10:48:11 +000050#include "llvm/MC/MCInstBuilder.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000051#include "llvm/MC/MCObjectFileInfo.h"
Rafael Espindola2ab7ea72014-01-27 01:33:33 +000052#include "llvm/MC/MCSectionELF.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000053#include "llvm/MC/MCSymbol.h"
Rafael Espindolaa8695762015-06-02 00:25:12 +000054#include "llvm/MC/MCSymbolELF.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000055#include "llvm/Support/Casting.h"
56#include "llvm/Support/ErrorHandling.h"
Jack Carterb2af5122012-07-05 23:58:21 +000057#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000058#include "llvm/Support/raw_ostream.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000059#include "llvm/Target/TargetMachine.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000060#include <cassert>
61#include <cstdint>
62#include <map>
63#include <memory>
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000064#include <string>
Eugene Zelenko79220eae2017-08-03 22:12:30 +000065#include <vector>
Akira Hatanakaf2bcad92011-07-01 01:04:43 +000066
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000067using namespace llvm;
68
Chandler Carruth84e68b22014-04-22 02:41:26 +000069#define DEBUG_TYPE "mips-asm-printer"
70
Toma Tabacua23f13c2014-12-17 10:56:16 +000071MipsTargetStreamer &MipsAsmPrinter::getTargetStreamer() const {
Lang Hames9ff69c82015-04-24 19:11:51 +000072 return static_cast<MipsTargetStreamer &>(*OutStreamer->getTargetStreamer());
Rafael Espindolaa17151a2013-10-08 13:08:17 +000073}
74
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000075bool MipsAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Eric Christopher3ee30d02015-02-20 08:39:06 +000076 Subtarget = &MF.getSubtarget<MipsSubtarget>();
Eric Christopher8ef7a6a2014-07-18 00:08:53 +000077
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000078 MipsFI = MF.getInfo<MipsFunctionInfo>();
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000079 if (Subtarget->inMips16Mode())
80 for (std::map<
81 const char *,
Eugene Zelenko79220eae2017-08-03 22:12:30 +000082 const Mips16HardFloatInfo::FuncSignature *>::const_iterator
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000083 it = MipsFI->StubsNeeded.begin();
84 it != MipsFI->StubsNeeded.end(); ++it) {
85 const char *Symbol = it->first;
Eugene Zelenko79220eae2017-08-03 22:12:30 +000086 const Mips16HardFloatInfo::FuncSignature *Signature = it->second;
Reed Kotler4cdaa7d2014-02-14 19:16:39 +000087 if (StubsNeeded.find(Symbol) == StubsNeeded.end())
88 StubsNeeded[Symbol] = Signature;
89 }
Reed Kotler91ae9822013-10-27 21:57:36 +000090 MCP = MF.getConstantPool();
Sasa Stankovic8c5736b2014-02-28 10:00:38 +000091
92 // In NaCl, all indirect jump targets must be aligned to bundle size.
93 if (Subtarget->isTargetNaCl())
94 NaClAlignIndirectJumpTargets(MF);
95
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +000096 AsmPrinter::runOnMachineFunction(MF);
Sagar Thakurec657922017-02-15 10:48:11 +000097
Simon Dardis080d4782017-05-04 11:03:50 +000098 emitXRayTable();
Sagar Thakurec657922017-02-15 10:48:11 +000099
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +0000100 return true;
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000101}
102
Akira Hatanaka42a35242012-09-27 01:59:07 +0000103bool MipsAsmPrinter::lowerOperand(const MachineOperand &MO, MCOperand &MCOp) {
104 MCOp = MCInstLowering.LowerOperand(MO);
105 return MCOp.isValid();
106}
107
108#include "MipsGenMCPseudoLowering.inc"
109
Daniel Sandersf5a5fbd2014-07-09 10:21:59 +0000110// Lower PseudoReturn/PseudoIndirectBranch/PseudoIndirectBranch64 to JR, JR_MM,
Aleksandar Beserminji7d610f42017-09-14 14:34:04 +0000111// JALR, or JALR64 as appropriate for the target.
Daniel Sandersf5a5fbd2014-07-09 10:21:59 +0000112void MipsAsmPrinter::emitPseudoIndirectBranch(MCStreamer &OutStreamer,
113 const MachineInstr *MI) {
Daniel Sanders338513b2014-07-09 10:16:07 +0000114 bool HasLinkReg = false;
Simon Dardisea343152016-08-18 13:22:43 +0000115 bool InMicroMipsMode = Subtarget->inMicroMipsMode();
Daniel Sanders338513b2014-07-09 10:16:07 +0000116 MCInst TmpInst0;
117
118 if (Subtarget->hasMips64r6()) {
119 // MIPS64r6 should use (JALR64 ZERO_64, $rs)
120 TmpInst0.setOpcode(Mips::JALR64);
121 HasLinkReg = true;
122 } else if (Subtarget->hasMips32r6()) {
123 // MIPS32r6 should use (JALR ZERO, $rs)
Simon Dardisea343152016-08-18 13:22:43 +0000124 if (InMicroMipsMode)
125 TmpInst0.setOpcode(Mips::JRC16_MMR6);
126 else {
127 TmpInst0.setOpcode(Mips::JALR);
128 HasLinkReg = true;
129 }
Daniel Sanders338513b2014-07-09 10:16:07 +0000130 } else if (Subtarget->inMicroMipsMode())
131 // microMIPS should use (JR_MM $rs)
132 TmpInst0.setOpcode(Mips::JR_MM);
133 else {
134 // Everything else should use (JR $rs)
135 TmpInst0.setOpcode(Mips::JR);
136 }
137
138 MCOperand MCOp;
139
140 if (HasLinkReg) {
141 unsigned ZeroReg = Subtarget->isGP64bit() ? Mips::ZERO_64 : Mips::ZERO;
Jim Grosbache9119e42015-05-13 18:37:00 +0000142 TmpInst0.addOperand(MCOperand::createReg(ZeroReg));
Daniel Sanders338513b2014-07-09 10:16:07 +0000143 }
144
145 lowerOperand(MI->getOperand(0), MCOp);
146 TmpInst0.addOperand(MCOp);
147
148 EmitToStreamer(OutStreamer, TmpInst0);
149}
150
Akira Hatanakaddd12652011-07-07 20:10:52 +0000151void MipsAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Vladimir Medicfb8a2a92014-07-08 08:59:22 +0000152 MipsTargetStreamer &TS = getTargetStreamer();
Sagar Thakurec657922017-02-15 10:48:11 +0000153 unsigned Opc = MI->getOpcode();
Daniel Sanderscdb45fa2014-08-14 09:18:14 +0000154 TS.forbidModuleDirective();
Daniel Sandersc7dbc632014-07-08 10:11:38 +0000155
Akira Hatanakaddd12652011-07-07 20:10:52 +0000156 if (MI->isDebugValue()) {
Bruno Cardoso Lopescd1d4472011-12-30 21:09:41 +0000157 SmallString<128> Str;
158 raw_svector_ostream OS(Str);
159
Akira Hatanakaddd12652011-07-07 20:10:52 +0000160 PrintDebugValueComment(MI, OS);
161 return;
162 }
Shiva Chen801bf7e2018-05-09 02:42:00 +0000163 if (MI->isDebugLabel())
164 return;
Akira Hatanakaddd12652011-07-07 20:10:52 +0000165
Reed Kotler91ae9822013-10-27 21:57:36 +0000166 // If we just ended a constant pool, mark it as such.
Sagar Thakurec657922017-02-15 10:48:11 +0000167 if (InConstantPool && Opc != Mips::CONSTPOOL_ENTRY) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000168 OutStreamer->EmitDataRegion(MCDR_DataRegionEnd);
Reed Kotler91ae9822013-10-27 21:57:36 +0000169 InConstantPool = false;
170 }
Sagar Thakurec657922017-02-15 10:48:11 +0000171 if (Opc == Mips::CONSTPOOL_ENTRY) {
Reed Kotler91ae9822013-10-27 21:57:36 +0000172 // CONSTPOOL_ENTRY - This instruction represents a floating
Sagar Thakurec657922017-02-15 10:48:11 +0000173 // constant pool in the function. The first operand is the ID#
Reed Kotler91ae9822013-10-27 21:57:36 +0000174 // for this instruction, the second is the index into the
175 // MachineConstantPool that this is, the third is the size in
176 // bytes of this constant pool entry.
177 // The required alignment is specified on the basic block holding this MI.
178 //
179 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
Sagar Thakurec657922017-02-15 10:48:11 +0000180 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
Reed Kotler91ae9822013-10-27 21:57:36 +0000181
182 // If this is the first entry of the pool, mark it.
183 if (!InConstantPool) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000184 OutStreamer->EmitDataRegion(MCDR_DataRegion);
Reed Kotler91ae9822013-10-27 21:57:36 +0000185 InConstantPool = true;
186 }
187
Lang Hames9ff69c82015-04-24 19:11:51 +0000188 OutStreamer->EmitLabel(GetCPISymbol(LabelId));
Reed Kotler91ae9822013-10-27 21:57:36 +0000189
190 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
191 if (MCPE.isMachineConstantPoolEntry())
192 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
193 else
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000194 EmitGlobalConstant(MF->getDataLayout(), MCPE.Val.ConstVal);
Reed Kotler91ae9822013-10-27 21:57:36 +0000195 return;
196 }
197
Sagar Thakurec657922017-02-15 10:48:11 +0000198 switch (Opc) {
199 case Mips::PATCHABLE_FUNCTION_ENTER:
200 LowerPATCHABLE_FUNCTION_ENTER(*MI);
201 return;
202 case Mips::PATCHABLE_FUNCTION_EXIT:
203 LowerPATCHABLE_FUNCTION_EXIT(*MI);
204 return;
205 case Mips::PATCHABLE_TAIL_CALL:
206 LowerPATCHABLE_TAIL_CALL(*MI);
207 return;
208 }
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000209
210 MachineBasicBlock::const_instr_iterator I = MI->getIterator();
211 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
Akira Hatanaka5ac78682012-06-13 23:25:52 +0000212
213 do {
Akira Hatanaka556135d2013-02-06 21:50:15 +0000214 // Do any auto-generated pseudo lowerings.
Lang Hames9ff69c82015-04-24 19:11:51 +0000215 if (emitPseudoExpansionLowering(*OutStreamer, &*I))
Akira Hatanaka556135d2013-02-06 21:50:15 +0000216 continue;
Jack Carterc20a21b2012-08-28 19:07:39 +0000217
Daniel Sanders338513b2014-07-09 10:16:07 +0000218 if (I->getOpcode() == Mips::PseudoReturn ||
Daniel Sandersf5a5fbd2014-07-09 10:21:59 +0000219 I->getOpcode() == Mips::PseudoReturn64 ||
220 I->getOpcode() == Mips::PseudoIndirectBranch ||
Simon Dardisea343152016-08-18 13:22:43 +0000221 I->getOpcode() == Mips::PseudoIndirectBranch64 ||
222 I->getOpcode() == Mips::TAILCALLREG ||
223 I->getOpcode() == Mips::TAILCALLREG64) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000224 emitPseudoIndirectBranch(*OutStreamer, &*I);
Daniel Sanders338513b2014-07-09 10:16:07 +0000225 continue;
226 }
227
Reed Kotler76c9bcd2013-02-15 21:05:58 +0000228 // The inMips16Mode() test is not permanent.
229 // Some instructions are marked as pseudo right now which
230 // would make the test fail for the wrong reason but
231 // that will be fixed soon. We need this here because we are
232 // removing another test for this situation downstream in the
233 // callchain.
234 //
Sasa Stankovic7b061a42014-04-30 15:06:25 +0000235 if (I->isPseudo() && !Subtarget->inMips16Mode()
236 && !isLongBranchPseudo(I->getOpcode()))
Reed Kotler76c9bcd2013-02-15 21:05:58 +0000237 llvm_unreachable("Pseudo opcode found in EmitInstruction()");
238
Akira Hatanaka556135d2013-02-06 21:50:15 +0000239 MCInst TmpInst0;
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +0000240 MCInstLowering.Lower(&*I, TmpInst0);
Lang Hames9ff69c82015-04-24 19:11:51 +0000241 EmitToStreamer(*OutStreamer, TmpInst0);
Akira Hatanaka556135d2013-02-06 21:50:15 +0000242 } while ((++I != E) && I->isInsideBundle()); // Delay slot check
Akira Hatanakaddd12652011-07-07 20:10:52 +0000243}
244
Akira Hatanakae2489122011-04-15 21:51:11 +0000245//===----------------------------------------------------------------------===//
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000246//
247// Mips Asm Directives
248//
249// -- Frame directive "frame Stackpointer, Stacksize, RARegister"
250// Describe the stack frame.
251//
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000252// -- Mask directives "(f)mask bitmask, offset"
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000253// Tells the assembler which registers are saved and where.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000254// bitmask - contain a little endian bitset indicating which registers are
255// saved on function prologue (e.g. with a 0x80000000 mask, the
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000256// assembler knows the register 31 (RA) is saved at prologue.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000257// offset - the position before stack pointer subtraction indicating where
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000258// the first saved register on prologue is located. (e.g. with a
259//
260// Consider the following function prologue:
261//
Bill Wendling97925ec2008-02-27 06:33:05 +0000262// .frame $fp,48,$ra
263// .mask 0xc0000000,-8
264// addiu $sp, $sp, -48
265// sw $ra, 40($sp)
266// sw $fp, 36($sp)
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000267//
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000268// With a 0xc0000000 mask, the assembler knows the register 31 (RA) and
269// 30 (FP) are saved at prologue. As the save order on prologue is from
270// left to right, RA is saved first. A -8 offset means that after the
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000271// stack pointer subtration, the first register in the mask (RA) will be
272// saved at address 48-8=40.
273//
Akira Hatanakae2489122011-04-15 21:51:11 +0000274//===----------------------------------------------------------------------===//
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000275
Akira Hatanakae2489122011-04-15 21:51:11 +0000276//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000277// Mask directives
Akira Hatanakae2489122011-04-15 21:51:11 +0000278//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000279
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000280// Create a bitmask with all callee saved registers for CPU or Floating Point
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000281// registers. For CPU registers consider RA, GP and FP for saving if necessary.
Rafael Espindola25fa2912014-01-27 04:33:11 +0000282void MipsAsmPrinter::printSavedRegsBitmask() {
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000283 // CPU and FPU Saved Registers Bitmasks
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000284 unsigned CPUBitmask = 0, FPUBitmask = 0;
285 int CPUTopSavedRegOff, FPUTopSavedRegOff;
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000286
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000287 // Set the CPU and FPU Bitmasks
Matthias Braun941a7052016-07-28 18:40:00 +0000288 const MachineFrameInfo &MFI = MF->getFrameInfo();
Eric Christophercba722f2015-03-21 03:13:07 +0000289 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
Matthias Braun941a7052016-07-28 18:40:00 +0000290 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000291 // size of stack area to which FP callee-saved regs are saved.
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +0000292 unsigned CPURegSize = TRI->getRegSizeInBits(Mips::GPR32RegClass) / 8;
293 unsigned FGR32RegSize = TRI->getRegSizeInBits(Mips::FGR32RegClass) / 8;
294 unsigned AFGR64RegSize = TRI->getRegSizeInBits(Mips::AFGR64RegClass) / 8;
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000295 bool HasAFGR64Reg = false;
296 unsigned CSFPRegsSize = 0;
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000297
Toma Tabacube218922015-04-09 10:54:16 +0000298 for (const auto &I : CSI) {
299 unsigned Reg = I.getReg();
Eric Christophercba722f2015-03-21 03:13:07 +0000300 unsigned RegNum = TRI->getEncodingValue(Reg);
Toma Tabacube218922015-04-09 10:54:16 +0000301
302 // If it's a floating point register, set the FPU Bitmask.
303 // If it's a general purpose register, set the CPU Bitmask.
304 if (Mips::FGR32RegClass.contains(Reg)) {
305 FPUBitmask |= (1 << RegNum);
306 CSFPRegsSize += FGR32RegSize;
307 } else if (Mips::AFGR64RegClass.contains(Reg)) {
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000308 FPUBitmask |= (3 << RegNum);
309 CSFPRegsSize += AFGR64RegSize;
310 HasAFGR64Reg = true;
Toma Tabacube218922015-04-09 10:54:16 +0000311 } else if (Mips::GPR32RegClass.contains(Reg))
312 CPUBitmask |= (1 << RegNum);
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000313 }
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000314
Akira Hatanaka90d96f42011-05-23 20:34:30 +0000315 // FP Regs are saved right below where the virtual frame pointer points to.
316 FPUTopSavedRegOff = FPUBitmask ?
317 (HasAFGR64Reg ? -AFGR64RegSize : -FGR32RegSize) : 0;
318
319 // CPU Regs are saved below FP Regs.
320 CPUTopSavedRegOff = CPUBitmask ? -CSFPRegsSize - CPURegSize : 0;
Bruno Cardoso Lopescfd16382007-08-28 05:06:17 +0000321
Rafael Espindola25fa2912014-01-27 04:33:11 +0000322 MipsTargetStreamer &TS = getTargetStreamer();
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000323 // Print CPUBitmask
Rafael Espindola25fa2912014-01-27 04:33:11 +0000324 TS.emitMask(CPUBitmask, CPUTopSavedRegOff);
Bruno Cardoso Lopes4659aad2008-08-06 06:14:43 +0000325
326 // Print FPUBitmask
Rafael Espindola25fa2912014-01-27 04:33:11 +0000327 TS.emitFMask(FPUBitmask, FPUTopSavedRegOff);
Bruno Cardoso Lopesbcda5e22007-07-11 23:24:41 +0000328}
329
Akira Hatanakae2489122011-04-15 21:51:11 +0000330//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000331// Frame and Set directives
Akira Hatanakae2489122011-04-15 21:51:11 +0000332//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000333
334/// Frame Directive
Chris Lattner5e596182010-04-04 07:05:53 +0000335void MipsAsmPrinter::emitFrameDirective() {
Eric Christophercba722f2015-03-21 03:13:07 +0000336 const TargetRegisterInfo &RI = *MF->getSubtarget().getRegisterInfo();
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000337
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000338 unsigned stackReg = RI.getFrameRegister(*MF);
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000339 unsigned returnReg = RI.getRARegister();
Matthias Braun941a7052016-07-28 18:40:00 +0000340 unsigned stackSize = MF->getFrameInfo().getStackSize();
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000341
Rafael Espindola054234f2014-01-27 03:53:56 +0000342 getTargetStreamer().emitFrame(stackReg, stackSize, returnReg);
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000343}
344
345/// Emit Set directives.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000346const char *MipsAsmPrinter::getCurrentABIString() const {
Eric Christophera5762812015-01-26 17:33:46 +0000347 switch (static_cast<MipsTargetMachine &>(TM).getABI().GetEnumValue()) {
Daniel Sanderse2e25da2014-10-24 16:15:27 +0000348 case MipsABIInfo::ABI::O32: return "abi32";
349 case MipsABIInfo::ABI::N32: return "abiN32";
350 case MipsABIInfo::ABI::N64: return "abi64";
Dmitri Gribenkoca1e27b2012-09-10 21:26:47 +0000351 default: llvm_unreachable("Unknown Mips ABI");
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000352 }
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000353}
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000354
Chris Lattner5d9fb4b2010-01-27 23:23:58 +0000355void MipsAsmPrinter::EmitFunctionEntryLabel() {
Rafael Espindola6633d572014-01-14 18:57:12 +0000356 MipsTargetStreamer &TS = getTargetStreamer();
Sasa Stankovic8c5736b2014-02-28 10:00:38 +0000357
358 // NaCl sandboxing requires that indirect call instructions are masked.
359 // This means that function entry points should be bundle-aligned.
360 if (Subtarget->isTargetNaCl())
361 EmitAlignment(std::max(MF->getAlignment(), MIPS_NACL_BUNDLE_ALIGN));
362
Daniel Sanders1d148642016-06-16 09:17:03 +0000363 if (Subtarget->inMicroMipsMode()) {
Rafael Espindola6633d572014-01-14 18:57:12 +0000364 TS.emitDirectiveSetMicroMips();
Daniel Sanders1d148642016-06-16 09:17:03 +0000365 TS.setUsesMicroMips();
Aleksandar Beserminji590f0792017-11-24 14:00:47 +0000366 TS.updateABIInfo(*Subtarget);
Daniel Sanders1d148642016-06-16 09:17:03 +0000367 } else
Matheus Almeidadc7e48e2014-04-16 11:46:59 +0000368 TS.emitDirectiveSetNoMicroMips();
Rafael Espindola6d5f7ce2014-01-14 04:25:13 +0000369
Rafael Espindola6633d572014-01-14 18:57:12 +0000370 if (Subtarget->inMips16Mode())
371 TS.emitDirectiveSetMips16();
372 else
373 TS.emitDirectiveSetNoMips16();
Jack Carterab3cb422013-02-19 22:04:37 +0000374
Rafael Espindola6633d572014-01-14 18:57:12 +0000375 TS.emitDirectiveEnt(*CurrentFnSym);
Lang Hames9ff69c82015-04-24 19:11:51 +0000376 OutStreamer->EmitLabel(CurrentFnSym);
Chris Lattner5d9fb4b2010-01-27 23:23:58 +0000377}
378
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000379/// EmitFunctionBodyStart - Targets can override this to emit stuff before
380/// the first basic block in the function.
381void MipsAsmPrinter::EmitFunctionBodyStart() {
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000382 MipsTargetStreamer &TS = getTargetStreamer();
383
Rafael Espindola7d78b2a2013-10-29 16:24:21 +0000384 MCInstLowering.Initialize(&MF->getContext());
Akira Hatanaka34ee3ff2012-03-28 00:22:50 +0000385
Matthias Braunf1caa282017-12-15 22:22:58 +0000386 bool IsNakedFunction = MF->getFunction().hasFnAttribute(Attribute::Naked);
Reed Kotler0f2b10e2013-05-03 23:17:24 +0000387 if (!IsNakedFunction)
388 emitFrameDirective();
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000389
Rafael Espindola25fa2912014-01-27 04:33:11 +0000390 if (!IsNakedFunction)
391 printSavedRegsBitmask();
392
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000393 if (!Subtarget->inMips16Mode()) {
394 TS.emitDirectiveSetNoReorder();
395 TS.emitDirectiveSetNoMacro();
396 TS.emitDirectiveSetNoAt();
Akira Hatanaka8f3573032012-05-12 00:48:43 +0000397 }
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000398}
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000399
Chris Lattnercc9a6f02010-01-28 06:22:43 +0000400/// EmitFunctionBodyEnd - Targets can override this to emit stuff after
401/// the last basic block in the function.
402void MipsAsmPrinter::EmitFunctionBodyEnd() {
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000403 MipsTargetStreamer &TS = getTargetStreamer();
404
Chris Lattnerfd97a332010-01-28 01:48:52 +0000405 // There are instruction for this macros, but they must
406 // always be at the function end, and we can't emit and
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000407 // break with BB logic.
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000408 if (!Subtarget->inMips16Mode()) {
409 TS.emitDirectiveSetAt();
410 TS.emitDirectiveSetMacro();
411 TS.emitDirectiveSetReorder();
Bruno Cardoso Lopesd5edb382011-11-08 22:26:47 +0000412 }
Rafael Espindolaeb0a8af2014-01-26 05:06:48 +0000413 TS.emitDirectiveEnd(CurrentFnSym->getName());
Reed Kotler91ae9822013-10-27 21:57:36 +0000414 // Make sure to terminate any constant pools that were at the end
415 // of the function.
416 if (!InConstantPool)
417 return;
418 InConstantPool = false;
Lang Hames9ff69c82015-04-24 19:11:51 +0000419 OutStreamer->EmitDataRegion(MCDR_DataRegionEnd);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000420}
421
Vasileios Kalintiris42544d62015-05-08 09:10:15 +0000422void MipsAsmPrinter::EmitBasicBlockEnd(const MachineBasicBlock &MBB) {
Omer Paparo Bivas2251c792017-10-24 06:16:03 +0000423 AsmPrinter::EmitBasicBlockEnd(MBB);
Vasileios Kalintiris42544d62015-05-08 09:10:15 +0000424 MipsTargetStreamer &TS = getTargetStreamer();
Eugene Zelenko79220eae2017-08-03 22:12:30 +0000425 if (MBB.empty())
Vasileios Kalintiris42544d62015-05-08 09:10:15 +0000426 TS.emitDirectiveInsn();
427}
428
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000429/// isBlockOnlyReachableByFallthough - Return true if the basic block has
430/// exactly one predecessor and the control transfer mechanism between
431/// the predecessor and this block is a fall-through.
Akira Hatanakae2489122011-04-15 21:51:11 +0000432bool MipsAsmPrinter::isBlockOnlyReachableByFallthrough(const MachineBasicBlock*
433 MBB) const {
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000434 // The predecessor has to be immediately before this block.
435 const MachineBasicBlock *Pred = *MBB->pred_begin();
436
437 // If the predecessor is a switch statement, assume a jump table
438 // implementation, so it is not a fall through.
439 if (const BasicBlock *bb = Pred->getBasicBlock())
440 if (isa<SwitchInst>(bb->getTerminator()))
441 return false;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000442
Akira Hatanakae625ba42011-04-01 18:57:38 +0000443 // If this is a landing pad, it isn't a fall through. If it has no preds,
444 // then nothing falls through to it.
Reid Kleckner0e288232015-08-27 23:27:47 +0000445 if (MBB->isEHPad() || MBB->pred_empty())
Akira Hatanakae625ba42011-04-01 18:57:38 +0000446 return false;
447
448 // If there isn't exactly one predecessor, it can't be a fall through.
449 MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI;
450 ++PI2;
Jia Liuf54f60f2012-02-28 07:46:26 +0000451
Akira Hatanakae625ba42011-04-01 18:57:38 +0000452 if (PI2 != MBB->pred_end())
Jia Liuf54f60f2012-02-28 07:46:26 +0000453 return false;
Akira Hatanakae625ba42011-04-01 18:57:38 +0000454
455 // The predecessor has to be immediately before this block.
456 if (!Pred->isLayoutSuccessor(MBB))
457 return false;
Jia Liuf54f60f2012-02-28 07:46:26 +0000458
Akira Hatanakae625ba42011-04-01 18:57:38 +0000459 // If the block is completely empty, then it definitely does fall through.
460 if (Pred->empty())
461 return true;
Jia Liuf54f60f2012-02-28 07:46:26 +0000462
Akira Hatanakae625ba42011-04-01 18:57:38 +0000463 // Otherwise, check the last instruction.
464 // Check if the last terminator is an unconditional branch.
465 MachineBasicBlock::const_iterator I = Pred->end();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000466 while (I != Pred->begin() && !(--I)->isTerminator()) ;
Akira Hatanakae625ba42011-04-01 18:57:38 +0000467
Evan Cheng7f8e5632011-12-07 07:15:52 +0000468 return !I->isBarrier();
Bruno Cardoso Lopes160695f2010-07-20 08:37:04 +0000469}
470
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000471// Print out an operand for an inline asm expression.
Eric Christophered51b9e2012-05-10 21:48:22 +0000472bool MipsAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000473 unsigned AsmVariant, const char *ExtraCode,
Chris Lattner3bb09762010-04-04 05:29:35 +0000474 raw_ostream &O) {
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000475 // Does this asm operand have a single letter operand modifier?
Eric Christophered51b9e2012-05-10 21:48:22 +0000476 if (ExtraCode && ExtraCode[0]) {
477 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000478
Eric Christophered51b9e2012-05-10 21:48:22 +0000479 const MachineOperand &MO = MI->getOperand(OpNum);
480 switch (ExtraCode[0]) {
Eric Christopherbc5d2492012-05-19 00:51:56 +0000481 default:
Jack Carterb2fd5f62012-06-21 17:14:46 +0000482 // See if this is a generic print operand
483 return AsmPrinter::PrintAsmOperand(MI,OpNum,AsmVariant,ExtraCode,O);
Eric Christopherbc5d2492012-05-19 00:51:56 +0000484 case 'X': // hex const int
485 if ((MO.getType()) != MachineOperand::MO_Immediate)
486 return true;
Benjamin Kramer33b46912015-05-23 16:53:07 +0000487 O << "0x" << Twine::utohexstr(MO.getImm());
Eric Christopherbc5d2492012-05-19 00:51:56 +0000488 return false;
489 case 'x': // hex const int (low 16 bits)
490 if ((MO.getType()) != MachineOperand::MO_Immediate)
491 return true;
Benjamin Kramer33b46912015-05-23 16:53:07 +0000492 O << "0x" << Twine::utohexstr(MO.getImm() & 0xffff);
Eric Christopherbc5d2492012-05-19 00:51:56 +0000493 return false;
494 case 'd': // decimal const int
495 if ((MO.getType()) != MachineOperand::MO_Immediate)
496 return true;
497 O << MO.getImm();
498 return false;
Eric Christopherf481ab32012-05-30 19:05:19 +0000499 case 'm': // decimal const int minus 1
500 if ((MO.getType()) != MachineOperand::MO_Immediate)
501 return true;
502 O << MO.getImm() - 1;
503 return false;
Simon Atanasyan70498f82018-02-07 12:36:39 +0000504 case 'y': // exact log2
505 if ((MO.getType()) != MachineOperand::MO_Immediate)
506 return true;
507 if (!isPowerOf2_64(MO.getImm()))
508 return true;
509 O << Log2_64(MO.getImm());
510 return false;
Eugene Zelenko79220eae2017-08-03 22:12:30 +0000511 case 'z':
Jack Carter27747b52012-06-28 20:46:26 +0000512 // $0 if zero, regular printing otherwise
Toma Tabacu27cab752014-11-06 14:25:42 +0000513 if (MO.getType() == MachineOperand::MO_Immediate && MO.getImm() == 0) {
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000514 O << "$0";
Toma Tabacu27cab752014-11-06 14:25:42 +0000515 return false;
516 }
517 // If not, call printOperand as normal.
518 break;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000519 case 'D': // Second part of a double word register operand
520 case 'L': // Low order register of a double word register operand
Jack Cartera62ba822012-07-18 06:41:36 +0000521 case 'M': // High order register of a double word register operand
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000522 {
Jack Carterb2af5122012-07-05 23:58:21 +0000523 if (OpNum == 0)
524 return true;
525 const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
526 if (!FlagsOP.isImm())
527 return true;
528 unsigned Flags = FlagsOP.getImm();
529 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Jack Carter2ab73b12012-07-06 02:44:22 +0000530 // Number of registers represented by this operand. We are looking
531 // for 2 for 32 bit mode and 1 for 64 bit mode.
Jack Carterb2af5122012-07-05 23:58:21 +0000532 if (NumVals != 2) {
Jack Carter2ab73b12012-07-06 02:44:22 +0000533 if (Subtarget->isGP64bit() && NumVals == 1 && MO.isReg()) {
Jack Carterb2af5122012-07-05 23:58:21 +0000534 unsigned Reg = MO.getReg();
535 O << '$' << MipsInstPrinter::getRegisterName(Reg);
536 return false;
537 }
538 return true;
539 }
Jack Carter42ebf982012-07-11 21:41:49 +0000540
541 unsigned RegOp = OpNum;
542 if (!Subtarget->isGP64bit()){
Simon Pilgrimdcd84332016-11-18 11:53:36 +0000543 // Endianness reverses which register holds the high or low value
Jack Cartera62ba822012-07-18 06:41:36 +0000544 // between M and L.
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000545 switch(ExtraCode[0]) {
Jack Cartera62ba822012-07-18 06:41:36 +0000546 case 'M':
547 RegOp = (Subtarget->isLittle()) ? OpNum + 1 : OpNum;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000548 break;
549 case 'L':
Jack Cartera62ba822012-07-18 06:41:36 +0000550 RegOp = (Subtarget->isLittle()) ? OpNum : OpNum + 1;
551 break;
552 case 'D': // Always the second part
553 RegOp = OpNum + 1;
Jack Cartere8cb2fc2012-07-10 22:41:20 +0000554 }
555 if (RegOp >= MI->getNumOperands())
556 return true;
557 const MachineOperand &MO = MI->getOperand(RegOp);
558 if (!MO.isReg())
559 return true;
560 unsigned Reg = MO.getReg();
561 O << '$' << MipsInstPrinter::getRegisterName(Reg);
562 return false;
Jack Carterb2af5122012-07-05 23:58:21 +0000563 }
Eric Christophered51b9e2012-05-10 21:48:22 +0000564 }
Daniel Sanders8b59af12013-11-12 12:56:01 +0000565 case 'w':
566 // Print MSA registers for the 'f' constraint
567 // In LLVM, the 'w' modifier doesn't need to do anything.
568 // We can just call printOperand as normal.
569 break;
Jack Carter2ab73b12012-07-06 02:44:22 +0000570 }
571 }
Eric Christophered51b9e2012-05-10 21:48:22 +0000572
573 printOperand(MI, OpNum, O);
Bruno Cardoso Lopes3d4bdcc2008-08-02 19:42:36 +0000574 return false;
575}
576
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000577bool MipsAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
578 unsigned OpNum, unsigned AsmVariant,
579 const char *ExtraCode,
580 raw_ostream &O) {
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000581 assert(OpNum + 1 < MI->getNumOperands() && "Insufficient operands");
582 const MachineOperand &BaseMO = MI->getOperand(OpNum);
583 const MachineOperand &OffsetMO = MI->getOperand(OpNum + 1);
584 assert(BaseMO.isReg() && "Unexpected base pointer for inline asm memory operand.");
585 assert(OffsetMO.isImm() && "Unexpected offset for inline asm memory operand.");
586 int Offset = OffsetMO.getImm();
587
Simon Atanasyan737bec32018-02-07 12:36:33 +0000588 // Currently we are expecting either no ExtraCode or 'D','M','L'.
Jack Carterb04e3572013-04-09 23:19:50 +0000589 if (ExtraCode) {
Simon Atanasyan737bec32018-02-07 12:36:33 +0000590 switch (ExtraCode[0]) {
591 case 'D':
Daniel Sandersa73d8fe2015-03-24 11:26:34 +0000592 Offset += 4;
Simon Atanasyan737bec32018-02-07 12:36:33 +0000593 break;
594 case 'M':
595 if (Subtarget->isLittle())
596 Offset += 4;
597 break;
598 case 'L':
599 if (!Subtarget->isLittle())
600 Offset += 4;
601 break;
602 default:
Jack Carterb04e3572013-04-09 23:19:50 +0000603 return true; // Unknown modifier.
Simon Atanasyan737bec32018-02-07 12:36:33 +0000604 }
Jack Carterb04e3572013-04-09 23:19:50 +0000605 }
Jia Liuf54f60f2012-02-28 07:46:26 +0000606
Simon Atanasyan737bec32018-02-07 12:36:33 +0000607 O << Offset << "($" << MipsInstPrinter::getRegisterName(BaseMO.getReg())
608 << ")";
Jack Carter6c0bc0b2012-06-28 01:33:40 +0000609
Akira Hatanaka4c406e72011-06-21 00:40:49 +0000610 return false;
611}
612
Chris Lattner76c564b2010-04-04 04:47:45 +0000613void MipsAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
614 raw_ostream &O) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000615 const MachineOperand &MO = MI->getOperand(opNum);
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +0000616 bool closeP = false;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000617
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000618 if (MO.getTargetFlags())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000619 closeP = true;
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000620
621 switch(MO.getTargetFlags()) {
622 case MipsII::MO_GPREL: O << "%gp_rel("; break;
623 case MipsII::MO_GOT_CALL: O << "%call16("; break;
Akira Hatanaka56d9ef52011-04-01 21:41:06 +0000624 case MipsII::MO_GOT: O << "%got("; break;
625 case MipsII::MO_ABS_HI: O << "%hi("; break;
626 case MipsII::MO_ABS_LO: O << "%lo("; break;
Simon Dardisca74dd72017-01-27 11:36:52 +0000627 case MipsII::MO_HIGHER: O << "%higher("; break;
628 case MipsII::MO_HIGHEST: O << "%highest(("; break;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +0000629 case MipsII::MO_TLSGD: O << "%tlsgd("; break;
630 case MipsII::MO_GOTTPREL: O << "%gottprel("; break;
631 case MipsII::MO_TPREL_HI: O << "%tprel_hi("; break;
632 case MipsII::MO_TPREL_LO: O << "%tprel_lo("; break;
Akira Hatanaka25ce3642011-09-22 03:09:07 +0000633 case MipsII::MO_GPOFF_HI: O << "%hi(%neg(%gp_rel("; break;
634 case MipsII::MO_GPOFF_LO: O << "%lo(%neg(%gp_rel("; break;
635 case MipsII::MO_GOT_DISP: O << "%got_disp("; break;
636 case MipsII::MO_GOT_PAGE: O << "%got_page("; break;
637 case MipsII::MO_GOT_OFST: O << "%got_ofst("; break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000638 }
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +0000639
Chris Lattnereb2cc682009-09-13 20:31:40 +0000640 switch (MO.getType()) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000641 case MachineOperand::MO_Register:
Akira Hatanaka9c6028f2011-07-07 23:56:50 +0000642 O << '$'
Benjamin Kramer20baffb2011-11-06 20:37:06 +0000643 << StringRef(MipsInstPrinter::getRegisterName(MO.getReg())).lower();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000644 break;
645
646 case MachineOperand::MO_Immediate:
Akira Hatanaka2db176c2011-05-24 21:22:21 +0000647 O << MO.getImm();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000648 break;
649
650 case MachineOperand::MO_MachineBasicBlock:
Matt Arsenault8b643552015-06-09 00:31:39 +0000651 MO.getMBB()->getSymbol()->print(O, MAI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000652 return;
653
654 case MachineOperand::MO_GlobalAddress:
Matt Arsenault8b643552015-06-09 00:31:39 +0000655 getSymbol(MO.getGlobal())->print(O, MAI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000656 break;
657
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000658 case MachineOperand::MO_BlockAddress: {
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000659 MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000660 O << BA->getName();
661 break;
662 }
663
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000664 case MachineOperand::MO_ConstantPoolIndex:
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000665 O << getDataLayout().getPrivateGlobalPrefix() << "CPI"
Chris Lattnera5bb3702007-12-30 23:10:15 +0000666 << getFunctionNumber() << "_" << MO.getIndex();
Bruno Cardoso Lopes4713b282009-11-19 06:06:13 +0000667 if (MO.getOffset())
668 O << "+" << MO.getOffset();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000669 break;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000670
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000671 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000672 llvm_unreachable("<unknown operand type>");
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000673 }
674
675 if (closeP) O << ")";
676}
677
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000678void MipsAsmPrinter::
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000679printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O) {
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000680 // Load/Store memory operands -- imm($reg)
681 // If PIC target the target is loaded as the
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +0000682 // pattern lw $25,%call16($28)
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000683
684 // opNum can be invalid if instruction has reglist as operand.
685 // MemOperand is always last operand of instruction (base + offset).
686 switch (MI->getOpcode()) {
687 default:
688 break;
689 case Mips::SWM32_MM:
690 case Mips::LWM32_MM:
691 opNum = MI->getNumOperands() - 2;
692 break;
693 }
694
Chris Lattner76c564b2010-04-04 04:47:45 +0000695 printOperand(MI, opNum+1, O);
Akira Hatanaka2e766ed2011-07-07 18:57:00 +0000696 O << "(";
697 printOperand(MI, opNum, O);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000698 O << ")";
699}
700
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000701void MipsAsmPrinter::
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000702printMemOperandEA(const MachineInstr *MI, int opNum, raw_ostream &O) {
703 // when using stack locations for not load/store instructions
704 // print the same way as all normal 3 operand instructions.
705 printOperand(MI, opNum, O);
706 O << ", ";
707 printOperand(MI, opNum+1, O);
Akira Hatanaka9f6f6f62011-07-07 20:54:20 +0000708}
709
710void MipsAsmPrinter::
Simon Dardisba92b032016-09-09 11:06:01 +0000711printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
712 const char *Modifier) {
713 const MachineOperand &MO = MI->getOperand(opNum);
714 O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
715}
716
717void MipsAsmPrinter::
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000718printRegisterList(const MachineInstr *MI, int opNum, raw_ostream &O) {
719 for (int i = opNum, e = MI->getNumOperands(); i != e; ++i) {
720 if (i != opNum) O << ", ";
721 printOperand(MI, i, O);
722 }
723}
724
Bob Wilsonb633d7a2009-09-30 22:06:26 +0000725void MipsAsmPrinter::EmitStartOfAsmFile(Module &M) {
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000726 MipsTargetStreamer &TS = getTargetStreamer();
727
728 // MipsTargetStreamer has an initialization order problem when emitting an
729 // object file directly (see MipsTargetELFStreamer for full details). Work
730 // around it by re-initializing the PIC state here.
Rafael Espindola699281c2016-05-18 11:58:50 +0000731 TS.setPic(OutContext.getObjectFileInfo()->isPositionIndependent());
Eric Christopher8af49b32015-02-18 01:01:57 +0000732
733 // Compute MIPS architecture attributes based on the default subtarget
734 // that we'd have constructed. Module level directives aren't LTO
735 // clean anyhow.
736 // FIXME: For ifunc related functions we could iterate over and look
737 // for a feature string that doesn't match the default one.
Daniel Sanders50f17232015-09-15 16:17:27 +0000738 const Triple &TT = TM.getTargetTriple();
Daniel Sandersa73f1fd2015-06-10 12:11:26 +0000739 StringRef CPU = MIPS_MC::selectMipsCPU(TT, TM.getTargetCPU());
Eric Christopher8af49b32015-02-18 01:01:57 +0000740 StringRef FS = TM.getTargetFeatureString();
741 const MipsTargetMachine &MTM = static_cast<const MipsTargetMachine &>(TM);
John Baldwin1255b162017-08-14 21:49:38 +0000742 const MipsSubtarget STI(TT, CPU, FS, MTM.isLittleEndian(), MTM, 0);
Eric Christopher8af49b32015-02-18 01:01:57 +0000743
744 bool IsABICalls = STI.isABICalls();
745 const MipsABIInfo &ABI = MTM.getABI();
Daniel Sanders16fa1db2014-04-16 13:58:57 +0000746 if (IsABICalls) {
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000747 TS.emitDirectiveAbiCalls();
Daniel Sanders16fa1db2014-04-16 13:58:57 +0000748 // FIXME: This condition should be a lot more complicated that it is here.
749 // Ideally it should test for properties of the ABI and not the ABI
750 // itself.
751 // For the moment, I'm only correcting enough to make MIPS-IV work.
Simon Dardisca74dd72017-01-27 11:36:52 +0000752 if (!isPositionIndependent() && STI.hasSym32())
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000753 TS.emitDirectiveOptionPic0();
Daniel Sanders16fa1db2014-04-16 13:58:57 +0000754 }
Jack Carterf9f753c2013-06-18 19:47:15 +0000755
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000756 // Tell the assembler which ABI we are using
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000757 std::string SectionName = std::string(".mdebug.") + getCurrentABIString();
Lang Hames9ff69c82015-04-24 19:11:51 +0000758 OutStreamer->SwitchSection(
Rafael Espindolaba31e272015-01-29 17:33:21 +0000759 OutContext.getELFSection(SectionName, ELF::SHT_PROGBITS, 0));
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000760
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000761 // NaN: At the moment we only support:
762 // 1. .nan legacy (default)
763 // 2. .nan 2008
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000764 STI.isNaN2008() ? TS.emitDirectiveNaN2008()
765 : TS.emitDirectiveNaNLegacy();
Matheus Almeida0051f2d2014-04-16 15:48:55 +0000766
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000767 // TODO: handle O64 ABI
Rafael Espindola2ab7ea72014-01-27 01:33:33 +0000768
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000769 TS.updateABIInfo(STI);
Daniel Sanders7e527422014-07-10 13:38:23 +0000770
Daniel Sanderse22244b2014-07-21 15:25:24 +0000771 // We should always emit a '.module fp=...' but binutils 2.24 does not accept
772 // it. We therefore emit it when it contradicts the ABI defaults (-mfpxx or
773 // -mfp64) and omit it otherwise.
Eric Christopher8af49b32015-02-18 01:01:57 +0000774 if (ABI.IsO32() && (STI.isABI_FPXX() || STI.isFP64bit()))
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000775 TS.emitDirectiveModuleFP();
Daniel Sanderse22244b2014-07-21 15:25:24 +0000776
777 // We should always emit a '.module [no]oddspreg' but binutils 2.24 does not
778 // accept it. We therefore emit it when it contradicts the default or an
779 // option has changed the default (i.e. FPXX) and omit it otherwise.
Eric Christopher8af49b32015-02-18 01:01:57 +0000780 if (ABI.IsO32() && (!STI.useOddSPReg() || STI.isABI_FPXX()))
Daniel Sanders8de3d3c2016-05-06 14:37:24 +0000781 TS.emitDirectiveModuleOddSPReg();
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000782}
Bruno Cardoso Lopes80ab8f92008-07-14 14:42:54 +0000783
Eric Christopher64d35be2015-02-19 19:52:25 +0000784void MipsAsmPrinter::emitInlineAsmStart() const {
Toma Tabacua23f13c2014-12-17 10:56:16 +0000785 MipsTargetStreamer &TS = getTargetStreamer();
786
Toma Tabacu68e8a9c2015-01-09 15:00:30 +0000787 // GCC's choice of assembler options for inline assembly code ('at', 'macro'
788 // and 'reorder') is different from LLVM's choice for generated code ('noat',
789 // 'nomacro' and 'noreorder').
790 // In order to maintain compatibility with inline assembly code which depends
791 // on GCC's assembler options being used, we have to switch to those options
792 // for the duration of the inline assembly block and then switch back.
Toma Tabacua23f13c2014-12-17 10:56:16 +0000793 TS.emitDirectiveSetPush();
794 TS.emitDirectiveSetAt();
795 TS.emitDirectiveSetMacro();
796 TS.emitDirectiveSetReorder();
Lang Hames9ff69c82015-04-24 19:11:51 +0000797 OutStreamer->AddBlankLine();
Toma Tabacua23f13c2014-12-17 10:56:16 +0000798}
799
800void MipsAsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
801 const MCSubtargetInfo *EndInfo) const {
Lang Hames9ff69c82015-04-24 19:11:51 +0000802 OutStreamer->AddBlankLine();
Toma Tabacua23f13c2014-12-17 10:56:16 +0000803 getTargetStreamer().emitDirectiveSetPop();
804}
805
Eric Christopher327fc972015-02-21 08:48:22 +0000806void MipsAsmPrinter::EmitJal(const MCSubtargetInfo &STI, MCSymbol *Symbol) {
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000807 MCInst I;
808 I.setOpcode(Mips::JAL);
809 I.addOperand(
Jim Grosbach13760bd2015-05-30 01:25:56 +0000810 MCOperand::createExpr(MCSymbolRefExpr::create(Symbol, OutContext)));
Lang Hames9ff69c82015-04-24 19:11:51 +0000811 OutStreamer->EmitInstruction(I, STI);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000812}
813
Eric Christopher327fc972015-02-21 08:48:22 +0000814void MipsAsmPrinter::EmitInstrReg(const MCSubtargetInfo &STI, unsigned Opcode,
815 unsigned Reg) {
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000816 MCInst I;
817 I.setOpcode(Opcode);
Jim Grosbache9119e42015-05-13 18:37:00 +0000818 I.addOperand(MCOperand::createReg(Reg));
Lang Hames9ff69c82015-04-24 19:11:51 +0000819 OutStreamer->EmitInstruction(I, STI);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000820}
821
Eric Christopher327fc972015-02-21 08:48:22 +0000822void MipsAsmPrinter::EmitInstrRegReg(const MCSubtargetInfo &STI,
823 unsigned Opcode, unsigned Reg1,
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000824 unsigned Reg2) {
825 MCInst I;
826 //
827 // Because of the current td files for Mips32, the operands for MTC1
828 // appear backwards from their normal assembly order. It's not a trivial
829 // change to fix this in the td file so we adjust for it here.
830 //
831 if (Opcode == Mips::MTC1) {
832 unsigned Temp = Reg1;
833 Reg1 = Reg2;
834 Reg2 = Temp;
835 }
836 I.setOpcode(Opcode);
Jim Grosbache9119e42015-05-13 18:37:00 +0000837 I.addOperand(MCOperand::createReg(Reg1));
838 I.addOperand(MCOperand::createReg(Reg2));
Lang Hames9ff69c82015-04-24 19:11:51 +0000839 OutStreamer->EmitInstruction(I, STI);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000840}
841
Eric Christopher327fc972015-02-21 08:48:22 +0000842void MipsAsmPrinter::EmitInstrRegRegReg(const MCSubtargetInfo &STI,
843 unsigned Opcode, unsigned Reg1,
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000844 unsigned Reg2, unsigned Reg3) {
845 MCInst I;
846 I.setOpcode(Opcode);
Jim Grosbache9119e42015-05-13 18:37:00 +0000847 I.addOperand(MCOperand::createReg(Reg1));
848 I.addOperand(MCOperand::createReg(Reg2));
849 I.addOperand(MCOperand::createReg(Reg3));
Lang Hames9ff69c82015-04-24 19:11:51 +0000850 OutStreamer->EmitInstruction(I, STI);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000851}
852
Eric Christopher327fc972015-02-21 08:48:22 +0000853void MipsAsmPrinter::EmitMovFPIntPair(const MCSubtargetInfo &STI,
854 unsigned MovOpc, unsigned Reg1,
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000855 unsigned Reg2, unsigned FPReg1,
856 unsigned FPReg2, bool LE) {
857 if (!LE) {
858 unsigned temp = Reg1;
859 Reg1 = Reg2;
860 Reg2 = temp;
861 }
Eric Christopher327fc972015-02-21 08:48:22 +0000862 EmitInstrRegReg(STI, MovOpc, Reg1, FPReg1);
863 EmitInstrRegReg(STI, MovOpc, Reg2, FPReg2);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000864}
865
Eric Christopher327fc972015-02-21 08:48:22 +0000866void MipsAsmPrinter::EmitSwapFPIntParams(const MCSubtargetInfo &STI,
867 Mips16HardFloatInfo::FPParamVariant PV,
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000868 bool LE, bool ToFP) {
869 using namespace Mips16HardFloatInfo;
Eugene Zelenko79220eae2017-08-03 22:12:30 +0000870
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000871 unsigned MovOpc = ToFP ? Mips::MTC1 : Mips::MFC1;
872 switch (PV) {
873 case FSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000874 EmitInstrRegReg(STI, MovOpc, Mips::A0, Mips::F12);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000875 break;
876 case FFSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000877 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F14, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000878 break;
879 case FDSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000880 EmitInstrRegReg(STI, MovOpc, Mips::A0, Mips::F12);
881 EmitMovFPIntPair(STI, MovOpc, Mips::A2, Mips::A3, Mips::F14, Mips::F15, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000882 break;
883 case DSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000884 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000885 break;
886 case DDSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000887 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
888 EmitMovFPIntPair(STI, MovOpc, Mips::A2, Mips::A3, Mips::F14, Mips::F15, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000889 break;
890 case DFSig:
Eric Christopher327fc972015-02-21 08:48:22 +0000891 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
892 EmitInstrRegReg(STI, MovOpc, Mips::A2, Mips::F14);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000893 break;
894 case NoSig:
895 return;
896 }
897}
898
Eric Christopher327fc972015-02-21 08:48:22 +0000899void MipsAsmPrinter::EmitSwapFPIntRetval(
900 const MCSubtargetInfo &STI, Mips16HardFloatInfo::FPReturnVariant RV,
901 bool LE) {
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000902 using namespace Mips16HardFloatInfo;
Eugene Zelenko79220eae2017-08-03 22:12:30 +0000903
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000904 unsigned MovOpc = Mips::MFC1;
905 switch (RV) {
906 case FRet:
Eric Christopher327fc972015-02-21 08:48:22 +0000907 EmitInstrRegReg(STI, MovOpc, Mips::V0, Mips::F0);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000908 break;
909 case DRet:
Eric Christopher327fc972015-02-21 08:48:22 +0000910 EmitMovFPIntPair(STI, MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000911 break;
912 case CFRet:
Eric Christopher327fc972015-02-21 08:48:22 +0000913 EmitMovFPIntPair(STI, MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000914 break;
915 case CDRet:
Eric Christopher327fc972015-02-21 08:48:22 +0000916 EmitMovFPIntPair(STI, MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
917 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F2, Mips::F3, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000918 break;
919 case NoFPRet:
920 break;
921 }
922}
923
924void MipsAsmPrinter::EmitFPCallStub(
925 const char *Symbol, const Mips16HardFloatInfo::FuncSignature *Signature) {
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000926 using namespace Mips16HardFloatInfo;
Eugene Zelenko79220eae2017-08-03 22:12:30 +0000927
928 MCSymbol *MSymbol = OutContext.getOrCreateSymbol(StringRef(Symbol));
Eric Christopherbb401642015-02-21 08:32:22 +0000929 bool LE = getDataLayout().isLittleEndian();
Eric Christopher327fc972015-02-21 08:48:22 +0000930 // Construct a local MCSubtargetInfo here.
931 // This is because the MachineFunction won't exist (but have not yet been
932 // freed) and since we're at the global level we can use the default
933 // constructed subtarget.
934 std::unique_ptr<MCSubtargetInfo> STI(TM.getTarget().createMCSubtargetInfo(
Daniel Sanders335487a2015-06-16 13:15:50 +0000935 TM.getTargetTriple().str(), TM.getTargetCPU(),
936 TM.getTargetFeatureString()));
Eric Christopher327fc972015-02-21 08:48:22 +0000937
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000938 //
939 // .global xxxx
940 //
Lang Hames9ff69c82015-04-24 19:11:51 +0000941 OutStreamer->EmitSymbolAttribute(MSymbol, MCSA_Global);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000942 const char *RetType;
943 //
944 // make the comment field identifying the return and parameter
945 // types of the floating point stub
946 // # Stub function to call rettype xxxx (params)
947 //
948 switch (Signature->RetSig) {
949 case FRet:
950 RetType = "float";
951 break;
952 case DRet:
953 RetType = "double";
954 break;
955 case CFRet:
956 RetType = "complex";
957 break;
958 case CDRet:
959 RetType = "double complex";
960 break;
961 case NoFPRet:
962 RetType = "";
963 break;
964 }
965 const char *Parms;
966 switch (Signature->ParamSig) {
967 case FSig:
968 Parms = "float";
969 break;
970 case FFSig:
971 Parms = "float, float";
972 break;
973 case FDSig:
974 Parms = "float, double";
975 break;
976 case DSig:
977 Parms = "double";
978 break;
979 case DDSig:
980 Parms = "double, double";
981 break;
982 case DFSig:
983 Parms = "double, float";
984 break;
985 case NoSig:
986 Parms = "";
987 break;
988 }
Lang Hames9ff69c82015-04-24 19:11:51 +0000989 OutStreamer->AddComment("\t# Stub function to call " + Twine(RetType) + " " +
990 Twine(Symbol) + " (" + Twine(Parms) + ")");
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000991 //
992 // probably not necessary but we save and restore the current section state
993 //
Lang Hames9ff69c82015-04-24 19:11:51 +0000994 OutStreamer->PushSection();
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000995 //
996 // .section mips16.call.fpxxxx,"ax",@progbits
997 //
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000998 MCSectionELF *M = OutContext.getELFSection(
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000999 ".mips16.call.fp." + std::string(Symbol), ELF::SHT_PROGBITS,
Rafael Espindolaba31e272015-01-29 17:33:21 +00001000 ELF::SHF_ALLOC | ELF::SHF_EXECINSTR);
Lang Hames9ff69c82015-04-24 19:11:51 +00001001 OutStreamer->SwitchSection(M, nullptr);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001002 //
1003 // .align 2
1004 //
Lang Hames9ff69c82015-04-24 19:11:51 +00001005 OutStreamer->EmitValueToAlignment(4);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001006 MipsTargetStreamer &TS = getTargetStreamer();
1007 //
1008 // .set nomips16
1009 // .set nomicromips
1010 //
1011 TS.emitDirectiveSetNoMips16();
1012 TS.emitDirectiveSetNoMicroMips();
1013 //
1014 // .ent __call_stub_fp_xxxx
Vladimir Medicfb8a2a92014-07-08 08:59:22 +00001015 // .type __call_stub_fp_xxxx,@function
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001016 // __call_stub_fp_xxxx:
1017 //
1018 std::string x = "__call_stub_fp_" + std::string(Symbol);
Rafael Espindolaa8695762015-06-02 00:25:12 +00001019 MCSymbolELF *Stub =
1020 cast<MCSymbolELF>(OutContext.getOrCreateSymbol(StringRef(x)));
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001021 TS.emitDirectiveEnt(*Stub);
1022 MCSymbol *MType =
Jim Grosbach6f482002015-05-18 18:43:14 +00001023 OutContext.getOrCreateSymbol("__call_stub_fp_" + Twine(Symbol));
Lang Hames9ff69c82015-04-24 19:11:51 +00001024 OutStreamer->EmitSymbolAttribute(MType, MCSA_ELF_TypeFunction);
1025 OutStreamer->EmitLabel(Stub);
Eric Christopherd5bc07e2015-02-21 08:32:38 +00001026
1027 // Only handle non-pic for now.
Rafael Espindolab0f59cb2016-06-27 17:21:46 +00001028 assert(!isPositionIndependent() &&
Eric Christopherd5bc07e2015-02-21 08:32:38 +00001029 "should not be here if we are compiling pic");
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001030 TS.emitDirectiveSetReorder();
1031 //
1032 // We need to add a MipsMCExpr class to MCTargetDesc to fully implement
1033 // stubs without raw text but this current patch is for compiler generated
1034 // functions and they all return some value.
1035 // The calling sequence for non pic is different in that case and we need
1036 // to implement %lo and %hi in order to handle the case of no return value
1037 // See the corresponding method in Mips16HardFloat for details.
1038 //
1039 // mov the return address to S2.
1040 // we have no stack space to store it and we are about to make another call.
1041 // We need to make sure that the enclosing function knows to save S2
1042 // This should have already been handled.
1043 //
1044 // Mov $18, $31
1045
Vasileios Kalintiris1c78ca62015-08-11 08:56:25 +00001046 EmitInstrRegRegReg(*STI, Mips::OR, Mips::S2, Mips::RA, Mips::ZERO);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001047
Eric Christopher327fc972015-02-21 08:48:22 +00001048 EmitSwapFPIntParams(*STI, Signature->ParamSig, LE, true);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001049
1050 // Jal xxxx
1051 //
Eric Christopher327fc972015-02-21 08:48:22 +00001052 EmitJal(*STI, MSymbol);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001053
1054 // fix return values
Eric Christopher327fc972015-02-21 08:48:22 +00001055 EmitSwapFPIntRetval(*STI, Signature->RetSig, LE);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001056 //
1057 // do the return
1058 // if (Signature->RetSig == NoFPRet)
1059 // llvm_unreachable("should not be any stubs here with no return value");
1060 // else
Eric Christopher327fc972015-02-21 08:48:22 +00001061 EmitInstrReg(*STI, Mips::JR, Mips::S2);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001062
Jim Grosbach6f482002015-05-18 18:43:14 +00001063 MCSymbol *Tmp = OutContext.createTempSymbol();
Lang Hames9ff69c82015-04-24 19:11:51 +00001064 OutStreamer->EmitLabel(Tmp);
Jim Grosbach13760bd2015-05-30 01:25:56 +00001065 const MCSymbolRefExpr *E = MCSymbolRefExpr::create(Stub, OutContext);
1066 const MCSymbolRefExpr *T = MCSymbolRefExpr::create(Tmp, OutContext);
1067 const MCExpr *T_min_E = MCBinaryExpr::createSub(T, E, OutContext);
Rafael Espindolaa8695762015-06-02 00:25:12 +00001068 OutStreamer->emitELFSize(Stub, T_min_E);
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001069 TS.emitDirectiveEnd(x);
Lang Hames9ff69c82015-04-24 19:11:51 +00001070 OutStreamer->PopSection();
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001071}
1072
1073void MipsAsmPrinter::EmitEndOfAsmFile(Module &M) {
1074 // Emit needed stubs
1075 //
1076 for (std::map<
1077 const char *,
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001078 const Mips16HardFloatInfo::FuncSignature *>::const_iterator
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001079 it = StubsNeeded.begin();
1080 it != StubsNeeded.end(); ++it) {
1081 const char *Symbol = it->first;
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001082 const Mips16HardFloatInfo::FuncSignature *Signature = it->second;
Reed Kotler4cdaa7d2014-02-14 19:16:39 +00001083 EmitFPCallStub(Symbol, Signature);
1084 }
Rafael Espindola2ab7ea72014-01-27 01:33:33 +00001085 // return to the text section
Lang Hames9ff69c82015-04-24 19:11:51 +00001086 OutStreamer->SwitchSection(OutContext.getObjectFileInfo()->getTextSection());
Jack Carterc1b17ed2013-01-18 21:20:38 +00001087}
1088
Sagar Thakurec657922017-02-15 10:48:11 +00001089void MipsAsmPrinter::EmitSled(const MachineInstr &MI, SledKind Kind) {
1090 const uint8_t NoopsInSledCount = Subtarget->isGP64bit() ? 15 : 11;
1091 // For mips32 we want to emit the following pattern:
1092 //
1093 // .Lxray_sled_N:
1094 // ALIGN
1095 // B .tmpN
1096 // 11 NOP instructions (44 bytes)
1097 // ADDIU T9, T9, 52
1098 // .tmpN
1099 //
1100 // We need the 44 bytes (11 instructions) because at runtime, we'd
1101 // be patching over the full 48 bytes (12 instructions) with the following
1102 // pattern:
1103 //
NAKAMURA Takumi6f43bd42017-10-18 13:31:28 +00001104 // ADDIU SP, SP, -8
Sagar Thakurec657922017-02-15 10:48:11 +00001105 // NOP
NAKAMURA Takumi6f43bd42017-10-18 13:31:28 +00001106 // SW RA, 4(SP)
Sagar Thakurec657922017-02-15 10:48:11 +00001107 // SW T9, 0(SP)
1108 // LUI T9, %hi(__xray_FunctionEntry/Exit)
1109 // ORI T9, T9, %lo(__xray_FunctionEntry/Exit)
1110 // LUI T0, %hi(function_id)
NAKAMURA Takumi6f43bd42017-10-18 13:31:28 +00001111 // JALR T9
1112 // ORI T0, T0, %lo(function_id)
1113 // LW T9, 0(SP)
Sagar Thakurec657922017-02-15 10:48:11 +00001114 // LW RA, 4(SP)
1115 // ADDIU SP, SP, 8
1116 //
1117 // We add 52 bytes to t9 because we want to adjust the function pointer to
1118 // the actual start of function i.e. the address just after the noop sled.
1119 // We do this because gp displacement relocation is emitted at the start of
1120 // of the function i.e after the nop sled and to correctly calculate the
1121 // global offset table address, t9 must hold the address of the instruction
1122 // containing the gp displacement relocation.
1123 // FIXME: Is this correct for the static relocation model?
1124 //
1125 // For mips64 we want to emit the following pattern:
1126 //
1127 // .Lxray_sled_N:
1128 // ALIGN
1129 // B .tmpN
1130 // 15 NOP instructions (60 bytes)
1131 // .tmpN
1132 //
1133 // We need the 60 bytes (15 instructions) because at runtime, we'd
1134 // be patching over the full 64 bytes (16 instructions) with the following
1135 // pattern:
1136 //
1137 // DADDIU SP, SP, -16
1138 // NOP
1139 // SD RA, 8(SP)
1140 // SD T9, 0(SP)
1141 // LUI T9, %highest(__xray_FunctionEntry/Exit)
1142 // ORI T9, T9, %higher(__xray_FunctionEntry/Exit)
1143 // DSLL T9, T9, 16
1144 // ORI T9, T9, %hi(__xray_FunctionEntry/Exit)
1145 // DSLL T9, T9, 16
1146 // ORI T9, T9, %lo(__xray_FunctionEntry/Exit)
1147 // LUI T0, %hi(function_id)
1148 // JALR T9
1149 // ADDIU T0, T0, %lo(function_id)
1150 // LD T9, 0(SP)
1151 // LD RA, 8(SP)
1152 // DADDIU SP, SP, 16
1153 //
1154 OutStreamer->EmitCodeAlignment(4);
1155 auto CurSled = OutContext.createTempSymbol("xray_sled_", true);
1156 OutStreamer->EmitLabel(CurSled);
1157 auto Target = OutContext.createTempSymbol();
1158
1159 // Emit "B .tmpN" instruction, which jumps over the nop sled to the actual
1160 // start of function
1161 const MCExpr *TargetExpr = MCSymbolRefExpr::create(
1162 Target, MCSymbolRefExpr::VariantKind::VK_None, OutContext);
1163 EmitToStreamer(*OutStreamer, MCInstBuilder(Mips::BEQ)
1164 .addReg(Mips::ZERO)
1165 .addReg(Mips::ZERO)
1166 .addExpr(TargetExpr));
1167
1168 for (int8_t I = 0; I < NoopsInSledCount; I++)
1169 EmitToStreamer(*OutStreamer, MCInstBuilder(Mips::SLL)
1170 .addReg(Mips::ZERO)
1171 .addReg(Mips::ZERO)
1172 .addImm(0));
1173
1174 OutStreamer->EmitLabel(Target);
1175
1176 if (!Subtarget->isGP64bit()) {
1177 EmitToStreamer(*OutStreamer,
1178 MCInstBuilder(Mips::ADDiu)
1179 .addReg(Mips::T9)
1180 .addReg(Mips::T9)
1181 .addImm(0x34));
1182 }
1183
1184 recordSled(CurSled, MI, Kind);
1185}
1186
Sagar Thakurec657922017-02-15 10:48:11 +00001187void MipsAsmPrinter::LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr &MI) {
1188 EmitSled(MI, SledKind::FUNCTION_ENTER);
1189}
1190
1191void MipsAsmPrinter::LowerPATCHABLE_FUNCTION_EXIT(const MachineInstr &MI) {
1192 EmitSled(MI, SledKind::FUNCTION_EXIT);
1193}
1194
1195void MipsAsmPrinter::LowerPATCHABLE_TAIL_CALL(const MachineInstr &MI) {
1196 EmitSled(MI, SledKind::TAIL_CALL);
1197}
1198
Akira Hatanakaf2bcad92011-07-01 01:04:43 +00001199void MipsAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
1200 raw_ostream &OS) {
1201 // TODO: implement
1202}
1203
Petar Jovanovicdbb39352017-01-20 17:53:30 +00001204// Emit .dtprelword or .dtpreldword directive
1205// and value for debug thread local expression.
Simon Dardis2e8cdbd2017-02-08 19:03:46 +00001206void MipsAsmPrinter::EmitDebugThreadLocal(const MCExpr *Value,
Petar Jovanovicdbb39352017-01-20 17:53:30 +00001207 unsigned Size) const {
1208 switch (Size) {
1209 case 4:
1210 OutStreamer->EmitDTPRel32Value(Value);
1211 break;
1212 case 8:
1213 OutStreamer->EmitDTPRel64Value(Value);
1214 break;
1215 default:
1216 llvm_unreachable("Unexpected size of expression value.");
1217 }
1218}
1219
Sasa Stankovic8c5736b2014-02-28 10:00:38 +00001220// Align all targets of indirect branches on bundle size. Used only if target
1221// is NaCl.
1222void MipsAsmPrinter::NaClAlignIndirectJumpTargets(MachineFunction &MF) {
1223 // Align all blocks that are jumped to through jump table.
1224 if (MachineJumpTableInfo *JtInfo = MF.getJumpTableInfo()) {
1225 const std::vector<MachineJumpTableEntry> &JT = JtInfo->getJumpTables();
1226 for (unsigned I = 0; I < JT.size(); ++I) {
1227 const std::vector<MachineBasicBlock*> &MBBs = JT[I].MBBs;
1228
1229 for (unsigned J = 0; J < MBBs.size(); ++J)
1230 MBBs[J]->setAlignment(MIPS_NACL_BUNDLE_ALIGN);
1231 }
1232 }
1233
1234 // If basic block address is taken, block can be target of indirect branch.
Vasileios Kalintiris5a971a42016-04-15 20:43:17 +00001235 for (auto &MBB : MF) {
1236 if (MBB.hasAddressTaken())
1237 MBB.setAlignment(MIPS_NACL_BUNDLE_ALIGN);
Sasa Stankovic8c5736b2014-02-28 10:00:38 +00001238 }
1239}
1240
Sasa Stankovic7b061a42014-04-30 15:06:25 +00001241bool MipsAsmPrinter::isLongBranchPseudo(int Opcode) const {
1242 return (Opcode == Mips::LONG_BRANCH_LUi
1243 || Opcode == Mips::LONG_BRANCH_ADDiu
Sasa Stankovic7b061a42014-04-30 15:06:25 +00001244 || Opcode == Mips::LONG_BRANCH_DADDiu);
1245}
1246
Bob Wilson5a495fe2009-06-23 23:59:40 +00001247// Force static initialization.
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +00001248extern "C" void LLVMInitializeMipsAsmPrinter() {
Mehdi Aminif42454b2016-10-09 23:00:34 +00001249 RegisterAsmPrinter<MipsAsmPrinter> X(getTheMipsTarget());
1250 RegisterAsmPrinter<MipsAsmPrinter> Y(getTheMipselTarget());
1251 RegisterAsmPrinter<MipsAsmPrinter> A(getTheMips64Target());
1252 RegisterAsmPrinter<MipsAsmPrinter> B(getTheMips64elTarget());
Daniel Dunbare8338102009-07-15 20:24:03 +00001253}