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(MCSymbol *Sym,
42 MCSymbolRefExpr::VariantKind VK =
43 IsFunc ? MCSymbolRefExpr::VK_WebAssembly_FUNCTION
44 : MCSymbolRefExpr::VK_None;
45 const MCExpr *Expr = MCSymbolRefExpr::create(Sym, VK, Ctx);
49 report_fatal_error("Function addresses with offsets not supported");
51 MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(Offset, Ctx), Ctx);
54 return MCOperand::createExpr(Expr);
57 void WebAssemblyMCInstLower::Lower(const MachineInstr *MI,
58 MCInst &OutMI) const {
59 OutMI.setOpcode(MI->getOpcode());
61 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
62 const MachineOperand &MO = MI->getOperand(i);
65 switch (MO.getType()) {
68 llvm_unreachable("unknown operand type");
69 case MachineOperand::MO_MachineBasicBlock:
71 llvm_unreachable("MachineBasicBlock operand should have been rewritten");
72 case MachineOperand::MO_Register: {
73 // Ignore all implicit register operands.
76 const WebAssemblyFunctionInfo &MFI =
77 *MI->getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>();
78 unsigned WAReg = MFI.getWAReg(MO.getReg());
79 MCOp = MCOperand::createReg(WAReg);
82 case MachineOperand::MO_Immediate:
83 MCOp = MCOperand::createImm(MO.getImm());
85 case MachineOperand::MO_FPImmediate: {
86 // TODO: MC converts all floating point immediate operands to double.
87 // This is fine for numeric values, but may cause NaNs to change bits.
88 const ConstantFP *Imm = MO.getFPImm();
89 if (Imm->getType()->isFloatTy())
90 MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToFloat());
91 else if (Imm->getType()->isDoubleTy())
92 MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToDouble());
94 llvm_unreachable("unknown floating point immediate type");
97 case MachineOperand::MO_GlobalAddress:
98 assert(MO.getTargetFlags() == 0 &&
99 "WebAssembly does not use target flags on GlobalAddresses");
100 MCOp = LowerSymbolOperand(GetGlobalAddressSymbol(MO), MO.getOffset(),
101 MO.getGlobal()->getValueType()->isFunctionTy());
103 case MachineOperand::MO_ExternalSymbol:
104 // The target flag indicates whether this is a symbol for a
105 // variable or a function.
106 assert((MO.getTargetFlags() & -2) == 0 &&
107 "WebAssembly uses only one target flag bit on ExternalSymbols");
108 MCOp = LowerSymbolOperand(GetExternalSymbolSymbol(MO), /*Offset=*/0,
109 MO.getTargetFlags() & 1);
113 OutMI.addOperand(MCOp);