Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 1 | // WebAssemblyMCInstLower.cpp - Convert WebAssembly 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 | /// \file |
| 11 | /// \brief This file contains code to lower WebAssembly MachineInstrs to their |
| 12 | /// corresponding MCInst records. |
| 13 | /// |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "WebAssemblyMCInstLower.h" |
| 17 | #include "llvm/ADT/SmallString.h" |
| 18 | #include "llvm/IR/Constants.h" |
| 19 | #include "llvm/CodeGen/AsmPrinter.h" |
| 20 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 21 | #include "llvm/CodeGen/MachineInstr.h" |
| 22 | #include "llvm/MC/MCAsmInfo.h" |
| 23 | #include "llvm/MC/MCContext.h" |
| 24 | #include "llvm/MC/MCExpr.h" |
| 25 | #include "llvm/MC/MCInst.h" |
| 26 | #include "llvm/Support/ErrorHandling.h" |
| 27 | #include "llvm/Support/raw_ostream.h" |
| 28 | using namespace llvm; |
| 29 | |
| 30 | MCSymbol * |
| 31 | WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const { |
| 32 | return Printer.getSymbol(MO.getGlobal()); |
| 33 | } |
| 34 | |
| 35 | MCOperand WebAssemblyMCInstLower::LowerSymbolOperand(const MachineOperand &MO, |
| 36 | MCSymbol *Sym) const { |
| 37 | |
| 38 | const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx); |
| 39 | |
| 40 | if (!MO.isJTI() && MO.getOffset()) |
| 41 | llvm_unreachable("unknown symbol op"); |
| 42 | |
| 43 | return MCOperand::createExpr(Expr); |
| 44 | } |
| 45 | |
| 46 | void WebAssemblyMCInstLower::Lower(const MachineInstr *MI, |
| 47 | 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: |
| 56 | MI->dump(); |
| 57 | llvm_unreachable("unknown operand type"); |
| 58 | case MachineOperand::MO_Register: |
| 59 | // Ignore all implicit register operands. |
| 60 | if (MO.isImplicit()) |
| 61 | continue; |
| 62 | MCOp = MCOperand::createReg(MO.getReg()); |
| 63 | break; |
| 64 | case MachineOperand::MO_Immediate: |
| 65 | MCOp = MCOperand::createImm(MO.getImm()); |
| 66 | break; |
| 67 | case MachineOperand::MO_FPImmediate: |
| 68 | MCOp = MCOperand::createFPImm( |
| 69 | MO.getFPImm()->getValueAPF().convertToDouble()); |
| 70 | break; |
| 71 | case MachineOperand::MO_MachineBasicBlock: |
| 72 | MCOp = MCOperand::createExpr( |
| 73 | MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx)); |
| 74 | break; |
| 75 | case MachineOperand::MO_RegisterMask: |
| 76 | continue; |
| 77 | case MachineOperand::MO_GlobalAddress: |
| 78 | MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO)); |
| 79 | break; |
| 80 | } |
| 81 | |
| 82 | OutMI.addOperand(MCOp); |
| 83 | } |
| 84 | } |