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