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/raw_ostream.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/ADT/SmallString.h"
29 BPFMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {
30 return Printer.getSymbol(MO.getGlobal());
33 MCOperand BPFMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
34 MCSymbol *Sym) const {
36 const MCExpr *Expr = MCSymbolRefExpr::Create(Sym, Ctx);
38 if (!MO.isJTI() && MO.getOffset())
39 llvm_unreachable("unknown symbol op");
41 return MCOperand::CreateExpr(Expr);
44 void BPFMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
45 OutMI.setOpcode(MI->getOpcode());
47 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
48 const MachineOperand &MO = MI->getOperand(i);
51 switch (MO.getType()) {
54 llvm_unreachable("unknown operand type");
55 case MachineOperand::MO_Register:
56 // Ignore all implicit register operands.
59 MCOp = MCOperand::CreateReg(MO.getReg());
61 case MachineOperand::MO_Immediate:
62 MCOp = MCOperand::CreateImm(MO.getImm());
64 case MachineOperand::MO_MachineBasicBlock:
65 MCOp = MCOperand::CreateExpr(
66 MCSymbolRefExpr::Create(MO.getMBB()->getSymbol(), Ctx));
68 case MachineOperand::MO_RegisterMask:
70 case MachineOperand::MO_GlobalAddress:
71 MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
75 OutMI.addOperand(MCOp);