1 //=-- BPFMCInstLower.cpp - Convert BPF 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 BPF MachineInstrs to their corresponding 11 // MCInst records. 12 // 13 //===----------------------------------------------------------------------===// 14 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" 26 using namespace llvm; 27 28 MCSymbol * GetGlobalAddressSymbol(const MachineOperand & MO) const29BPFMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const { 30 return Printer.getSymbol(MO.getGlobal()); 31 } 32 LowerSymbolOperand(const MachineOperand & MO,MCSymbol * Sym) const33MCOperand BPFMCInstLower::LowerSymbolOperand(const MachineOperand &MO, 34 MCSymbol *Sym) const { 35 36 const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx); 37 38 if (!MO.isJTI() && MO.getOffset()) 39 llvm_unreachable("unknown symbol op"); 40 41 return MCOperand::createExpr(Expr); 42 } 43 Lower(const MachineInstr * MI,MCInst & OutMI) const44void BPFMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const { 45 OutMI.setOpcode(MI->getOpcode()); 46 47 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 48 const MachineOperand &MO = MI->getOperand(i); 49 50 MCOperand MCOp; 51 switch (MO.getType()) { 52 default: 53 MI->dump(); 54 llvm_unreachable("unknown operand type"); 55 case MachineOperand::MO_Register: 56 // Ignore all implicit register operands. 57 if (MO.isImplicit()) 58 continue; 59 MCOp = MCOperand::createReg(MO.getReg()); 60 break; 61 case MachineOperand::MO_Immediate: 62 MCOp = MCOperand::createImm(MO.getImm()); 63 break; 64 case MachineOperand::MO_MachineBasicBlock: 65 MCOp = MCOperand::createExpr( 66 MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx)); 67 break; 68 case MachineOperand::MO_RegisterMask: 69 continue; 70 case MachineOperand::MO_GlobalAddress: 71 MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO)); 72 break; 73 } 74 75 OutMI.addOperand(MCOp); 76 } 77 } 78