Path: blob/main/contrib/llvm-project/llvm/lib/Target/Mips/MipsAsmPrinter.cpp
35294 views
//===- MipsAsmPrinter.cpp - Mips LLVM Assembly Printer --------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file contains a printer that converts from our internal representation9// of machine-dependent LLVM code to GAS-format MIPS assembly language.10//11//===----------------------------------------------------------------------===//1213#include "MipsAsmPrinter.h"14#include "MCTargetDesc/MipsABIInfo.h"15#include "MCTargetDesc/MipsBaseInfo.h"16#include "MCTargetDesc/MipsInstPrinter.h"17#include "MCTargetDesc/MipsMCNaCl.h"18#include "MCTargetDesc/MipsMCTargetDesc.h"19#include "Mips.h"20#include "MipsMCInstLower.h"21#include "MipsMachineFunction.h"22#include "MipsSubtarget.h"23#include "MipsTargetMachine.h"24#include "MipsTargetStreamer.h"25#include "TargetInfo/MipsTargetInfo.h"26#include "llvm/ADT/SmallString.h"27#include "llvm/ADT/StringRef.h"28#include "llvm/ADT/Twine.h"29#include "llvm/BinaryFormat/ELF.h"30#include "llvm/CodeGen/MachineBasicBlock.h"31#include "llvm/CodeGen/MachineConstantPool.h"32#include "llvm/CodeGen/MachineFrameInfo.h"33#include "llvm/CodeGen/MachineFunction.h"34#include "llvm/CodeGen/MachineInstr.h"35#include "llvm/CodeGen/MachineJumpTableInfo.h"36#include "llvm/CodeGen/MachineOperand.h"37#include "llvm/CodeGen/TargetRegisterInfo.h"38#include "llvm/CodeGen/TargetSubtargetInfo.h"39#include "llvm/IR/Attributes.h"40#include "llvm/IR/BasicBlock.h"41#include "llvm/IR/DataLayout.h"42#include "llvm/IR/Function.h"43#include "llvm/IR/InlineAsm.h"44#include "llvm/IR/Instructions.h"45#include "llvm/IR/Module.h"46#include "llvm/MC/MCContext.h"47#include "llvm/MC/MCExpr.h"48#include "llvm/MC/MCInst.h"49#include "llvm/MC/MCInstBuilder.h"50#include "llvm/MC/MCObjectFileInfo.h"51#include "llvm/MC/MCSectionELF.h"52#include "llvm/MC/MCSymbol.h"53#include "llvm/MC/MCSymbolELF.h"54#include "llvm/MC/TargetRegistry.h"55#include "llvm/Support/Casting.h"56#include "llvm/Support/ErrorHandling.h"57#include "llvm/Support/raw_ostream.h"58#include "llvm/Target/TargetLoweringObjectFile.h"59#include "llvm/Target/TargetMachine.h"60#include "llvm/TargetParser/Triple.h"61#include <cassert>62#include <cstdint>63#include <map>64#include <memory>65#include <string>66#include <vector>6768using namespace llvm;6970#define DEBUG_TYPE "mips-asm-printer"7172extern cl::opt<bool> EmitJalrReloc;7374MipsTargetStreamer &MipsAsmPrinter::getTargetStreamer() const {75return static_cast<MipsTargetStreamer &>(*OutStreamer->getTargetStreamer());76}7778bool MipsAsmPrinter::runOnMachineFunction(MachineFunction &MF) {79Subtarget = &MF.getSubtarget<MipsSubtarget>();8081MipsFI = MF.getInfo<MipsFunctionInfo>();82if (Subtarget->inMips16Mode())83for (const auto &I : MipsFI->StubsNeeded) {84const char *Symbol = I.first;85const Mips16HardFloatInfo::FuncSignature *Signature = I.second;86if (StubsNeeded.find(Symbol) == StubsNeeded.end())87StubsNeeded[Symbol] = Signature;88}89MCP = MF.getConstantPool();9091// In NaCl, all indirect jump targets must be aligned to bundle size.92if (Subtarget->isTargetNaCl())93NaClAlignIndirectJumpTargets(MF);9495AsmPrinter::runOnMachineFunction(MF);9697emitXRayTable();9899return true;100}101102bool MipsAsmPrinter::lowerOperand(const MachineOperand &MO, MCOperand &MCOp) {103MCOp = MCInstLowering.LowerOperand(MO);104return MCOp.isValid();105}106107#include "MipsGenMCPseudoLowering.inc"108109// Lower PseudoReturn/PseudoIndirectBranch/PseudoIndirectBranch64 to JR, JR_MM,110// JALR, or JALR64 as appropriate for the target.111void MipsAsmPrinter::emitPseudoIndirectBranch(MCStreamer &OutStreamer,112const MachineInstr *MI) {113bool HasLinkReg = false;114bool InMicroMipsMode = Subtarget->inMicroMipsMode();115MCInst TmpInst0;116117if (Subtarget->hasMips64r6()) {118// MIPS64r6 should use (JALR64 ZERO_64, $rs)119TmpInst0.setOpcode(Mips::JALR64);120HasLinkReg = true;121} else if (Subtarget->hasMips32r6()) {122// MIPS32r6 should use (JALR ZERO, $rs)123if (InMicroMipsMode)124TmpInst0.setOpcode(Mips::JRC16_MMR6);125else {126TmpInst0.setOpcode(Mips::JALR);127HasLinkReg = true;128}129} else if (Subtarget->inMicroMipsMode())130// microMIPS should use (JR_MM $rs)131TmpInst0.setOpcode(Mips::JR_MM);132else {133// Everything else should use (JR $rs)134TmpInst0.setOpcode(Mips::JR);135}136137MCOperand MCOp;138139if (HasLinkReg) {140unsigned ZeroReg = Subtarget->isGP64bit() ? Mips::ZERO_64 : Mips::ZERO;141TmpInst0.addOperand(MCOperand::createReg(ZeroReg));142}143144lowerOperand(MI->getOperand(0), MCOp);145TmpInst0.addOperand(MCOp);146147EmitToStreamer(OutStreamer, TmpInst0);148}149150// If there is an MO_JALR operand, insert:151//152// .reloc tmplabel, R_{MICRO}MIPS_JALR, symbol153// tmplabel:154//155// This is an optimization hint for the linker which may then replace156// an indirect call with a direct branch.157static void emitDirectiveRelocJalr(const MachineInstr &MI,158MCContext &OutContext,159TargetMachine &TM,160MCStreamer &OutStreamer,161const MipsSubtarget &Subtarget) {162for (const MachineOperand &MO :163llvm::drop_begin(MI.operands(), MI.getDesc().getNumOperands())) {164if (MO.isMCSymbol() && (MO.getTargetFlags() & MipsII::MO_JALR)) {165MCSymbol *Callee = MO.getMCSymbol();166if (Callee && !Callee->getName().empty()) {167MCSymbol *OffsetLabel = OutContext.createTempSymbol();168const MCExpr *OffsetExpr =169MCSymbolRefExpr::create(OffsetLabel, OutContext);170const MCExpr *CaleeExpr =171MCSymbolRefExpr::create(Callee, OutContext);172OutStreamer.emitRelocDirective(173*OffsetExpr,174Subtarget.inMicroMipsMode() ? "R_MICROMIPS_JALR" : "R_MIPS_JALR",175CaleeExpr, SMLoc(), *TM.getMCSubtargetInfo());176OutStreamer.emitLabel(OffsetLabel);177return;178}179}180}181}182183void MipsAsmPrinter::emitInstruction(const MachineInstr *MI) {184// FIXME: Enable feature predicate checks once all the test pass.185// Mips_MC::verifyInstructionPredicates(MI->getOpcode(),186// getSubtargetInfo().getFeatureBits());187188MipsTargetStreamer &TS = getTargetStreamer();189unsigned Opc = MI->getOpcode();190TS.forbidModuleDirective();191192if (MI->isDebugValue()) {193SmallString<128> Str;194raw_svector_ostream OS(Str);195196PrintDebugValueComment(MI, OS);197return;198}199if (MI->isDebugLabel())200return;201202// If we just ended a constant pool, mark it as such.203if (InConstantPool && Opc != Mips::CONSTPOOL_ENTRY) {204OutStreamer->emitDataRegion(MCDR_DataRegionEnd);205InConstantPool = false;206}207if (Opc == Mips::CONSTPOOL_ENTRY) {208// CONSTPOOL_ENTRY - This instruction represents a floating209// constant pool in the function. The first operand is the ID#210// for this instruction, the second is the index into the211// MachineConstantPool that this is, the third is the size in212// bytes of this constant pool entry.213// The required alignment is specified on the basic block holding this MI.214//215unsigned LabelId = (unsigned)MI->getOperand(0).getImm();216unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();217218// If this is the first entry of the pool, mark it.219if (!InConstantPool) {220OutStreamer->emitDataRegion(MCDR_DataRegion);221InConstantPool = true;222}223224OutStreamer->emitLabel(GetCPISymbol(LabelId));225226const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];227if (MCPE.isMachineConstantPoolEntry())228emitMachineConstantPoolValue(MCPE.Val.MachineCPVal);229else230emitGlobalConstant(MF->getDataLayout(), MCPE.Val.ConstVal);231return;232}233234switch (Opc) {235case Mips::PATCHABLE_FUNCTION_ENTER:236LowerPATCHABLE_FUNCTION_ENTER(*MI);237return;238case Mips::PATCHABLE_FUNCTION_EXIT:239LowerPATCHABLE_FUNCTION_EXIT(*MI);240return;241case Mips::PATCHABLE_TAIL_CALL:242LowerPATCHABLE_TAIL_CALL(*MI);243return;244}245246if (EmitJalrReloc &&247(MI->isReturn() || MI->isCall() || MI->isIndirectBranch())) {248emitDirectiveRelocJalr(*MI, OutContext, TM, *OutStreamer, *Subtarget);249}250251MachineBasicBlock::const_instr_iterator I = MI->getIterator();252MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();253254do {255// Do any auto-generated pseudo lowerings.256if (emitPseudoExpansionLowering(*OutStreamer, &*I))257continue;258259// Skip the BUNDLE pseudo instruction and lower the contents260if (I->isBundle())261continue;262263if (I->getOpcode() == Mips::PseudoReturn ||264I->getOpcode() == Mips::PseudoReturn64 ||265I->getOpcode() == Mips::PseudoIndirectBranch ||266I->getOpcode() == Mips::PseudoIndirectBranch64 ||267I->getOpcode() == Mips::TAILCALLREG ||268I->getOpcode() == Mips::TAILCALLREG64) {269emitPseudoIndirectBranch(*OutStreamer, &*I);270continue;271}272273// The inMips16Mode() test is not permanent.274// Some instructions are marked as pseudo right now which275// would make the test fail for the wrong reason but276// that will be fixed soon. We need this here because we are277// removing another test for this situation downstream in the278// callchain.279//280if (I->isPseudo() && !Subtarget->inMips16Mode()281&& !isLongBranchPseudo(I->getOpcode()))282llvm_unreachable("Pseudo opcode found in emitInstruction()");283284MCInst TmpInst0;285MCInstLowering.Lower(&*I, TmpInst0);286EmitToStreamer(*OutStreamer, TmpInst0);287} while ((++I != E) && I->isInsideBundle()); // Delay slot check288}289290//===----------------------------------------------------------------------===//291//292// Mips Asm Directives293//294// -- Frame directive "frame Stackpointer, Stacksize, RARegister"295// Describe the stack frame.296//297// -- Mask directives "(f)mask bitmask, offset"298// Tells the assembler which registers are saved and where.299// bitmask - contain a little endian bitset indicating which registers are300// saved on function prologue (e.g. with a 0x80000000 mask, the301// assembler knows the register 31 (RA) is saved at prologue.302// offset - the position before stack pointer subtraction indicating where303// the first saved register on prologue is located. (e.g. with a304//305// Consider the following function prologue:306//307// .frame $fp,48,$ra308// .mask 0xc0000000,-8309// addiu $sp, $sp, -48310// sw $ra, 40($sp)311// sw $fp, 36($sp)312//313// With a 0xc0000000 mask, the assembler knows the register 31 (RA) and314// 30 (FP) are saved at prologue. As the save order on prologue is from315// left to right, RA is saved first. A -8 offset means that after the316// stack pointer subtration, the first register in the mask (RA) will be317// saved at address 48-8=40.318//319//===----------------------------------------------------------------------===//320321//===----------------------------------------------------------------------===//322// Mask directives323//===----------------------------------------------------------------------===//324325// Create a bitmask with all callee saved registers for CPU or Floating Point326// registers. For CPU registers consider RA, GP and FP for saving if necessary.327void MipsAsmPrinter::printSavedRegsBitmask() {328// CPU and FPU Saved Registers Bitmasks329unsigned CPUBitmask = 0, FPUBitmask = 0;330int CPUTopSavedRegOff, FPUTopSavedRegOff;331332// Set the CPU and FPU Bitmasks333const MachineFrameInfo &MFI = MF->getFrameInfo();334const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();335const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();336// size of stack area to which FP callee-saved regs are saved.337unsigned CPURegSize = TRI->getRegSizeInBits(Mips::GPR32RegClass) / 8;338unsigned FGR32RegSize = TRI->getRegSizeInBits(Mips::FGR32RegClass) / 8;339unsigned AFGR64RegSize = TRI->getRegSizeInBits(Mips::AFGR64RegClass) / 8;340bool HasAFGR64Reg = false;341unsigned CSFPRegsSize = 0;342343for (const auto &I : CSI) {344Register Reg = I.getReg();345unsigned RegNum = TRI->getEncodingValue(Reg);346347// If it's a floating point register, set the FPU Bitmask.348// If it's a general purpose register, set the CPU Bitmask.349if (Mips::FGR32RegClass.contains(Reg)) {350FPUBitmask |= (1 << RegNum);351CSFPRegsSize += FGR32RegSize;352} else if (Mips::AFGR64RegClass.contains(Reg)) {353FPUBitmask |= (3 << RegNum);354CSFPRegsSize += AFGR64RegSize;355HasAFGR64Reg = true;356} else if (Mips::GPR32RegClass.contains(Reg))357CPUBitmask |= (1 << RegNum);358}359360// FP Regs are saved right below where the virtual frame pointer points to.361FPUTopSavedRegOff = FPUBitmask ?362(HasAFGR64Reg ? -AFGR64RegSize : -FGR32RegSize) : 0;363364// CPU Regs are saved below FP Regs.365CPUTopSavedRegOff = CPUBitmask ? -CSFPRegsSize - CPURegSize : 0;366367MipsTargetStreamer &TS = getTargetStreamer();368// Print CPUBitmask369TS.emitMask(CPUBitmask, CPUTopSavedRegOff);370371// Print FPUBitmask372TS.emitFMask(FPUBitmask, FPUTopSavedRegOff);373}374375//===----------------------------------------------------------------------===//376// Frame and Set directives377//===----------------------------------------------------------------------===//378379/// Frame Directive380void MipsAsmPrinter::emitFrameDirective() {381const TargetRegisterInfo &RI = *MF->getSubtarget().getRegisterInfo();382383Register stackReg = RI.getFrameRegister(*MF);384unsigned returnReg = RI.getRARegister();385unsigned stackSize = MF->getFrameInfo().getStackSize();386387getTargetStreamer().emitFrame(stackReg, stackSize, returnReg);388}389390/// Emit Set directives.391const char *MipsAsmPrinter::getCurrentABIString() const {392switch (static_cast<MipsTargetMachine &>(TM).getABI().GetEnumValue()) {393case MipsABIInfo::ABI::O32: return "abi32";394case MipsABIInfo::ABI::N32: return "abiN32";395case MipsABIInfo::ABI::N64: return "abi64";396default: llvm_unreachable("Unknown Mips ABI");397}398}399400void MipsAsmPrinter::emitFunctionEntryLabel() {401MipsTargetStreamer &TS = getTargetStreamer();402403// NaCl sandboxing requires that indirect call instructions are masked.404// This means that function entry points should be bundle-aligned.405if (Subtarget->isTargetNaCl())406emitAlignment(std::max(MF->getAlignment(), MIPS_NACL_BUNDLE_ALIGN));407408if (Subtarget->inMicroMipsMode()) {409TS.emitDirectiveSetMicroMips();410TS.setUsesMicroMips();411TS.updateABIInfo(*Subtarget);412} else413TS.emitDirectiveSetNoMicroMips();414415if (Subtarget->inMips16Mode())416TS.emitDirectiveSetMips16();417else418TS.emitDirectiveSetNoMips16();419420TS.emitDirectiveEnt(*CurrentFnSym);421OutStreamer->emitLabel(CurrentFnSym);422}423424/// EmitFunctionBodyStart - Targets can override this to emit stuff before425/// the first basic block in the function.426void MipsAsmPrinter::emitFunctionBodyStart() {427MipsTargetStreamer &TS = getTargetStreamer();428429MCInstLowering.Initialize(&MF->getContext());430431bool IsNakedFunction = MF->getFunction().hasFnAttribute(Attribute::Naked);432if (!IsNakedFunction)433emitFrameDirective();434435if (!IsNakedFunction)436printSavedRegsBitmask();437438if (!Subtarget->inMips16Mode()) {439TS.emitDirectiveSetNoReorder();440TS.emitDirectiveSetNoMacro();441TS.emitDirectiveSetNoAt();442}443}444445/// EmitFunctionBodyEnd - Targets can override this to emit stuff after446/// the last basic block in the function.447void MipsAsmPrinter::emitFunctionBodyEnd() {448MipsTargetStreamer &TS = getTargetStreamer();449450// There are instruction for this macros, but they must451// always be at the function end, and we can't emit and452// break with BB logic.453if (!Subtarget->inMips16Mode()) {454TS.emitDirectiveSetAt();455TS.emitDirectiveSetMacro();456TS.emitDirectiveSetReorder();457}458TS.emitDirectiveEnd(CurrentFnSym->getName());459// Make sure to terminate any constant pools that were at the end460// of the function.461if (!InConstantPool)462return;463InConstantPool = false;464OutStreamer->emitDataRegion(MCDR_DataRegionEnd);465}466467void MipsAsmPrinter::emitBasicBlockEnd(const MachineBasicBlock &MBB) {468AsmPrinter::emitBasicBlockEnd(MBB);469MipsTargetStreamer &TS = getTargetStreamer();470if (MBB.empty())471TS.emitDirectiveInsn();472}473474// Print out an operand for an inline asm expression.475bool MipsAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,476const char *ExtraCode, raw_ostream &O) {477// Does this asm operand have a single letter operand modifier?478if (ExtraCode && ExtraCode[0]) {479if (ExtraCode[1] != 0) return true; // Unknown modifier.480481const MachineOperand &MO = MI->getOperand(OpNum);482switch (ExtraCode[0]) {483default:484// See if this is a generic print operand485return AsmPrinter::PrintAsmOperand(MI, OpNum, ExtraCode, O);486case 'X': // hex const int487if (!MO.isImm())488return true;489O << "0x" << Twine::utohexstr(MO.getImm());490return false;491case 'x': // hex const int (low 16 bits)492if (!MO.isImm())493return true;494O << "0x" << Twine::utohexstr(MO.getImm() & 0xffff);495return false;496case 'd': // decimal const int497if (!MO.isImm())498return true;499O << MO.getImm();500return false;501case 'm': // decimal const int minus 1502if (!MO.isImm())503return true;504O << MO.getImm() - 1;505return false;506case 'y': // exact log2507if (!MO.isImm())508return true;509if (!isPowerOf2_64(MO.getImm()))510return true;511O << Log2_64(MO.getImm());512return false;513case 'z':514// $0 if zero, regular printing otherwise515if (MO.isImm() && MO.getImm() == 0) {516O << "$0";517return false;518}519// If not, call printOperand as normal.520break;521case 'D': // Second part of a double word register operand522case 'L': // Low order register of a double word register operand523case 'M': // High order register of a double word register operand524{525if (OpNum == 0)526return true;527const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);528if (!FlagsOP.isImm())529return true;530const InlineAsm::Flag Flags(FlagsOP.getImm());531const unsigned NumVals = Flags.getNumOperandRegisters();532// Number of registers represented by this operand. We are looking533// for 2 for 32 bit mode and 1 for 64 bit mode.534if (NumVals != 2) {535if (Subtarget->isGP64bit() && NumVals == 1 && MO.isReg()) {536Register Reg = MO.getReg();537O << '$' << MipsInstPrinter::getRegisterName(Reg);538return false;539}540return true;541}542543unsigned RegOp = OpNum;544if (!Subtarget->isGP64bit()){545// Endianness reverses which register holds the high or low value546// between M and L.547switch(ExtraCode[0]) {548case 'M':549RegOp = (Subtarget->isLittle()) ? OpNum + 1 : OpNum;550break;551case 'L':552RegOp = (Subtarget->isLittle()) ? OpNum : OpNum + 1;553break;554case 'D': // Always the second part555RegOp = OpNum + 1;556}557if (RegOp >= MI->getNumOperands())558return true;559const MachineOperand &MO = MI->getOperand(RegOp);560if (!MO.isReg())561return true;562Register Reg = MO.getReg();563O << '$' << MipsInstPrinter::getRegisterName(Reg);564return false;565}566break;567}568case 'w': {569MCRegister w = getMSARegFromFReg(MO.getReg());570if (w != Mips::NoRegister) {571O << '$' << MipsInstPrinter::getRegisterName(w);572return false;573}574break;575}576}577}578579printOperand(MI, OpNum, O);580return false;581}582583bool MipsAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,584unsigned OpNum,585const char *ExtraCode,586raw_ostream &O) {587assert(OpNum + 1 < MI->getNumOperands() && "Insufficient operands");588const MachineOperand &BaseMO = MI->getOperand(OpNum);589const MachineOperand &OffsetMO = MI->getOperand(OpNum + 1);590assert(BaseMO.isReg() &&591"Unexpected base pointer for inline asm memory operand.");592assert(OffsetMO.isImm() &&593"Unexpected offset for inline asm memory operand.");594int Offset = OffsetMO.getImm();595596// Currently we are expecting either no ExtraCode or 'D','M','L'.597if (ExtraCode) {598switch (ExtraCode[0]) {599case 'D':600Offset += 4;601break;602case 'M':603if (Subtarget->isLittle())604Offset += 4;605break;606case 'L':607if (!Subtarget->isLittle())608Offset += 4;609break;610default:611return true; // Unknown modifier.612}613}614615O << Offset << "($" << MipsInstPrinter::getRegisterName(BaseMO.getReg())616<< ")";617618return false;619}620621void MipsAsmPrinter::printOperand(const MachineInstr *MI, int opNum,622raw_ostream &O) {623const MachineOperand &MO = MI->getOperand(opNum);624bool closeP = false;625626if (MO.getTargetFlags())627closeP = true;628629switch(MO.getTargetFlags()) {630case MipsII::MO_GPREL: O << "%gp_rel("; break;631case MipsII::MO_GOT_CALL: O << "%call16("; break;632case MipsII::MO_GOT: O << "%got("; break;633case MipsII::MO_ABS_HI: O << "%hi("; break;634case MipsII::MO_ABS_LO: O << "%lo("; break;635case MipsII::MO_HIGHER: O << "%higher("; break;636case MipsII::MO_HIGHEST: O << "%highest(("; break;637case MipsII::MO_TLSGD: O << "%tlsgd("; break;638case MipsII::MO_GOTTPREL: O << "%gottprel("; break;639case MipsII::MO_TPREL_HI: O << "%tprel_hi("; break;640case MipsII::MO_TPREL_LO: O << "%tprel_lo("; break;641case MipsII::MO_GPOFF_HI: O << "%hi(%neg(%gp_rel("; break;642case MipsII::MO_GPOFF_LO: O << "%lo(%neg(%gp_rel("; break;643case MipsII::MO_GOT_DISP: O << "%got_disp("; break;644case MipsII::MO_GOT_PAGE: O << "%got_page("; break;645case MipsII::MO_GOT_OFST: O << "%got_ofst("; break;646}647648switch (MO.getType()) {649case MachineOperand::MO_Register:650O << '$'651<< StringRef(MipsInstPrinter::getRegisterName(MO.getReg())).lower();652break;653654case MachineOperand::MO_Immediate:655O << MO.getImm();656break;657658case MachineOperand::MO_MachineBasicBlock:659MO.getMBB()->getSymbol()->print(O, MAI);660return;661662case MachineOperand::MO_GlobalAddress:663PrintSymbolOperand(MO, O);664break;665666case MachineOperand::MO_BlockAddress: {667MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());668O << BA->getName();669break;670}671672case MachineOperand::MO_ConstantPoolIndex:673O << getDataLayout().getPrivateGlobalPrefix() << "CPI"674<< getFunctionNumber() << "_" << MO.getIndex();675if (MO.getOffset())676O << "+" << MO.getOffset();677break;678679default:680llvm_unreachable("<unknown operand type>");681}682683if (closeP) O << ")";684}685686void MipsAsmPrinter::687printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O) {688// Load/Store memory operands -- imm($reg)689// If PIC target the target is loaded as the690// pattern lw $25,%call16($28)691692// opNum can be invalid if instruction has reglist as operand.693// MemOperand is always last operand of instruction (base + offset).694switch (MI->getOpcode()) {695default:696break;697case Mips::SWM32_MM:698case Mips::LWM32_MM:699opNum = MI->getNumOperands() - 2;700break;701}702703printOperand(MI, opNum+1, O);704O << "(";705printOperand(MI, opNum, O);706O << ")";707}708709void MipsAsmPrinter::710printMemOperandEA(const MachineInstr *MI, int opNum, raw_ostream &O) {711// when using stack locations for not load/store instructions712// print the same way as all normal 3 operand instructions.713printOperand(MI, opNum, O);714O << ", ";715printOperand(MI, opNum+1, O);716}717718void MipsAsmPrinter::719printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O,720const char *Modifier) {721const MachineOperand &MO = MI->getOperand(opNum);722O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());723}724725void MipsAsmPrinter::726printRegisterList(const MachineInstr *MI, int opNum, raw_ostream &O) {727for (int i = opNum, e = MI->getNumOperands(); i != e; ++i) {728if (i != opNum) O << ", ";729printOperand(MI, i, O);730}731}732733void MipsAsmPrinter::emitStartOfAsmFile(Module &M) {734MipsTargetStreamer &TS = getTargetStreamer();735736// MipsTargetStreamer has an initialization order problem when emitting an737// object file directly (see MipsTargetELFStreamer for full details). Work738// around it by re-initializing the PIC state here.739TS.setPic(OutContext.getObjectFileInfo()->isPositionIndependent());740741// Try to get target-features from the first function.742StringRef FS = TM.getTargetFeatureString();743Module::iterator F = M.begin();744if (FS.empty() && M.size() && F->hasFnAttribute("target-features"))745FS = F->getFnAttribute("target-features").getValueAsString();746747// Compute MIPS architecture attributes based on the default subtarget748// that we'd have constructed.749// FIXME: For ifunc related functions we could iterate over and look750// for a feature string that doesn't match the default one.751const Triple &TT = TM.getTargetTriple();752StringRef CPU = MIPS_MC::selectMipsCPU(TT, TM.getTargetCPU());753const MipsTargetMachine &MTM = static_cast<const MipsTargetMachine &>(TM);754const MipsSubtarget STI(TT, CPU, FS, MTM.isLittleEndian(), MTM, std::nullopt);755756bool IsABICalls = STI.isABICalls();757const MipsABIInfo &ABI = MTM.getABI();758if (IsABICalls) {759TS.emitDirectiveAbiCalls();760// FIXME: This condition should be a lot more complicated that it is here.761// Ideally it should test for properties of the ABI and not the ABI762// itself.763// For the moment, I'm only correcting enough to make MIPS-IV work.764if (!isPositionIndependent() && STI.hasSym32())765TS.emitDirectiveOptionPic0();766}767768// Tell the assembler which ABI we are using769std::string SectionName = std::string(".mdebug.") + getCurrentABIString();770OutStreamer->switchSection(771OutContext.getELFSection(SectionName, ELF::SHT_PROGBITS, 0));772773// NaN: At the moment we only support:774// 1. .nan legacy (default)775// 2. .nan 2008776STI.isNaN2008() ? TS.emitDirectiveNaN2008()777: TS.emitDirectiveNaNLegacy();778779// TODO: handle O64 ABI780781TS.updateABIInfo(STI);782783// We should always emit a '.module fp=...' but binutils 2.24 does not accept784// it. We therefore emit it when it contradicts the ABI defaults (-mfpxx or785// -mfp64) and omit it otherwise.786if ((ABI.IsO32() && (STI.isABI_FPXX() || STI.isFP64bit())) ||787STI.useSoftFloat())788TS.emitDirectiveModuleFP();789790// We should always emit a '.module [no]oddspreg' but binutils 2.24 does not791// accept it. We therefore emit it when it contradicts the default or an792// option has changed the default (i.e. FPXX) and omit it otherwise.793if (ABI.IsO32() && (!STI.useOddSPReg() || STI.isABI_FPXX()))794TS.emitDirectiveModuleOddSPReg();795796// Switch to the .text section.797OutStreamer->switchSection(getObjFileLowering().getTextSection());798}799800void MipsAsmPrinter::emitInlineAsmStart() const {801MipsTargetStreamer &TS = getTargetStreamer();802803// GCC's choice of assembler options for inline assembly code ('at', 'macro'804// and 'reorder') is different from LLVM's choice for generated code ('noat',805// 'nomacro' and 'noreorder').806// In order to maintain compatibility with inline assembly code which depends807// on GCC's assembler options being used, we have to switch to those options808// for the duration of the inline assembly block and then switch back.809TS.emitDirectiveSetPush();810TS.emitDirectiveSetAt();811TS.emitDirectiveSetMacro();812TS.emitDirectiveSetReorder();813OutStreamer->addBlankLine();814}815816void MipsAsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,817const MCSubtargetInfo *EndInfo) const {818OutStreamer->addBlankLine();819getTargetStreamer().emitDirectiveSetPop();820}821822void MipsAsmPrinter::EmitJal(const MCSubtargetInfo &STI, MCSymbol *Symbol) {823MCInst I;824I.setOpcode(Mips::JAL);825I.addOperand(826MCOperand::createExpr(MCSymbolRefExpr::create(Symbol, OutContext)));827OutStreamer->emitInstruction(I, STI);828}829830void MipsAsmPrinter::EmitInstrReg(const MCSubtargetInfo &STI, unsigned Opcode,831unsigned Reg) {832MCInst I;833I.setOpcode(Opcode);834I.addOperand(MCOperand::createReg(Reg));835OutStreamer->emitInstruction(I, STI);836}837838void MipsAsmPrinter::EmitInstrRegReg(const MCSubtargetInfo &STI,839unsigned Opcode, unsigned Reg1,840unsigned Reg2) {841MCInst I;842//843// Because of the current td files for Mips32, the operands for MTC1844// appear backwards from their normal assembly order. It's not a trivial845// change to fix this in the td file so we adjust for it here.846//847if (Opcode == Mips::MTC1) {848unsigned Temp = Reg1;849Reg1 = Reg2;850Reg2 = Temp;851}852I.setOpcode(Opcode);853I.addOperand(MCOperand::createReg(Reg1));854I.addOperand(MCOperand::createReg(Reg2));855OutStreamer->emitInstruction(I, STI);856}857858void MipsAsmPrinter::EmitInstrRegRegReg(const MCSubtargetInfo &STI,859unsigned Opcode, unsigned Reg1,860unsigned Reg2, unsigned Reg3) {861MCInst I;862I.setOpcode(Opcode);863I.addOperand(MCOperand::createReg(Reg1));864I.addOperand(MCOperand::createReg(Reg2));865I.addOperand(MCOperand::createReg(Reg3));866OutStreamer->emitInstruction(I, STI);867}868869void MipsAsmPrinter::EmitMovFPIntPair(const MCSubtargetInfo &STI,870unsigned MovOpc, unsigned Reg1,871unsigned Reg2, unsigned FPReg1,872unsigned FPReg2, bool LE) {873if (!LE) {874unsigned temp = Reg1;875Reg1 = Reg2;876Reg2 = temp;877}878EmitInstrRegReg(STI, MovOpc, Reg1, FPReg1);879EmitInstrRegReg(STI, MovOpc, Reg2, FPReg2);880}881882void MipsAsmPrinter::EmitSwapFPIntParams(const MCSubtargetInfo &STI,883Mips16HardFloatInfo::FPParamVariant PV,884bool LE, bool ToFP) {885using namespace Mips16HardFloatInfo;886887unsigned MovOpc = ToFP ? Mips::MTC1 : Mips::MFC1;888switch (PV) {889case FSig:890EmitInstrRegReg(STI, MovOpc, Mips::A0, Mips::F12);891break;892case FFSig:893EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F14, LE);894break;895case FDSig:896EmitInstrRegReg(STI, MovOpc, Mips::A0, Mips::F12);897EmitMovFPIntPair(STI, MovOpc, Mips::A2, Mips::A3, Mips::F14, Mips::F15, LE);898break;899case DSig:900EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);901break;902case DDSig:903EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);904EmitMovFPIntPair(STI, MovOpc, Mips::A2, Mips::A3, Mips::F14, Mips::F15, LE);905break;906case DFSig:907EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);908EmitInstrRegReg(STI, MovOpc, Mips::A2, Mips::F14);909break;910case NoSig:911return;912}913}914915void MipsAsmPrinter::EmitSwapFPIntRetval(916const MCSubtargetInfo &STI, Mips16HardFloatInfo::FPReturnVariant RV,917bool LE) {918using namespace Mips16HardFloatInfo;919920unsigned MovOpc = Mips::MFC1;921switch (RV) {922case FRet:923EmitInstrRegReg(STI, MovOpc, Mips::V0, Mips::F0);924break;925case DRet:926EmitMovFPIntPair(STI, MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);927break;928case CFRet:929EmitMovFPIntPair(STI, MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);930break;931case CDRet:932EmitMovFPIntPair(STI, MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);933EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F2, Mips::F3, LE);934break;935case NoFPRet:936break;937}938}939940void MipsAsmPrinter::EmitFPCallStub(941const char *Symbol, const Mips16HardFloatInfo::FuncSignature *Signature) {942using namespace Mips16HardFloatInfo;943944MCSymbol *MSymbol = OutContext.getOrCreateSymbol(StringRef(Symbol));945bool LE = getDataLayout().isLittleEndian();946// Construct a local MCSubtargetInfo here.947// This is because the MachineFunction won't exist (but have not yet been948// freed) and since we're at the global level we can use the default949// constructed subtarget.950std::unique_ptr<MCSubtargetInfo> STI(TM.getTarget().createMCSubtargetInfo(951TM.getTargetTriple().str(), TM.getTargetCPU(),952TM.getTargetFeatureString()));953954//955// .global xxxx956//957OutStreamer->emitSymbolAttribute(MSymbol, MCSA_Global);958const char *RetType;959//960// make the comment field identifying the return and parameter961// types of the floating point stub962// # Stub function to call rettype xxxx (params)963//964switch (Signature->RetSig) {965case FRet:966RetType = "float";967break;968case DRet:969RetType = "double";970break;971case CFRet:972RetType = "complex";973break;974case CDRet:975RetType = "double complex";976break;977case NoFPRet:978RetType = "";979break;980}981const char *Parms;982switch (Signature->ParamSig) {983case FSig:984Parms = "float";985break;986case FFSig:987Parms = "float, float";988break;989case FDSig:990Parms = "float, double";991break;992case DSig:993Parms = "double";994break;995case DDSig:996Parms = "double, double";997break;998case DFSig:999Parms = "double, float";1000break;1001case NoSig:1002Parms = "";1003break;1004}1005OutStreamer->AddComment("\t# Stub function to call " + Twine(RetType) + " " +1006Twine(Symbol) + " (" + Twine(Parms) + ")");1007//1008// probably not necessary but we save and restore the current section state1009//1010OutStreamer->pushSection();1011//1012// .section mips16.call.fpxxxx,"ax",@progbits1013//1014MCSectionELF *M = OutContext.getELFSection(1015".mips16.call.fp." + std::string(Symbol), ELF::SHT_PROGBITS,1016ELF::SHF_ALLOC | ELF::SHF_EXECINSTR);1017OutStreamer->switchSection(M);1018//1019// .align 21020//1021OutStreamer->emitValueToAlignment(Align(4));1022MipsTargetStreamer &TS = getTargetStreamer();1023//1024// .set nomips161025// .set nomicromips1026//1027TS.emitDirectiveSetNoMips16();1028TS.emitDirectiveSetNoMicroMips();1029//1030// .ent __call_stub_fp_xxxx1031// .type __call_stub_fp_xxxx,@function1032// __call_stub_fp_xxxx:1033//1034std::string x = "__call_stub_fp_" + std::string(Symbol);1035MCSymbolELF *Stub =1036cast<MCSymbolELF>(OutContext.getOrCreateSymbol(StringRef(x)));1037TS.emitDirectiveEnt(*Stub);1038MCSymbol *MType =1039OutContext.getOrCreateSymbol("__call_stub_fp_" + Twine(Symbol));1040OutStreamer->emitSymbolAttribute(MType, MCSA_ELF_TypeFunction);1041OutStreamer->emitLabel(Stub);10421043// Only handle non-pic for now.1044assert(!isPositionIndependent() &&1045"should not be here if we are compiling pic");1046TS.emitDirectiveSetReorder();1047//1048// We need to add a MipsMCExpr class to MCTargetDesc to fully implement1049// stubs without raw text but this current patch is for compiler generated1050// functions and they all return some value.1051// The calling sequence for non pic is different in that case and we need1052// to implement %lo and %hi in order to handle the case of no return value1053// See the corresponding method in Mips16HardFloat for details.1054//1055// mov the return address to S2.1056// we have no stack space to store it and we are about to make another call.1057// We need to make sure that the enclosing function knows to save S21058// This should have already been handled.1059//1060// Mov $18, $3110611062EmitInstrRegRegReg(*STI, Mips::OR, Mips::S2, Mips::RA, Mips::ZERO);10631064EmitSwapFPIntParams(*STI, Signature->ParamSig, LE, true);10651066// Jal xxxx1067//1068EmitJal(*STI, MSymbol);10691070// fix return values1071EmitSwapFPIntRetval(*STI, Signature->RetSig, LE);1072//1073// do the return1074// if (Signature->RetSig == NoFPRet)1075// llvm_unreachable("should not be any stubs here with no return value");1076// else1077EmitInstrReg(*STI, Mips::JR, Mips::S2);10781079MCSymbol *Tmp = OutContext.createTempSymbol();1080OutStreamer->emitLabel(Tmp);1081const MCSymbolRefExpr *E = MCSymbolRefExpr::create(Stub, OutContext);1082const MCSymbolRefExpr *T = MCSymbolRefExpr::create(Tmp, OutContext);1083const MCExpr *T_min_E = MCBinaryExpr::createSub(T, E, OutContext);1084OutStreamer->emitELFSize(Stub, T_min_E);1085TS.emitDirectiveEnd(x);1086OutStreamer->popSection();1087}10881089void MipsAsmPrinter::emitEndOfAsmFile(Module &M) {1090// Emit needed stubs1091//1092for (std::map<1093const char *,1094const Mips16HardFloatInfo::FuncSignature *>::const_iterator1095it = StubsNeeded.begin();1096it != StubsNeeded.end(); ++it) {1097const char *Symbol = it->first;1098const Mips16HardFloatInfo::FuncSignature *Signature = it->second;1099EmitFPCallStub(Symbol, Signature);1100}1101// return to the text section1102OutStreamer->switchSection(OutContext.getObjectFileInfo()->getTextSection());1103}11041105void MipsAsmPrinter::EmitSled(const MachineInstr &MI, SledKind Kind) {1106const uint8_t NoopsInSledCount = Subtarget->isGP64bit() ? 15 : 11;1107// For mips32 we want to emit the following pattern:1108//1109// .Lxray_sled_N:1110// ALIGN1111// B .tmpN1112// 11 NOP instructions (44 bytes)1113// ADDIU T9, T9, 521114// .tmpN1115//1116// We need the 44 bytes (11 instructions) because at runtime, we'd1117// be patching over the full 48 bytes (12 instructions) with the following1118// pattern:1119//1120// ADDIU SP, SP, -81121// NOP1122// SW RA, 4(SP)1123// SW T9, 0(SP)1124// LUI T9, %hi(__xray_FunctionEntry/Exit)1125// ORI T9, T9, %lo(__xray_FunctionEntry/Exit)1126// LUI T0, %hi(function_id)1127// JALR T91128// ORI T0, T0, %lo(function_id)1129// LW T9, 0(SP)1130// LW RA, 4(SP)1131// ADDIU SP, SP, 81132//1133// We add 52 bytes to t9 because we want to adjust the function pointer to1134// the actual start of function i.e. the address just after the noop sled.1135// We do this because gp displacement relocation is emitted at the start of1136// of the function i.e after the nop sled and to correctly calculate the1137// global offset table address, t9 must hold the address of the instruction1138// containing the gp displacement relocation.1139// FIXME: Is this correct for the static relocation model?1140//1141// For mips64 we want to emit the following pattern:1142//1143// .Lxray_sled_N:1144// ALIGN1145// B .tmpN1146// 15 NOP instructions (60 bytes)1147// .tmpN1148//1149// We need the 60 bytes (15 instructions) because at runtime, we'd1150// be patching over the full 64 bytes (16 instructions) with the following1151// pattern:1152//1153// DADDIU SP, SP, -161154// NOP1155// SD RA, 8(SP)1156// SD T9, 0(SP)1157// LUI T9, %highest(__xray_FunctionEntry/Exit)1158// ORI T9, T9, %higher(__xray_FunctionEntry/Exit)1159// DSLL T9, T9, 161160// ORI T9, T9, %hi(__xray_FunctionEntry/Exit)1161// DSLL T9, T9, 161162// ORI T9, T9, %lo(__xray_FunctionEntry/Exit)1163// LUI T0, %hi(function_id)1164// JALR T91165// ADDIU T0, T0, %lo(function_id)1166// LD T9, 0(SP)1167// LD RA, 8(SP)1168// DADDIU SP, SP, 161169//1170OutStreamer->emitCodeAlignment(Align(4), &getSubtargetInfo());1171auto CurSled = OutContext.createTempSymbol("xray_sled_", true);1172OutStreamer->emitLabel(CurSled);1173auto Target = OutContext.createTempSymbol();11741175// Emit "B .tmpN" instruction, which jumps over the nop sled to the actual1176// start of function1177const MCExpr *TargetExpr = MCSymbolRefExpr::create(1178Target, MCSymbolRefExpr::VariantKind::VK_None, OutContext);1179EmitToStreamer(*OutStreamer, MCInstBuilder(Mips::BEQ)1180.addReg(Mips::ZERO)1181.addReg(Mips::ZERO)1182.addExpr(TargetExpr));11831184for (int8_t I = 0; I < NoopsInSledCount; I++)1185EmitToStreamer(*OutStreamer, MCInstBuilder(Mips::SLL)1186.addReg(Mips::ZERO)1187.addReg(Mips::ZERO)1188.addImm(0));11891190OutStreamer->emitLabel(Target);11911192if (!Subtarget->isGP64bit()) {1193EmitToStreamer(*OutStreamer,1194MCInstBuilder(Mips::ADDiu)1195.addReg(Mips::T9)1196.addReg(Mips::T9)1197.addImm(0x34));1198}11991200recordSled(CurSled, MI, Kind, 2);1201}12021203void MipsAsmPrinter::LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr &MI) {1204EmitSled(MI, SledKind::FUNCTION_ENTER);1205}12061207void MipsAsmPrinter::LowerPATCHABLE_FUNCTION_EXIT(const MachineInstr &MI) {1208EmitSled(MI, SledKind::FUNCTION_EXIT);1209}12101211void MipsAsmPrinter::LowerPATCHABLE_TAIL_CALL(const MachineInstr &MI) {1212EmitSled(MI, SledKind::TAIL_CALL);1213}12141215void MipsAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,1216raw_ostream &OS) {1217// TODO: implement1218}12191220// Emit .dtprelword or .dtpreldword directive1221// and value for debug thread local expression.1222void MipsAsmPrinter::emitDebugValue(const MCExpr *Value, unsigned Size) const {1223if (auto *MipsExpr = dyn_cast<MipsMCExpr>(Value)) {1224if (MipsExpr && MipsExpr->getKind() == MipsMCExpr::MEK_DTPREL) {1225switch (Size) {1226case 4:1227OutStreamer->emitDTPRel32Value(MipsExpr->getSubExpr());1228break;1229case 8:1230OutStreamer->emitDTPRel64Value(MipsExpr->getSubExpr());1231break;1232default:1233llvm_unreachable("Unexpected size of expression value.");1234}1235return;1236}1237}1238AsmPrinter::emitDebugValue(Value, Size);1239}12401241// Align all targets of indirect branches on bundle size. Used only if target1242// is NaCl.1243void MipsAsmPrinter::NaClAlignIndirectJumpTargets(MachineFunction &MF) {1244// Align all blocks that are jumped to through jump table.1245if (MachineJumpTableInfo *JtInfo = MF.getJumpTableInfo()) {1246const std::vector<MachineJumpTableEntry> &JT = JtInfo->getJumpTables();1247for (const auto &I : JT) {1248const std::vector<MachineBasicBlock *> &MBBs = I.MBBs;12491250for (MachineBasicBlock *MBB : MBBs)1251MBB->setAlignment(MIPS_NACL_BUNDLE_ALIGN);1252}1253}12541255// If basic block address is taken, block can be target of indirect branch.1256for (auto &MBB : MF) {1257if (MBB.hasAddressTaken())1258MBB.setAlignment(MIPS_NACL_BUNDLE_ALIGN);1259}1260}12611262bool MipsAsmPrinter::isLongBranchPseudo(int Opcode) const {1263return (Opcode == Mips::LONG_BRANCH_LUi1264|| Opcode == Mips::LONG_BRANCH_LUi2Op1265|| Opcode == Mips::LONG_BRANCH_LUi2Op_641266|| Opcode == Mips::LONG_BRANCH_ADDiu1267|| Opcode == Mips::LONG_BRANCH_ADDiu2Op1268|| Opcode == Mips::LONG_BRANCH_DADDiu1269|| Opcode == Mips::LONG_BRANCH_DADDiu2Op);1270}12711272// Force static initialization.1273extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeMipsAsmPrinter() {1274RegisterAsmPrinter<MipsAsmPrinter> X(getTheMipsTarget());1275RegisterAsmPrinter<MipsAsmPrinter> Y(getTheMipselTarget());1276RegisterAsmPrinter<MipsAsmPrinter> A(getTheMips64Target());1277RegisterAsmPrinter<MipsAsmPrinter> B(getTheMips64elTarget());1278}127912801281