blob: d608afb348cb60db7c40c37eff2bd8f76b082695 [file] [log] [blame]
Alexei Starovoitove4c8c802015-01-24 17:51:26 +00001//=-- 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"
26using namespace llvm;
27
28MCSymbol *
29BPFMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {
30 return Printer.getSymbol(MO.getGlobal());
31}
32
33MCOperand 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
Jim Grosbache9119e42015-05-13 18:37:00 +000041 return MCOperand::createExpr(Expr);
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000042}
43
44void 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;
Jim Grosbache9119e42015-05-13 18:37:00 +000059 MCOp = MCOperand::createReg(MO.getReg());
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000060 break;
61 case MachineOperand::MO_Immediate:
Jim Grosbache9119e42015-05-13 18:37:00 +000062 MCOp = MCOperand::createImm(MO.getImm());
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000063 break;
64 case MachineOperand::MO_MachineBasicBlock:
Jim Grosbache9119e42015-05-13 18:37:00 +000065 MCOp = MCOperand::createExpr(
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000066 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}