Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 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" |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ErrorHandling.h" |
Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
| 27 | MCSymbol * |
| 28 | BPFMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const { |
| 29 | return Printer.getSymbol(MO.getGlobal()); |
| 30 | } |
| 31 | |
Alexei Starovoitov | e497548 | 2017-01-17 07:26:17 +0000 | [diff] [blame] | 32 | MCSymbol * |
| 33 | BPFMCInstLower::GetExternalSymbolSymbol(const MachineOperand &MO) const { |
| 34 | return Printer.GetExternalSymbolSymbol(MO.getSymbolName()); |
| 35 | } |
| 36 | |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 37 | MCOperand BPFMCInstLower::LowerSymbolOperand(const MachineOperand &MO, |
| 38 | MCSymbol *Sym) const { |
| 39 | |
Alexei Starovoitov | dadc977 | 2015-06-01 22:24:36 +0000 | [diff] [blame] | 40 | const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx); |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 41 | |
| 42 | if (!MO.isJTI() && MO.getOffset()) |
| 43 | llvm_unreachable("unknown symbol op"); |
| 44 | |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 45 | return MCOperand::createExpr(Expr); |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void BPFMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const { |
| 49 | OutMI.setOpcode(MI->getOpcode()); |
| 50 | |
| 51 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 52 | const MachineOperand &MO = MI->getOperand(i); |
| 53 | |
| 54 | MCOperand MCOp; |
| 55 | switch (MO.getType()) { |
| 56 | default: |
| 57 | MI->dump(); |
| 58 | llvm_unreachable("unknown operand type"); |
| 59 | case MachineOperand::MO_Register: |
| 60 | // Ignore all implicit register operands. |
| 61 | if (MO.isImplicit()) |
| 62 | continue; |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 63 | MCOp = MCOperand::createReg(MO.getReg()); |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 64 | break; |
| 65 | case MachineOperand::MO_Immediate: |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 66 | MCOp = MCOperand::createImm(MO.getImm()); |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 67 | break; |
| 68 | case MachineOperand::MO_MachineBasicBlock: |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 69 | MCOp = MCOperand::createExpr( |
Alexei Starovoitov | dadc977 | 2015-06-01 22:24:36 +0000 | [diff] [blame] | 70 | MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx)); |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 71 | break; |
| 72 | case MachineOperand::MO_RegisterMask: |
| 73 | continue; |
Alexei Starovoitov | e497548 | 2017-01-17 07:26:17 +0000 | [diff] [blame] | 74 | case MachineOperand::MO_ExternalSymbol: |
| 75 | MCOp = LowerSymbolOperand(MO, GetExternalSymbolSymbol(MO)); |
| 76 | break; |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 77 | case MachineOperand::MO_GlobalAddress: |
| 78 | MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO)); |
| 79 | break; |
| 80 | } |
| 81 | |
| 82 | OutMI.addOperand(MCOp); |
| 83 | } |
| 84 | } |