1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
//===- LoongArchAsmPrinter.cpp - LoongArch LLVM Assembly Printer -*- C++ -*--=//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file contains a printer that converts from our internal representation
// of machine-dependent LLVM code to GAS-format LoongArch assembly language.
//
//===----------------------------------------------------------------------===//
#include "LoongArchAsmPrinter.h"
#include "LoongArch.h"
#include "LoongArchMachineFunctionInfo.h"
#include "MCTargetDesc/LoongArchInstPrinter.h"
#include "MCTargetDesc/LoongArchMCTargetDesc.h"
#include "TargetInfo/LoongArchTargetInfo.h"
#include "llvm/CodeGen/AsmPrinter.h"
#include "llvm/CodeGen/MachineJumpTableInfo.h"
#include "llvm/CodeGen/MachineModuleInfoImpls.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCInstBuilder.h"
#include "llvm/MC/MCSectionELF.h"
#include "llvm/MC/TargetRegistry.h"
using namespace llvm;
#define DEBUG_TYPE "loongarch-asm-printer"
cl::opt<bool> LArchAnnotateTableJump(
"loongarch-annotate-tablejump", cl::Hidden,
cl::desc(
"Annotate table jump instruction to correlate it with the jump table."),
cl::init(false));
// Simple pseudo-instructions have their lowering (with expansion to real
// instructions) auto-generated.
#include "LoongArchGenMCPseudoLowering.inc"
void LoongArchAsmPrinter::emitInstruction(const MachineInstr *MI) {
LoongArch_MC::verifyInstructionPredicates(
MI->getOpcode(), getSubtargetInfo().getFeatureBits());
// Do any auto-generated pseudo lowerings.
if (MCInst OutInst; lowerPseudoInstExpansion(MI, OutInst)) {
EmitToStreamer(*OutStreamer, OutInst);
return;
}
switch (MI->getOpcode()) {
case TargetOpcode::STATEPOINT:
LowerSTATEPOINT(*MI);
return;
case TargetOpcode::PATCHABLE_FUNCTION_ENTER:
LowerPATCHABLE_FUNCTION_ENTER(*MI);
return;
case TargetOpcode::PATCHABLE_FUNCTION_EXIT:
LowerPATCHABLE_FUNCTION_EXIT(*MI);
return;
case TargetOpcode::PATCHABLE_TAIL_CALL:
LowerPATCHABLE_TAIL_CALL(*MI);
return;
}
MCInst TmpInst;
if (!lowerLoongArchMachineInstrToMCInst(MI, TmpInst, *this))
EmitToStreamer(*OutStreamer, TmpInst);
}
bool LoongArchAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
const char *ExtraCode,
raw_ostream &OS) {
// First try the generic code, which knows about modifiers like 'c' and 'n'.
if (!AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, OS))
return false;
const MachineOperand &MO = MI->getOperand(OpNo);
if (ExtraCode && ExtraCode[0]) {
if (ExtraCode[1] != 0)
return true; // Unknown modifier.
switch (ExtraCode[0]) {
default:
return true; // Unknown modifier.
case 'z': // Print $zero register if zero, regular printing otherwise.
if (MO.isImm() && MO.getImm() == 0) {
OS << '$' << LoongArchInstPrinter::getRegisterName(LoongArch::R0);
return false;
}
break;
case 'u': // Print LASX registers.
case 'w': // Print LSX registers.
{
// If the operand is an LASX, LSX or floating point register, print the
// name of LASX or LSX register with the same index in that register
// class.
unsigned RegID = MO.getReg().id(), FirstReg;
if (RegID >= LoongArch::XR0 && RegID <= LoongArch::XR31)
FirstReg = LoongArch::XR0;
else if (RegID >= LoongArch::VR0 && RegID <= LoongArch::VR31)
FirstReg = LoongArch::VR0;
else if (RegID >= LoongArch::F0_64 && RegID <= LoongArch::F31_64)
FirstReg = LoongArch::F0_64;
else if (RegID >= LoongArch::F0 && RegID <= LoongArch::F31)
FirstReg = LoongArch::F0;
else
return true;
OS << '$'
<< LoongArchInstPrinter::getRegisterName(
RegID - FirstReg +
(ExtraCode[0] == 'u' ? LoongArch::XR0 : LoongArch::VR0));
return false;
}
// TODO: handle other extra codes if any.
}
}
switch (MO.getType()) {
case MachineOperand::MO_Immediate:
OS << MO.getImm();
return false;
case MachineOperand::MO_Register:
OS << '$' << LoongArchInstPrinter::getRegisterName(MO.getReg());
return false;
case MachineOperand::MO_GlobalAddress:
PrintSymbolOperand(MO, OS);
return false;
default:
llvm_unreachable("not implemented");
}
return true;
}
bool LoongArchAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
unsigned OpNo,
const char *ExtraCode,
raw_ostream &OS) {
// TODO: handle extra code.
if (ExtraCode)
return true;
// We only support memory operands like "Base + Offset", where base must be a
// register, and offset can be a register or an immediate value.
const MachineOperand &BaseMO = MI->getOperand(OpNo);
// Base address must be a register.
if (!BaseMO.isReg())
return true;
// Print the base address register.
OS << "$" << LoongArchInstPrinter::getRegisterName(BaseMO.getReg());
// Print the offset operand.
const MachineOperand &OffsetMO = MI->getOperand(OpNo + 1);
MCOperand MCO;
if (!lowerOperand(OffsetMO, MCO))
return true;
if (OffsetMO.isReg())
OS << ", $" << LoongArchInstPrinter::getRegisterName(OffsetMO.getReg());
else if (OffsetMO.isImm())
OS << ", " << OffsetMO.getImm();
else if (OffsetMO.isGlobal() || OffsetMO.isBlockAddress() ||
OffsetMO.isMCSymbol())
OS << ", " << *MCO.getExpr();
else
return true;
return false;
}
void LoongArchAsmPrinter::LowerSTATEPOINT(const MachineInstr &MI) {
StatepointOpers SOpers(&MI);
if (unsigned PatchBytes = SOpers.getNumPatchBytes()) {
assert(PatchBytes % 4 == 0 && "Invalid number of NOP bytes requested!");
emitNops(PatchBytes / 4);
} else {
// Lower call target and choose correct opcode.
const MachineOperand &CallTarget = SOpers.getCallTarget();
MCOperand CallTargetMCOp;
switch (CallTarget.getType()) {
case MachineOperand::MO_GlobalAddress:
case MachineOperand::MO_ExternalSymbol:
lowerOperand(CallTarget, CallTargetMCOp);
EmitToStreamer(*OutStreamer,
MCInstBuilder(LoongArch::BL).addOperand(CallTargetMCOp));
break;
case MachineOperand::MO_Immediate:
CallTargetMCOp = MCOperand::createImm(CallTarget.getImm());
EmitToStreamer(*OutStreamer,
MCInstBuilder(LoongArch::BL).addOperand(CallTargetMCOp));
break;
case MachineOperand::MO_Register:
CallTargetMCOp = MCOperand::createReg(CallTarget.getReg());
EmitToStreamer(*OutStreamer, MCInstBuilder(LoongArch::JIRL)
.addReg(LoongArch::R1)
.addOperand(CallTargetMCOp)
.addImm(0));
break;
default:
llvm_unreachable("Unsupported operand type in statepoint call target");
break;
}
}
auto &Ctx = OutStreamer->getContext();
MCSymbol *MILabel = Ctx.createTempSymbol();
OutStreamer->emitLabel(MILabel);
SM.recordStatepoint(*MILabel, MI);
}
void LoongArchAsmPrinter::LowerPATCHABLE_FUNCTION_ENTER(
const MachineInstr &MI) {
const Function &F = MF->getFunction();
if (F.hasFnAttribute("patchable-function-entry")) {
unsigned Num;
if (F.getFnAttribute("patchable-function-entry")
.getValueAsString()
.getAsInteger(10, Num))
return;
emitNops(Num);
return;
}
emitSled(MI, SledKind::FUNCTION_ENTER);
}
void LoongArchAsmPrinter::LowerPATCHABLE_FUNCTION_EXIT(const MachineInstr &MI) {
emitSled(MI, SledKind::FUNCTION_EXIT);
}
void LoongArchAsmPrinter::LowerPATCHABLE_TAIL_CALL(const MachineInstr &MI) {
emitSled(MI, SledKind::TAIL_CALL);
}
void LoongArchAsmPrinter::emitSled(const MachineInstr &MI, SledKind Kind) {
// For loongarch64 we want to emit the following pattern:
//
// .Lxray_sled_beginN:
// B .Lxray_sled_endN
// 11 NOPs (44 bytes)
// .Lxray_sled_endN:
//
// We need the extra bytes because at runtime they may be used for the
// actual pattern defined at compiler-rt/lib/xray/xray_loongarch64.cpp.
// The count here should be adjusted accordingly if the implementation
// changes.
const int8_t NoopsInSledCount = 11;
OutStreamer->emitCodeAlignment(Align(4), &getSubtargetInfo());
MCSymbol *BeginOfSled = OutContext.createTempSymbol("xray_sled_begin");
MCSymbol *EndOfSled = OutContext.createTempSymbol("xray_sled_end");
OutStreamer->emitLabel(BeginOfSled);
EmitToStreamer(*OutStreamer,
MCInstBuilder(LoongArch::B)
.addExpr(MCSymbolRefExpr::create(EndOfSled, OutContext)));
emitNops(NoopsInSledCount);
OutStreamer->emitLabel(EndOfSled);
recordSled(BeginOfSled, MI, Kind, 2);
}
void LoongArchAsmPrinter::emitJumpTableInfo() {
AsmPrinter::emitJumpTableInfo();
if (!LArchAnnotateTableJump)
return;
assert(TM.getTargetTriple().isOSBinFormatELF());
auto *LAFI = MF->getInfo<LoongArchMachineFunctionInfo>();
unsigned EntrySize = LAFI->getJumpInfoSize();
auto JTI = MF->getJumpTableInfo();
if (!JTI || 0 == EntrySize)
return;
unsigned Size = getDataLayout().getPointerSize();
auto JT = JTI->getJumpTables();
// Emit an additional section to store the correlation info as pairs of
// addresses, each pair contains the address of a jump instruction (jr) and
// the address of the jump table.
OutStreamer->switchSection(MMI->getContext().getELFSection(
".discard.tablejump_annotate", ELF::SHT_PROGBITS, 0));
for (unsigned Idx = 0; Idx < EntrySize; ++Idx) {
int JTIIdx = LAFI->getJumpInfoJTIIndex(Idx);
if (JT[JTIIdx].MBBs.empty())
continue;
OutStreamer->emitValue(
MCSymbolRefExpr::create(LAFI->getJumpInfoJrMI(Idx)->getPreInstrSymbol(),
OutContext),
Size);
OutStreamer->emitValue(
MCSymbolRefExpr::create(GetJTISymbol(JTIIdx), OutContext), Size);
}
}
bool LoongArchAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
AsmPrinter::runOnMachineFunction(MF);
// Emit the XRay table for this function.
emitXRayTable();
return true;
}
// Force static initialization.
extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeLoongArchAsmPrinter() {
RegisterAsmPrinter<LoongArchAsmPrinter> X(getTheLoongArch32Target());
RegisterAsmPrinter<LoongArchAsmPrinter> Y(getTheLoongArch64Target());
}
|