1 //=-- BPFMCInstLower.cpp - Convert BPF MachineInstr to an MCInst ------------=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains code to lower BPF MachineInstrs to their corresponding
13 //===----------------------------------------------------------------------===//
15 #include "BPFMCInstLower.h"
16 #include "llvm/CodeGen/AsmPrinter.h"
17 #include "llvm/CodeGen/MachineBasicBlock.h"
18 #include "llvm/CodeGen/MachineInstr.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"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/raw_ostream.h"
28 BPFMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {
29 return Printer.getSymbol(MO.getGlobal());
32 MCOperand BPFMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
33 MCSymbol *Sym) const {
35 const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx);
37 if (!MO.isJTI() && MO.getOffset())
38 llvm_unreachable("unknown symbol op");
40 return MCOperand::createExpr(Expr);
43 void BPFMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
44 OutMI.setOpcode(MI->getOpcode());
46 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
47 const MachineOperand &MO = MI->getOperand(i);
50 switch (MO.getType()) {
53 llvm_unreachable("unknown operand type");
54 case MachineOperand::MO_Register:
55 // Ignore all implicit register operands.
58 MCOp = MCOperand::createReg(MO.getReg());
60 case MachineOperand::MO_Immediate:
61 MCOp = MCOperand::createImm(MO.getImm());
63 case MachineOperand::MO_MachineBasicBlock:
64 MCOp = MCOperand::createExpr(
65 MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx));
67 case MachineOperand::MO_RegisterMask:
69 case MachineOperand::MO_GlobalAddress:
70 MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
74 OutMI.addOperand(MCOp);