blob: 4226a5385ce566600a1ad776d7858a06fdfda1c4 [file] [log] [blame]
Dan Gohmane9361d52015-11-05 19:28:16 +00001// 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 Gohmancf4748f2015-11-12 17:04:33 +000017#include "WebAssemblyMachineFunctionInfo.h"
Dan Gohmane9361d52015-11-05 19:28:16 +000018#include "llvm/ADT/SmallString.h"
Dan Gohmane9361d52015-11-05 19:28:16 +000019#include "llvm/CodeGen/AsmPrinter.h"
20#include "llvm/CodeGen/MachineBasicBlock.h"
Dan Gohmancf4748f2015-11-12 17:04:33 +000021#include "llvm/CodeGen/MachineFunction.h"
Dan Gohmane9361d52015-11-05 19:28:16 +000022#include "llvm/CodeGen/MachineInstr.h"
Dan Gohmancf4748f2015-11-12 17:04:33 +000023#include "llvm/IR/Constants.h"
Dan Gohmane9361d52015-11-05 19:28:16 +000024#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"
30using namespace llvm;
31
32MCSymbol *
33WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {
34 return Printer.getSymbol(MO.getGlobal());
35}
36
37MCOperand WebAssemblyMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
38 MCSymbol *Sym) const {
39
40 const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx);
41
42 if (!MO.isJTI() && MO.getOffset())
43 llvm_unreachable("unknown symbol op");
44
45 return MCOperand::createExpr(Expr);
46}
47
48void WebAssemblyMCInstLower::Lower(const MachineInstr *MI,
49 MCInst &OutMI) const {
50 OutMI.setOpcode(MI->getOpcode());
51
52 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
53 const MachineOperand &MO = MI->getOperand(i);
54
55 MCOperand MCOp;
56 switch (MO.getType()) {
57 default:
58 MI->dump();
59 llvm_unreachable("unknown operand type");
Dan Gohmancf4748f2015-11-12 17:04:33 +000060 case MachineOperand::MO_Register: {
Dan Gohmane9361d52015-11-05 19:28:16 +000061 // Ignore all implicit register operands.
62 if (MO.isImplicit())
63 continue;
Dan Gohmancf4748f2015-11-12 17:04:33 +000064 // TODO: Handle physical registers.
65 const WebAssemblyFunctionInfo &MFI =
66 *MI->getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>();
67 unsigned WAReg = MFI.getWAReg(MO.getReg());
68 MCOp = MCOperand::createReg(WAReg);
Dan Gohmane9361d52015-11-05 19:28:16 +000069 break;
Dan Gohmancf4748f2015-11-12 17:04:33 +000070 }
Dan Gohmane9361d52015-11-05 19:28:16 +000071 case MachineOperand::MO_Immediate:
72 MCOp = MCOperand::createImm(MO.getImm());
73 break;
Dan Gohmancf4748f2015-11-12 17:04:33 +000074 case MachineOperand::MO_FPImmediate: {
75 // TODO: MC converts all floating point immediate operands to double.
76 // This is fine for numeric values, but may cause NaNs to change bits.
77 const ConstantFP *Imm = MO.getFPImm();
78 if (Imm->getType()->isFloatTy())
79 MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToFloat());
80 else if (Imm->getType()->isDoubleTy())
81 MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToDouble());
82 else
83 llvm_unreachable("unknown floating point immediate type");
Dan Gohmane9361d52015-11-05 19:28:16 +000084 break;
Dan Gohmancf4748f2015-11-12 17:04:33 +000085 }
Dan Gohmane9361d52015-11-05 19:28:16 +000086 case MachineOperand::MO_MachineBasicBlock:
87 MCOp = MCOperand::createExpr(
88 MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx));
89 break;
Dan Gohmane9361d52015-11-05 19:28:16 +000090 case MachineOperand::MO_GlobalAddress:
91 MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
92 break;
93 }
94
95 OutMI.addOperand(MCOp);
96 }
97}