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 "WebAssemblyMachineFunctionInfo.h"
18 #include "llvm/CodeGen/AsmPrinter.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/MC/MCAsmInfo.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCExpr.h"
24 #include "llvm/MC/MCInst.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/raw_ostream.h"
30 WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {
31 return Printer.getSymbol(MO.getGlobal());
34 MCSymbol *WebAssemblyMCInstLower::GetExternalSymbolSymbol(
35 const MachineOperand &MO) const {
36 return Printer.GetExternalSymbolSymbol(MO.getSymbolName());
39 MCOperand WebAssemblyMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
40 MCSymbol *Sym) const {
41 assert(MO.getTargetFlags() == 0 && "WebAssembly does not use target flags");
43 const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx);
45 int64_t Offset = MO.getOffset();
47 assert(!MO.isJTI() && "Unexpected offset with jump table index");
49 MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(Offset, Ctx), Ctx);
52 return MCOperand::createExpr(Expr);
55 void WebAssemblyMCInstLower::Lower(const MachineInstr *MI,
56 MCInst &OutMI) const {
57 OutMI.setOpcode(MI->getOpcode());
59 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
60 const MachineOperand &MO = MI->getOperand(i);
63 switch (MO.getType()) {
66 llvm_unreachable("unknown operand type");
67 case MachineOperand::MO_Register: {
68 // Ignore all implicit register operands.
71 const WebAssemblyFunctionInfo &MFI =
72 *MI->getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>();
73 unsigned WAReg = MFI.getWAReg(MO.getReg());
74 MCOp = MCOperand::createReg(WAReg);
77 case MachineOperand::MO_Immediate:
78 MCOp = MCOperand::createImm(MO.getImm());
80 case MachineOperand::MO_FPImmediate: {
81 // TODO: MC converts all floating point immediate operands to double.
82 // This is fine for numeric values, but may cause NaNs to change bits.
83 const ConstantFP *Imm = MO.getFPImm();
84 if (Imm->getType()->isFloatTy())
85 MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToFloat());
86 else if (Imm->getType()->isDoubleTy())
87 MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToDouble());
89 llvm_unreachable("unknown floating point immediate type");
92 case MachineOperand::MO_MachineBasicBlock:
93 MCOp = MCOperand::createExpr(
94 MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx));
96 case MachineOperand::MO_GlobalAddress:
97 MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
99 case MachineOperand::MO_ExternalSymbol:
100 MCOp = LowerSymbolOperand(MO, GetExternalSymbolSymbol(MO));
104 OutMI.addOperand(MCOp);