LLVM  9.0.0svn
RISCVMCInstLower.cpp
Go to the documentation of this file.
1 //===-- RISCVMCInstLower.cpp - Convert RISCV MachineInstr to an MCInst ------=//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains code to lower RISCV MachineInstrs to their corresponding
10 // MCInst records.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "RISCV.h"
19 #include "llvm/MC/MCAsmInfo.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCInst.h"
25 
26 using namespace llvm;
27 
29  const AsmPrinter &AP) {
30  MCContext &Ctx = AP.OutContext;
32 
33  switch (MO.getTargetFlags()) {
34  default:
35  llvm_unreachable("Unknown target flag on GV operand");
36  case RISCVII::MO_None:
38  break;
39  case RISCVII::MO_LO:
41  break;
42  case RISCVII::MO_HI:
44  break;
45  }
46 
47  const MCExpr *ME =
49 
50  if (!MO.isJTI() && !MO.isMBB() && MO.getOffset())
52  ME, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
53 
54  if (Kind != RISCVMCExpr::VK_RISCV_None)
55  ME = RISCVMCExpr::create(ME, Kind, Ctx);
56  return MCOperand::createExpr(ME);
57 }
58 
60  MCOperand &MCOp,
61  const AsmPrinter &AP) {
62  switch (MO.getType()) {
63  default:
64  report_fatal_error("LowerRISCVMachineInstrToMCInst: unknown operand type");
66  // Ignore all implicit register operands.
67  if (MO.isImplicit())
68  return false;
69  MCOp = MCOperand::createReg(MO.getReg());
70  break;
72  // Regmasks are like implicit defs.
73  return false;
75  MCOp = MCOperand::createImm(MO.getImm());
76  break;
78  MCOp = lowerSymbolOperand(MO, MO.getMBB()->getSymbol(), AP);
79  break;
81  MCOp = lowerSymbolOperand(MO, AP.getSymbol(MO.getGlobal()), AP);
82  break;
84  MCOp = lowerSymbolOperand(
85  MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()), AP);
86  break;
88  MCOp = lowerSymbolOperand(
89  MO, AP.GetExternalSymbolSymbol(MO.getSymbolName()), AP);
90  break;
92  MCOp = lowerSymbolOperand(MO, AP.GetCPISymbol(MO.getIndex()), AP);
93  break;
94  }
95  return true;
96 }
97 
99  const AsmPrinter &AP) {
100  OutMI.setOpcode(MI->getOpcode());
101 
102  for (const MachineOperand &MO : MI->operands()) {
103  MCOperand MCOp;
104  if (LowerRISCVMachineOperandToMCOperand(MO, MCOp, AP))
105  OutMI.addOperand(MCOp);
106  }
107 }
unsigned getTargetFlags() const
MachineBasicBlock * getMBB() const
MCSymbol * GetExternalSymbolSymbol(StringRef Sym) const
Return the MCSymbol for the specified ExternalSymbol.
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:322
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:139
This class represents lattice values for constants.
Definition: AllocatorList.h:23
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition: AsmPrinter.h:88
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:136
unsigned getReg() const
getReg - Returns the register number.
MachineBasicBlock reference.
iterator_range< mop_iterator > operands()
Definition: MachineInstr.h:458
Mask of preserved registers.
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:115
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
Name of external global symbol.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:408
const char * getSymbolName() const
Context object for machine code objects.
Definition: MCContext.h:62
void LowerRISCVMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI, const AsmPrinter &AP)
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:460
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
Address of a global value.
const GlobalValue * getGlobal() const
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:78
static MCOperand lowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym, const AsmPrinter &AP)
Address of a basic block.
virtual MCSymbol * GetCPISymbol(unsigned CPID) const
Return the symbol for the specified constant pool entry.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void setOpcode(unsigned Op)
Definition: MCInst.h:170
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Return the MCSymbol used to satisfy BlockAddress uses of the specified basic block.
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:430
MachineOperand class - Representation of each machine instruction operand.
int64_t getImm() const
Representation of each machine instruction.
Definition: MachineInstr.h:63
int64_t getOffset() const
Return the offset from the symbol in this operand.
const BlockAddress * getBlockAddress() const
bool LowerRISCVMachineOperandToMCOperand(const MachineOperand &MO, MCOperand &MCOp, const AsmPrinter &AP)
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
const unsigned Kind
IRTranslator LLVM IR MI
void addOperand(const MCOperand &Op)
Definition: MCInst.h:183
bool isMBB() const
isMBB - Tests if this is a MO_MachineBasicBlock operand.
Address of indexed Constant in Constant Pool.
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:34
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:122
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:163
static const RISCVMCExpr * create(const MCExpr *Expr, VariantKind Kind, MCContext &Ctx)
Definition: RISCVMCExpr.cpp:28
bool isImplicit() const