Path: blob/main/contrib/llvm-project/llvm/lib/Target/BPF/BPFMCInstLower.cpp
35266 views
//=-- BPFMCInstLower.cpp - Convert BPF MachineInstr to an MCInst ------------=//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 code to lower BPF MachineInstrs to their corresponding9// MCInst records.10//11//===----------------------------------------------------------------------===//1213#include "BPFMCInstLower.h"14#include "llvm/CodeGen/AsmPrinter.h"15#include "llvm/CodeGen/MachineBasicBlock.h"16#include "llvm/CodeGen/MachineInstr.h"17#include "llvm/MC/MCAsmInfo.h"18#include "llvm/MC/MCContext.h"19#include "llvm/MC/MCExpr.h"20#include "llvm/MC/MCInst.h"21#include "llvm/Support/ErrorHandling.h"22#include "llvm/Support/raw_ostream.h"23using namespace llvm;2425MCSymbol *26BPFMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {27return Printer.getSymbol(MO.getGlobal());28}2930MCSymbol *31BPFMCInstLower::GetExternalSymbolSymbol(const MachineOperand &MO) const {32return Printer.GetExternalSymbolSymbol(MO.getSymbolName());33}3435MCOperand BPFMCInstLower::LowerSymbolOperand(const MachineOperand &MO,36MCSymbol *Sym) const {3738const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx);3940if (!MO.isJTI() && MO.getOffset())41llvm_unreachable("unknown symbol op");4243return MCOperand::createExpr(Expr);44}4546void BPFMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {47OutMI.setOpcode(MI->getOpcode());4849for (const MachineOperand &MO : MI->operands()) {50MCOperand MCOp;51switch (MO.getType()) {52default:53MI->print(errs());54llvm_unreachable("unknown operand type");55case MachineOperand::MO_Register:56// Ignore all implicit register operands.57if (MO.isImplicit())58continue;59MCOp = MCOperand::createReg(MO.getReg());60break;61case MachineOperand::MO_Immediate:62MCOp = MCOperand::createImm(MO.getImm());63break;64case MachineOperand::MO_MachineBasicBlock:65MCOp = MCOperand::createExpr(66MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx));67break;68case MachineOperand::MO_RegisterMask:69continue;70case MachineOperand::MO_ExternalSymbol:71MCOp = LowerSymbolOperand(MO, GetExternalSymbolSymbol(MO));72break;73case MachineOperand::MO_GlobalAddress:74MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));75break;76case MachineOperand::MO_ConstantPoolIndex:77MCOp = LowerSymbolOperand(MO, Printer.GetCPISymbol(MO.getIndex()));78break;79}8081OutMI.addOperand(MCOp);82}83}848586