LLVM  10.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_CALL:
41  break;
42  case RISCVII::MO_PLT:
44  break;
45  case RISCVII::MO_LO:
47  break;
48  case RISCVII::MO_HI:
50  break;
53  break;
56  break;
57  case RISCVII::MO_GOT_HI:
59  break;
62  break;
65  break;
68  break;
71  break;
74  break;
75  }
76 
77  const MCExpr *ME =
79 
80  if (!MO.isJTI() && !MO.isMBB() && MO.getOffset())
82  ME, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
83 
84  if (Kind != RISCVMCExpr::VK_RISCV_None)
85  ME = RISCVMCExpr::create(ME, Kind, Ctx);
86  return MCOperand::createExpr(ME);
87 }
88 
90  MCOperand &MCOp,
91  const AsmPrinter &AP) {
92  switch (MO.getType()) {
93  default:
94  report_fatal_error("LowerRISCVMachineInstrToMCInst: unknown operand type");
96  // Ignore all implicit register operands.
97  if (MO.isImplicit())
98  return false;
99  MCOp = MCOperand::createReg(MO.getReg());
100  break;
102  // Regmasks are like implicit defs.
103  return false;
105  MCOp = MCOperand::createImm(MO.getImm());
106  break;
108  MCOp = lowerSymbolOperand(MO, MO.getMBB()->getSymbol(), AP);
109  break;
111  MCOp = lowerSymbolOperand(MO, AP.getSymbol(MO.getGlobal()), AP);
112  break;
114  MCOp = lowerSymbolOperand(
115  MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()), AP);
116  break;
118  MCOp = lowerSymbolOperand(
119  MO, AP.GetExternalSymbolSymbol(MO.getSymbolName()), AP);
120  break;
122  MCOp = lowerSymbolOperand(MO, AP.GetCPISymbol(MO.getIndex()), AP);
123  break;
124  }
125  return true;
126 }
127 
129  const AsmPrinter &AP) {
130  OutMI.setOpcode(MI->getOpcode());
131 
132  for (const MachineOperand &MO : MI->operands()) {
133  MCOperand MCOp;
134  if (LowerRISCVMachineOperandToMCOperand(MO, MCOp, AP))
135  OutMI.addOperand(MCOp);
136  }
137 }
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:327
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
MachineBasicBlock reference.
iterator_range< mop_iterator > operands()
Definition: MachineInstr.h:477
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:411
const char * getSymbolName() const
Context object for machine code objects.
Definition: MCContext.h:65
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:465
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false)
Definition: MCExpr.cpp:169
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:444
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.
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.
Register getReg() const
getReg - Returns the register number.
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 RISCVMCExpr * create(const MCExpr *Expr, VariantKind Kind, MCContext &Ctx)
Definition: RISCVMCExpr.cpp:31
bool isImplicit() const