Path: blob/main/contrib/llvm-project/llvm/lib/Target/ARC/ARCMCInstLower.cpp
35269 views
//===- ARCMCInstLower.cpp - ARC MachineInstr to MCInst ----------*- C++ -*-===//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/// \file9/// This file contains code to lower ARC MachineInstrs to their10/// corresponding MCInst records.11///12//===----------------------------------------------------------------------===//1314#include "ARCMCInstLower.h"15#include "llvm/CodeGen/AsmPrinter.h"16#include "llvm/CodeGen/MachineFunction.h"17#include "llvm/CodeGen/MachineInstr.h"18#include "llvm/CodeGen/MachineOperand.h"19#include "llvm/MC/MCContext.h"20#include "llvm/MC/MCExpr.h"21#include "llvm/MC/MCInst.h"2223using namespace llvm;2425ARCMCInstLower::ARCMCInstLower(MCContext *C, AsmPrinter &AsmPrinter)26: Ctx(C), Printer(AsmPrinter) {}2728MCOperand ARCMCInstLower::LowerSymbolOperand(const MachineOperand &MO,29MachineOperandType MOTy,30unsigned Offset) const {31MCSymbolRefExpr::VariantKind Kind = MCSymbolRefExpr::VK_None;32const MCSymbol *Symbol;3334switch (MOTy) {35case MachineOperand::MO_MachineBasicBlock:36Symbol = MO.getMBB()->getSymbol();37break;38case MachineOperand::MO_GlobalAddress:39Symbol = Printer.getSymbol(MO.getGlobal());40Offset += MO.getOffset();41break;42case MachineOperand::MO_BlockAddress:43Symbol = Printer.GetBlockAddressSymbol(MO.getBlockAddress());44Offset += MO.getOffset();45break;46case MachineOperand::MO_ExternalSymbol:47Symbol = Printer.GetExternalSymbolSymbol(MO.getSymbolName());48Offset += MO.getOffset();49break;50case MachineOperand::MO_JumpTableIndex:51Symbol = Printer.GetJTISymbol(MO.getIndex());52break;53case MachineOperand::MO_ConstantPoolIndex:54Symbol = Printer.GetCPISymbol(MO.getIndex());55Offset += MO.getOffset();56break;57default:58llvm_unreachable("<unknown operand type>");59}6061assert(Symbol && "Symbol creation failed.\n");62const MCSymbolRefExpr *MCSym = MCSymbolRefExpr::create(Symbol, Kind, *Ctx);6364if (!Offset)65return MCOperand::createExpr(MCSym);6667// Assume offset is never negative.68assert(Offset > 0);6970const MCConstantExpr *OffsetExpr = MCConstantExpr::create(Offset, *Ctx);71const MCBinaryExpr *Add = MCBinaryExpr::createAdd(MCSym, OffsetExpr, *Ctx);72return MCOperand::createExpr(Add);73}7475MCOperand ARCMCInstLower::LowerOperand(const MachineOperand &MO,76unsigned Offset) const {77MachineOperandType MOTy = MO.getType();7879switch (MOTy) {80default:81llvm_unreachable("unknown operand type");82case MachineOperand::MO_Register:83// Ignore all implicit register operands.84if (MO.isImplicit())85break;86return MCOperand::createReg(MO.getReg());87case MachineOperand::MO_Immediate:88return MCOperand::createImm(MO.getImm() + Offset);89case MachineOperand::MO_MachineBasicBlock:90case MachineOperand::MO_GlobalAddress:91case MachineOperand::MO_ExternalSymbol:92case MachineOperand::MO_JumpTableIndex:93case MachineOperand::MO_ConstantPoolIndex:94case MachineOperand::MO_BlockAddress:95return LowerSymbolOperand(MO, MOTy, Offset);96case MachineOperand::MO_RegisterMask:97break;98}99100return {};101}102103void ARCMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {104OutMI.setOpcode(MI->getOpcode());105106for (const MachineOperand &MO : MI->operands()) {107MCOperand MCOp = LowerOperand(MO);108109if (MCOp.isValid())110OutMI.addOperand(MCOp);111}112}113114115