[WebAssembly] Factor out the list of supported calling conventions.
[oota-llvm.git] / lib / Target / WebAssembly / WebAssemblyMCInstLower.cpp
1 // 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"
17 #include "WebAssemblyMachineFunctionInfo.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/CodeGen/AsmPrinter.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/IR/Constants.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"
28 using namespace llvm;
29
30 MCSymbol *
31 WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {
32   return Printer.getSymbol(MO.getGlobal());
33 }
34
35 MCSymbol *WebAssemblyMCInstLower::GetExternalSymbolSymbol(
36     const MachineOperand &MO) const {
37   return Printer.GetExternalSymbolSymbol(MO.getSymbolName());
38 }
39
40 MCOperand WebAssemblyMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
41                                                      MCSymbol *Sym) const {
42
43   const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx);
44
45   if (!MO.isJTI() && MO.getOffset())
46     llvm_unreachable("unknown symbol op");
47
48   return MCOperand::createExpr(Expr);
49 }
50
51 void WebAssemblyMCInstLower::Lower(const MachineInstr *MI,
52                                    MCInst &OutMI) const {
53   OutMI.setOpcode(MI->getOpcode());
54
55   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
56     const MachineOperand &MO = MI->getOperand(i);
57
58     MCOperand MCOp;
59     switch (MO.getType()) {
60     default:
61       MI->dump();
62       llvm_unreachable("unknown operand type");
63     case MachineOperand::MO_Register: {
64       // Ignore all implicit register operands.
65       if (MO.isImplicit())
66         continue;
67       const WebAssemblyFunctionInfo &MFI =
68           *MI->getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>();
69       unsigned WAReg = MFI.getWAReg(MO.getReg());
70       MCOp = MCOperand::createReg(WAReg);
71       break;
72     }
73     case MachineOperand::MO_Immediate:
74       MCOp = MCOperand::createImm(MO.getImm());
75       break;
76     case MachineOperand::MO_FPImmediate: {
77       // TODO: MC converts all floating point immediate operands to double.
78       // This is fine for numeric values, but may cause NaNs to change bits.
79       const ConstantFP *Imm = MO.getFPImm();
80       if (Imm->getType()->isFloatTy())
81         MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToFloat());
82       else if (Imm->getType()->isDoubleTy())
83         MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToDouble());
84       else
85         llvm_unreachable("unknown floating point immediate type");
86       break;
87     }
88     case MachineOperand::MO_MachineBasicBlock:
89       MCOp = MCOperand::createExpr(
90           MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx));
91       break;
92     case MachineOperand::MO_GlobalAddress:
93       MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
94       break;
95     case MachineOperand::MO_ExternalSymbol:
96       MCOp = LowerSymbolOperand(MO, GetExternalSymbolSymbol(MO));
97       break;
98     }
99
100     OutMI.addOperand(MCOp);
101   }
102 }