1 // WebAssemblyMCInstLower.cpp - Convert WebAssembly MachineInstr to an MCInst //
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This file contains code to lower WebAssembly MachineInstrs to their
12 /// corresponding MCInst records.
14 //===----------------------------------------------------------------------===//
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"
31 WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {
32 return Printer.getSymbol(MO.getGlobal());
35 MCOperand WebAssemblyMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
36 MCSymbol *Sym) const {
38 const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx);
40 if (!MO.isJTI() && MO.getOffset())
41 llvm_unreachable("unknown symbol op");
43 return MCOperand::createExpr(Expr);
46 void WebAssemblyMCInstLower::Lower(const MachineInstr *MI,
47 MCInst &OutMI) const {
48 OutMI.setOpcode(MI->getOpcode());
50 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
51 const MachineOperand &MO = MI->getOperand(i);
54 switch (MO.getType()) {
57 llvm_unreachable("unknown operand type");
58 case MachineOperand::MO_Register:
59 // Ignore all implicit register operands.
62 MCOp = MCOperand::createReg(MO.getReg());
64 case MachineOperand::MO_Immediate:
65 MCOp = MCOperand::createImm(MO.getImm());
67 case MachineOperand::MO_FPImmediate:
68 MCOp = MCOperand::createFPImm(
69 MO.getFPImm()->getValueAPF().convertToDouble());
71 case MachineOperand::MO_MachineBasicBlock:
72 MCOp = MCOperand::createExpr(
73 MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx));
75 case MachineOperand::MO_RegisterMask:
77 case MachineOperand::MO_GlobalAddress:
78 MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
82 OutMI.addOperand(MCOp);