LLVM  8.0.1
RISCVMCInstLower.cpp
Go to the documentation of this file.
1 //===-- RISCVMCInstLower.cpp - Convert RISCV MachineInstr to an MCInst ------=//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains code to lower RISCV MachineInstrs to their corresponding
11 // MCInst records.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "RISCV.h"
20 #include "llvm/MC/MCAsmInfo.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCInst.h"
26 
27 using namespace llvm;
28 
30  const AsmPrinter &AP) {
31  MCContext &Ctx = AP.OutContext;
33 
34  switch (MO.getTargetFlags()) {
35  default:
36  llvm_unreachable("Unknown target flag on GV operand");
37  case RISCVII::MO_None:
39  break;
40  case RISCVII::MO_LO:
42  break;
43  case RISCVII::MO_HI:
45  break;
46  }
47 
48  const MCExpr *ME =
50 
51  if (!MO.isJTI() && !MO.isMBB() && MO.getOffset())
53  ME, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
54 
55  if (Kind != RISCVMCExpr::VK_RISCV_None)
56  ME = RISCVMCExpr::create(ME, Kind, Ctx);
57  return MCOperand::createExpr(ME);
58 }
59 
61  MCOperand &MCOp,
62  const AsmPrinter &AP) {
63  switch (MO.getType()) {
64  default:
65  report_fatal_error("LowerRISCVMachineInstrToMCInst: unknown operand type");
67  // Ignore all implicit register operands.
68  if (MO.isImplicit())
69  return false;
70  MCOp = MCOperand::createReg(MO.getReg());
71  break;
73  // Regmasks are like implicit defs.
74  return false;
76  MCOp = MCOperand::createImm(MO.getImm());
77  break;
79  MCOp = lowerSymbolOperand(MO, MO.getMBB()->getSymbol(), AP);
80  break;
82  MCOp = lowerSymbolOperand(MO, AP.getSymbol(MO.getGlobal()), AP);
83  break;
85  MCOp = lowerSymbolOperand(
86  MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()), AP);
87  break;
89  MCOp = lowerSymbolOperand(
90  MO, AP.GetExternalSymbolSymbol(MO.getSymbolName()), AP);
91  break;
93  MCOp = lowerSymbolOperand(MO, AP.GetCPISymbol(MO.getIndex()), AP);
94  break;
95  }
96  return true;
97 }
98 
100  const AsmPrinter &AP) {
101  OutMI.setOpcode(MI->getOpcode());
102 
103  for (const MachineOperand &MO : MI->operands()) {
104  MCOperand MCOp;
105  if (LowerRISCVMachineOperandToMCOperand(MO, MCOp, AP))
106  OutMI.addOperand(MCOp);
107  }
108 }
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:323
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:140
This class represents lattice values for constants.
Definition: AllocatorList.h:24
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition: AsmPrinter.h:89
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:137
unsigned getReg() const
getReg - Returns the register number.
MachineBasicBlock reference.
iterator_range< mop_iterator > operands()
Definition: MachineInstr.h:459
Mask of preserved registers.
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:116
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
Name of external global symbol.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:409
const char * getSymbolName() const
Context object for machine code objects.
Definition: MCContext.h:63
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:461
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:161
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:79
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:173
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:64
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:186
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:35
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:123
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:164
static const RISCVMCExpr * create(const MCExpr *Expr, VariantKind Kind, MCContext &Ctx)
Definition: RISCVMCExpr.cpp:29
bool isImplicit() const