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" |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 17 | #include "WebAssemblyMachineFunctionInfo.h" |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallString.h" |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/AsmPrinter.h" |
| 20 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineFunction.h" |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineInstr.h" |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Constants.h" |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCAsmInfo.h" |
| 25 | #include "llvm/MC/MCContext.h" |
| 26 | #include "llvm/MC/MCExpr.h" |
| 27 | #include "llvm/MC/MCInst.h" |
| 28 | #include "llvm/Support/ErrorHandling.h" |
| 29 | #include "llvm/Support/raw_ostream.h" |
| 30 | using namespace llvm; |
| 31 | |
| 32 | MCSymbol * |
| 33 | WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const { |
| 34 | return Printer.getSymbol(MO.getGlobal()); |
| 35 | } |
| 36 | |
Dan Gohman | 2c8fe6a | 2015-11-25 16:44:29 +0000 | [diff] [blame^] | 37 | MCSymbol * |
| 38 | WebAssemblyMCInstLower::GetExternalSymbolSymbol(const MachineOperand &MO) const { |
| 39 | return Printer.GetExternalSymbolSymbol(MO.getSymbolName()); |
| 40 | } |
| 41 | |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 42 | MCOperand WebAssemblyMCInstLower::LowerSymbolOperand(const MachineOperand &MO, |
| 43 | MCSymbol *Sym) const { |
| 44 | |
| 45 | const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx); |
| 46 | |
| 47 | if (!MO.isJTI() && MO.getOffset()) |
| 48 | llvm_unreachable("unknown symbol op"); |
| 49 | |
| 50 | return MCOperand::createExpr(Expr); |
| 51 | } |
| 52 | |
| 53 | void WebAssemblyMCInstLower::Lower(const MachineInstr *MI, |
| 54 | MCInst &OutMI) const { |
| 55 | OutMI.setOpcode(MI->getOpcode()); |
| 56 | |
| 57 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 58 | const MachineOperand &MO = MI->getOperand(i); |
| 59 | |
| 60 | MCOperand MCOp; |
| 61 | switch (MO.getType()) { |
| 62 | default: |
| 63 | MI->dump(); |
| 64 | llvm_unreachable("unknown operand type"); |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 65 | case MachineOperand::MO_Register: { |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 66 | // Ignore all implicit register operands. |
| 67 | if (MO.isImplicit()) |
| 68 | continue; |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 69 | // TODO: Handle physical registers. |
| 70 | const WebAssemblyFunctionInfo &MFI = |
| 71 | *MI->getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>(); |
| 72 | unsigned WAReg = MFI.getWAReg(MO.getReg()); |
| 73 | MCOp = MCOperand::createReg(WAReg); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 74 | break; |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 75 | } |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 76 | case MachineOperand::MO_Immediate: |
| 77 | MCOp = MCOperand::createImm(MO.getImm()); |
| 78 | break; |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 79 | case MachineOperand::MO_FPImmediate: { |
| 80 | // TODO: MC converts all floating point immediate operands to double. |
| 81 | // This is fine for numeric values, but may cause NaNs to change bits. |
| 82 | const ConstantFP *Imm = MO.getFPImm(); |
| 83 | if (Imm->getType()->isFloatTy()) |
| 84 | MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToFloat()); |
| 85 | else if (Imm->getType()->isDoubleTy()) |
| 86 | MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToDouble()); |
| 87 | else |
| 88 | llvm_unreachable("unknown floating point immediate type"); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 89 | break; |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 90 | } |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 91 | case MachineOperand::MO_MachineBasicBlock: |
| 92 | MCOp = MCOperand::createExpr( |
| 93 | MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx)); |
| 94 | break; |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 95 | case MachineOperand::MO_GlobalAddress: |
| 96 | MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO)); |
| 97 | break; |
Dan Gohman | 2c8fe6a | 2015-11-25 16:44:29 +0000 | [diff] [blame^] | 98 | case MachineOperand::MO_ExternalSymbol: |
| 99 | MCOp = LowerSymbolOperand(MO, GetExternalSymbolSymbol(MO)); |
| 100 | break; |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | OutMI.addOperand(MCOp); |
| 104 | } |
| 105 | } |