LLVM  4.0.0
WebAssemblyMCInstLower.cpp
Go to the documentation of this file.
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"
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"
27 using namespace llvm;
28 
29 MCSymbol *
30 WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {
31  return Printer.getSymbol(MO.getGlobal());
32 }
33 
34 MCSymbol *WebAssemblyMCInstLower::GetExternalSymbolSymbol(
35  const MachineOperand &MO) const {
36  return Printer.GetExternalSymbolSymbol(MO.getSymbolName());
37 }
38 
39 MCOperand WebAssemblyMCInstLower::LowerSymbolOperand(MCSymbol *Sym,
40  int64_t Offset,
41  bool IsFunc) const {
45  const MCExpr *Expr = MCSymbolRefExpr::create(Sym, VK, Ctx);
46 
47  if (Offset != 0) {
48  if (IsFunc)
49  report_fatal_error("Function addresses with offsets not supported");
50  Expr =
51  MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(Offset, Ctx), Ctx);
52  }
53 
54  return MCOperand::createExpr(Expr);
55 }
56 
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");
70  MI->dump();
71  llvm_unreachable("MachineBasicBlock operand should have been rewritten");
73  // Ignore all implicit register operands.
74  if (MO.isImplicit())
75  continue;
76  const WebAssemblyFunctionInfo &MFI =
78  unsigned WAReg = MFI.getWAReg(MO.getReg());
79  MCOp = MCOperand::createReg(WAReg);
80  break;
81  }
83  MCOp = MCOperand::createImm(MO.getImm());
84  break;
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())
91  else if (Imm->getType()->isDoubleTy())
93  else
94  llvm_unreachable("unknown floating point immediate type");
95  break;
96  }
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());
102  break;
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);
110  break;
111  }
112 
113  OutMI.addOperand(MCOp);
114  }
115 }
bool isImplicit() const
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
const GlobalValue * getGlobal() const
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:298
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:371
size_t i
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
const ConstantFP * getFPImm() const
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
float convertToFloat() const
Definition: APFloat.h:1014
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:129
Type * getValueType() const
Definition: GlobalValue.h:261
This file declares the class to lower WebAssembly MachineInstrs to their corresponding MCInst records...
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
Definition: Type.h:148
MachineBasicBlock reference.
const char * getSymbolName() const
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:111
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
Name of external global symbol.
unsigned getNumOperands() const
Access to explicit operands of the instruction.
Definition: MachineInstr.h:277
unsigned getWAReg(unsigned Reg) const
int64_t getImm() const
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:429
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:150
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:273
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:131
Address of a global value.
unsigned getTargetFlags() const
This file contains the declarations for the subclasses of Constant, which represent the different fla...
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:279
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
Definition: Type.h:145
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:269
uint32_t Offset
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
int64_t getOffset() const
Return the offset from the symbol in this operand.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void setOpcode(unsigned Op)
Definition: MCInst.h:158
bool isFunctionTy() const
True if this is an instance of FunctionType.
Definition: Type.h:204
MachineOperand class - Representation of each machine instruction operand.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:230
void dump(const TargetInstrInfo *TII=nullptr) const
static MCOperand createFPImm(double Val)
Definition: MCInst.h:123
void Lower(const MachineInstr *MI, MCInst &OutMI) const
double convertToDouble() const
Definition: APFloat.h:1013
Representation of each machine instruction.
Definition: MachineInstr.h:52
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
This class is derived from MachineFunctionInfo and contains private WebAssembly-specific information ...
This file declares WebAssembly-specific per-machine-function information.
const APFloat & getValueAPF() const
Definition: Constants.h:300
unsigned getReg() const
getReg - Returns the register number.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Floating-point immediate operand.
IRTranslator LLVM IR MI
void addOperand(const MCOperand &Op)
Definition: MCInst.h:168
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:33
MCSymbol * GetExternalSymbolSymbol(StringRef Sym) const
Return the MCSymbol for the specified ExternalSymbol.
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:117
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:149