blob: 022a448590ecea893f770e775297f8519358567f [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/CodeGen/AsmPrinter.h"
Dan Gohmancf4748f2015-11-12 17:04:33 +000019#include "llvm/CodeGen/MachineFunction.h"
Dan Gohmancf4748f2015-11-12 17:04:33 +000020#include "llvm/IR/Constants.h"
Dan Gohmane9361d52015-11-05 19:28:16 +000021#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"
27using namespace llvm;
28
29MCSymbol *
30WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {
31 return Printer.getSymbol(MO.getGlobal());
32}
33
Dan Gohman7a6b9822015-11-29 22:32:02 +000034MCSymbol *WebAssemblyMCInstLower::GetExternalSymbolSymbol(
35 const MachineOperand &MO) const {
Dan Gohman2c8fe6a2015-11-25 16:44:29 +000036 return Printer.GetExternalSymbolSymbol(MO.getSymbolName());
37}
38
Dan Gohman26c67652016-01-11 23:38:05 +000039MCOperand WebAssemblyMCInstLower::LowerSymbolOperand(MCSymbol *Sym,
40 int64_t Offset,
41 bool IsFunc) const {
42 MCSymbolRefExpr::VariantKind VK =
43 IsFunc ? MCSymbolRefExpr::VK_WebAssembly_FUNCTION
44 : MCSymbolRefExpr::VK_None;
45 const MCExpr *Expr = MCSymbolRefExpr::create(Sym, VK, Ctx);
Dan Gohmane9361d52015-11-05 19:28:16 +000046
Dan Gohmana4b710a2015-12-06 19:33:32 +000047 if (Offset != 0) {
Dan Gohman26c67652016-01-11 23:38:05 +000048 if (IsFunc)
49 report_fatal_error("Function addresses with offsets not supported");
Dan Gohmana4b710a2015-12-06 19:33:32 +000050 Expr =
51 MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(Offset, Ctx), Ctx);
52 }
Dan Gohmane9361d52015-11-05 19:28:16 +000053
54 return MCOperand::createExpr(Expr);
55}
56
57void WebAssemblyMCInstLower::Lower(const MachineInstr *MI,
58 MCInst &OutMI) const {
59 OutMI.setOpcode(MI->getOpcode());
60
61 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
62 const MachineOperand &MO = MI->getOperand(i);
63
64 MCOperand MCOp;
65 switch (MO.getType()) {
66 default:
67 MI->dump();
68 llvm_unreachable("unknown operand type");
Dan Gohman1d68e80f2016-01-12 19:14:46 +000069 case MachineOperand::MO_MachineBasicBlock:
70 MI->dump();
71 llvm_unreachable("MachineBasicBlock operand should have been rewritten");
Dan Gohmancf4748f2015-11-12 17:04:33 +000072 case MachineOperand::MO_Register: {
Dan Gohmane9361d52015-11-05 19:28:16 +000073 // Ignore all implicit register operands.
74 if (MO.isImplicit())
75 continue;
Dan Gohmancf4748f2015-11-12 17:04:33 +000076 const WebAssemblyFunctionInfo &MFI =
77 *MI->getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>();
78 unsigned WAReg = MFI.getWAReg(MO.getReg());
79 MCOp = MCOperand::createReg(WAReg);
Dan Gohmane9361d52015-11-05 19:28:16 +000080 break;
Dan Gohmancf4748f2015-11-12 17:04:33 +000081 }
Dan Gohmane9361d52015-11-05 19:28:16 +000082 case MachineOperand::MO_Immediate:
83 MCOp = MCOperand::createImm(MO.getImm());
84 break;
Dan Gohmancf4748f2015-11-12 17:04:33 +000085 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());
93 else
94 llvm_unreachable("unknown floating point immediate type");
Dan Gohmane9361d52015-11-05 19:28:16 +000095 break;
Dan Gohmancf4748f2015-11-12 17:04:33 +000096 }
Dan Gohmane9361d52015-11-05 19:28:16 +000097 case MachineOperand::MO_GlobalAddress:
Dan Gohman26c67652016-01-11 23:38:05 +000098 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());
Dan Gohmane9361d52015-11-05 19:28:16 +0000102 break;
Dan Gohman2c8fe6a2015-11-25 16:44:29 +0000103 case MachineOperand::MO_ExternalSymbol:
Dan Gohman26c67652016-01-11 23:38:05 +0000104 // 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);
Dan Gohman2c8fe6a2015-11-25 16:44:29 +0000110 break;
Dan Gohmane9361d52015-11-05 19:28:16 +0000111 }
112
113 OutMI.addOperand(MCOp);
114 }
115}