Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 1 | //=-- BPFMCInstLower.cpp - Convert BPF MachineInstr to an MCInst ------------=// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file contains code to lower BPF MachineInstrs to their corresponding |
| 10 | // MCInst records. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "BPFMCInstLower.h" |
| 15 | #include "llvm/CodeGen/AsmPrinter.h" |
| 16 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 17 | #include "llvm/CodeGen/MachineInstr.h" |
| 18 | #include "llvm/MC/MCAsmInfo.h" |
| 19 | #include "llvm/MC/MCContext.h" |
| 20 | #include "llvm/MC/MCExpr.h" |
| 21 | #include "llvm/MC/MCInst.h" |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ErrorHandling.h" |
Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
| 26 | MCSymbol * |
| 27 | BPFMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const { |
| 28 | return Printer.getSymbol(MO.getGlobal()); |
| 29 | } |
| 30 | |
Alexei Starovoitov | e497548 | 2017-01-17 07:26:17 +0000 | [diff] [blame] | 31 | MCSymbol * |
| 32 | BPFMCInstLower::GetExternalSymbolSymbol(const MachineOperand &MO) const { |
| 33 | return Printer.GetExternalSymbolSymbol(MO.getSymbolName()); |
| 34 | } |
| 35 | |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 36 | MCOperand BPFMCInstLower::LowerSymbolOperand(const MachineOperand &MO, |
| 37 | MCSymbol *Sym) const { |
| 38 | |
Alexei Starovoitov | dadc977 | 2015-06-01 22:24:36 +0000 | [diff] [blame] | 39 | const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx); |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 40 | |
| 41 | if (!MO.isJTI() && MO.getOffset()) |
| 42 | llvm_unreachable("unknown symbol op"); |
| 43 | |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 44 | return MCOperand::createExpr(Expr); |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void BPFMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const { |
| 48 | OutMI.setOpcode(MI->getOpcode()); |
| 49 | |
| 50 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 51 | const MachineOperand &MO = MI->getOperand(i); |
| 52 | |
| 53 | MCOperand MCOp; |
| 54 | switch (MO.getType()) { |
| 55 | default: |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 56 | MI->print(errs()); |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 57 | llvm_unreachable("unknown operand type"); |
| 58 | case MachineOperand::MO_Register: |
| 59 | // Ignore all implicit register operands. |
| 60 | if (MO.isImplicit()) |
| 61 | continue; |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 62 | MCOp = MCOperand::createReg(MO.getReg()); |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 63 | break; |
| 64 | case MachineOperand::MO_Immediate: |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 65 | MCOp = MCOperand::createImm(MO.getImm()); |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 66 | break; |
| 67 | case MachineOperand::MO_MachineBasicBlock: |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 68 | MCOp = MCOperand::createExpr( |
Alexei Starovoitov | dadc977 | 2015-06-01 22:24:36 +0000 | [diff] [blame] | 69 | MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx)); |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 70 | break; |
| 71 | case MachineOperand::MO_RegisterMask: |
| 72 | continue; |
Alexei Starovoitov | e497548 | 2017-01-17 07:26:17 +0000 | [diff] [blame] | 73 | case MachineOperand::MO_ExternalSymbol: |
| 74 | MCOp = LowerSymbolOperand(MO, GetExternalSymbolSymbol(MO)); |
| 75 | break; |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 76 | case MachineOperand::MO_GlobalAddress: |
| 77 | MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO)); |
| 78 | break; |
| 79 | } |
| 80 | |
| 81 | OutMI.addOperand(MCOp); |
| 82 | } |
| 83 | } |