1 // WebAssemblyMCInstLower.cpp - Convert WebAssembly MachineInstr to an MCInst //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 /// This file contains code to lower WebAssembly MachineInstrs to their
11 /// corresponding MCInst records.
13 //===----------------------------------------------------------------------===//
15 #include "WebAssemblyMCInstLower.h"
16 #include "WebAssemblyAsmPrinter.h"
17 #include "WebAssemblyMachineFunctionInfo.h"
18 #include "WebAssemblyRuntimeLibcallSignatures.h"
19 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
20 #include "llvm/CodeGen/AsmPrinter.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/IR/Constants.h"
23 #include "llvm/MC/MCAsmInfo.h"
24 #include "llvm/MC/MCContext.h"
25 #include "llvm/MC/MCExpr.h"
26 #include "llvm/MC/MCInst.h"
27 #include "llvm/MC/MCSymbolWasm.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/raw_ostream.h"
32 // Defines llvm::WebAssembly::getStackOpcode to convert register instructions to
34 #define GET_INSTRMAP_INFO 1
35 #include "WebAssemblyGenInstrInfo.inc"
37 // This disables the removal of registers when lowering into MC, as required
38 // by some current tests.
40 WasmKeepRegisters("wasm-keep-registers", cl::Hidden
,
41 cl::desc("WebAssembly: output stack registers in"
42 " instruction output for test purposes only."),
45 static void removeRegisterOperands(const MachineInstr
*MI
, MCInst
&OutMI
);
48 WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand
&MO
) const {
49 const GlobalValue
*Global
= MO
.getGlobal();
50 auto *WasmSym
= cast
<MCSymbolWasm
>(Printer
.getSymbol(Global
));
52 if (const auto *FuncTy
= dyn_cast
<FunctionType
>(Global
->getValueType())) {
53 const MachineFunction
&MF
= *MO
.getParent()->getParent()->getParent();
54 const TargetMachine
&TM
= MF
.getTarget();
55 const Function
&CurrentFunc
= MF
.getFunction();
57 SmallVector
<MVT
, 1> ResultMVTs
;
58 SmallVector
<MVT
, 4> ParamMVTs
;
59 computeSignatureVTs(FuncTy
, CurrentFunc
, TM
, ParamMVTs
, ResultMVTs
);
61 auto Signature
= signatureFromMVTs(ResultMVTs
, ParamMVTs
);
62 WasmSym
->setSignature(Signature
.get());
63 Printer
.addSignature(std::move(Signature
));
64 WasmSym
->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION
);
70 MCSymbol
*WebAssemblyMCInstLower::GetExternalSymbolSymbol(
71 const MachineOperand
&MO
) const {
72 const char *Name
= MO
.getSymbolName();
73 auto *WasmSym
= cast
<MCSymbolWasm
>(Printer
.GetExternalSymbolSymbol(Name
));
74 const WebAssemblySubtarget
&Subtarget
= Printer
.getSubtarget();
76 // Except for certain known symbols, all symbols used by CodeGen are
77 // functions. It's OK to hardcode knowledge of specific symbols here; this
78 // method is precisely there for fetching the signatures of known
79 // Clang-provided symbols.
80 if (strcmp(Name
, "__stack_pointer") == 0 || strcmp(Name
, "__tls_base") == 0 ||
81 strcmp(Name
, "__memory_base") == 0 || strcmp(Name
, "__table_base") == 0 ||
82 strcmp(Name
, "__tls_size") == 0 || strcmp(Name
, "__tls_align") == 0) {
84 strcmp(Name
, "__stack_pointer") == 0 || strcmp(Name
, "__tls_base") == 0;
85 WasmSym
->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL
);
86 WasmSym
->setGlobalType(wasm::WasmGlobalType
{
87 uint8_t(Subtarget
.hasAddr64() ? wasm::WASM_TYPE_I64
88 : wasm::WASM_TYPE_I32
),
93 SmallVector
<wasm::ValType
, 4> Returns
;
94 SmallVector
<wasm::ValType
, 4> Params
;
95 if (strcmp(Name
, "__cpp_exception") == 0) {
96 WasmSym
->setType(wasm::WASM_SYMBOL_TYPE_EVENT
);
97 // We can't confirm its signature index for now because there can be
98 // imported exceptions. Set it to be 0 for now.
99 WasmSym
->setEventType(
100 {wasm::WASM_EVENT_ATTRIBUTE_EXCEPTION
, /* SigIndex */ 0});
101 // We may have multiple C++ compilation units to be linked together, each of
102 // which defines the exception symbol. To resolve them, we declare them as
104 WasmSym
->setWeak(true);
105 WasmSym
->setExternal(true);
107 // All C++ exceptions are assumed to have a single i32 (for wasm32) or i64
108 // (for wasm64) param type and void return type. The reaon is, all C++
109 // exception values are pointers, and to share the type section with
110 // functions, exceptions are assumed to have void return type.
111 Params
.push_back(Subtarget
.hasAddr64() ? wasm::ValType::I64
112 : wasm::ValType::I32
);
113 } else { // Function symbols
114 WasmSym
->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION
);
115 getLibcallSignature(Subtarget
, Name
, Returns
, Params
);
118 std::make_unique
<wasm::WasmSignature
>(std::move(Returns
), std::move(Params
));
119 WasmSym
->setSignature(Signature
.get());
120 Printer
.addSignature(std::move(Signature
));
125 MCOperand
WebAssemblyMCInstLower::lowerSymbolOperand(const MachineOperand
&MO
,
126 MCSymbol
*Sym
) const {
127 MCSymbolRefExpr::VariantKind Kind
= MCSymbolRefExpr::VK_None
;
128 unsigned TargetFlags
= MO
.getTargetFlags();
130 switch (TargetFlags
) {
131 case WebAssemblyII::MO_NO_FLAG
:
133 case WebAssemblyII::MO_GOT
:
134 Kind
= MCSymbolRefExpr::VK_GOT
;
136 case WebAssemblyII::MO_MEMORY_BASE_REL
:
137 Kind
= MCSymbolRefExpr::VK_WASM_MBREL
;
139 case WebAssemblyII::MO_TABLE_BASE_REL
:
140 Kind
= MCSymbolRefExpr::VK_WASM_TBREL
;
143 llvm_unreachable("Unknown target flag on GV operand");
146 const MCExpr
*Expr
= MCSymbolRefExpr::create(Sym
, Kind
, Ctx
);
148 if (MO
.getOffset() != 0) {
149 const auto *WasmSym
= cast
<MCSymbolWasm
>(Sym
);
150 if (TargetFlags
== WebAssemblyII::MO_GOT
)
151 report_fatal_error("GOT symbol references do not support offsets");
152 if (WasmSym
->isFunction())
153 report_fatal_error("Function addresses with offsets not supported");
154 if (WasmSym
->isGlobal())
155 report_fatal_error("Global indexes with offsets not supported");
156 if (WasmSym
->isEvent())
157 report_fatal_error("Event indexes with offsets not supported");
159 Expr
= MCBinaryExpr::createAdd(
160 Expr
, MCConstantExpr::create(MO
.getOffset(), Ctx
), Ctx
);
163 return MCOperand::createExpr(Expr
);
166 // Return the WebAssembly type associated with the given register class.
167 static wasm::ValType
getType(const TargetRegisterClass
*RC
) {
168 if (RC
== &WebAssembly::I32RegClass
)
169 return wasm::ValType::I32
;
170 if (RC
== &WebAssembly::I64RegClass
)
171 return wasm::ValType::I64
;
172 if (RC
== &WebAssembly::F32RegClass
)
173 return wasm::ValType::F32
;
174 if (RC
== &WebAssembly::F64RegClass
)
175 return wasm::ValType::F64
;
176 if (RC
== &WebAssembly::V128RegClass
)
177 return wasm::ValType::V128
;
178 llvm_unreachable("Unexpected register class");
181 void WebAssemblyMCInstLower::lower(const MachineInstr
*MI
,
182 MCInst
&OutMI
) const {
183 OutMI
.setOpcode(MI
->getOpcode());
185 const MCInstrDesc
&Desc
= MI
->getDesc();
186 for (unsigned I
= 0, E
= MI
->getNumOperands(); I
!= E
; ++I
) {
187 const MachineOperand
&MO
= MI
->getOperand(I
);
190 switch (MO
.getType()) {
193 llvm_unreachable("unknown operand type");
194 case MachineOperand::MO_MachineBasicBlock
:
196 llvm_unreachable("MachineBasicBlock operand should have been rewritten");
197 case MachineOperand::MO_Register
: {
198 // Ignore all implicit register operands.
201 const WebAssemblyFunctionInfo
&MFI
=
202 *MI
->getParent()->getParent()->getInfo
<WebAssemblyFunctionInfo
>();
203 unsigned WAReg
= MFI
.getWAReg(MO
.getReg());
204 MCOp
= MCOperand::createReg(WAReg
);
207 case MachineOperand::MO_Immediate
:
208 if (I
< Desc
.NumOperands
) {
209 const MCOperandInfo
&Info
= Desc
.OpInfo
[I
];
210 if (Info
.OperandType
== WebAssembly::OPERAND_TYPEINDEX
) {
211 MCSymbol
*Sym
= Printer
.createTempSymbol("typeindex");
213 SmallVector
<wasm::ValType
, 4> Returns
;
214 SmallVector
<wasm::ValType
, 4> Params
;
216 const MachineRegisterInfo
&MRI
=
217 MI
->getParent()->getParent()->getRegInfo();
218 for (const MachineOperand
&MO
: MI
->defs())
219 Returns
.push_back(getType(MRI
.getRegClass(MO
.getReg())));
220 for (const MachineOperand
&MO
: MI
->explicit_uses())
222 Params
.push_back(getType(MRI
.getRegClass(MO
.getReg())));
224 // call_indirect instructions have a callee operand at the end which
225 // doesn't count as a param.
226 if (WebAssembly::isCallIndirect(MI
->getOpcode()))
229 // return_call_indirect instructions have the return type of the
231 if (MI
->getOpcode() == WebAssembly::RET_CALL_INDIRECT
) {
232 const Function
&F
= MI
->getMF()->getFunction();
233 const TargetMachine
&TM
= MI
->getMF()->getTarget();
234 Type
*RetTy
= F
.getReturnType();
235 SmallVector
<MVT
, 4> CallerRetTys
;
236 computeLegalValueVTs(F
, TM
, RetTy
, CallerRetTys
);
237 valTypesFromMVTs(CallerRetTys
, Returns
);
240 auto *WasmSym
= cast
<MCSymbolWasm
>(Sym
);
241 auto Signature
= std::make_unique
<wasm::WasmSignature
>(std::move(Returns
),
243 WasmSym
->setSignature(Signature
.get());
244 Printer
.addSignature(std::move(Signature
));
245 WasmSym
->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION
);
247 const MCExpr
*Expr
= MCSymbolRefExpr::create(
248 WasmSym
, MCSymbolRefExpr::VK_WASM_TYPEINDEX
, Ctx
);
249 MCOp
= MCOperand::createExpr(Expr
);
253 MCOp
= MCOperand::createImm(MO
.getImm());
255 case MachineOperand::MO_FPImmediate
: {
256 // TODO: MC converts all floating point immediate operands to double.
257 // This is fine for numeric values, but may cause NaNs to change bits.
258 const ConstantFP
*Imm
= MO
.getFPImm();
259 if (Imm
->getType()->isFloatTy())
260 MCOp
= MCOperand::createFPImm(Imm
->getValueAPF().convertToFloat());
261 else if (Imm
->getType()->isDoubleTy())
262 MCOp
= MCOperand::createFPImm(Imm
->getValueAPF().convertToDouble());
264 llvm_unreachable("unknown floating point immediate type");
267 case MachineOperand::MO_GlobalAddress
:
268 MCOp
= lowerSymbolOperand(MO
, GetGlobalAddressSymbol(MO
));
270 case MachineOperand::MO_ExternalSymbol
:
271 // The target flag indicates whether this is a symbol for a
272 // variable or a function.
273 assert(MO
.getTargetFlags() == 0 &&
274 "WebAssembly uses only symbol flags on ExternalSymbols");
275 MCOp
= lowerSymbolOperand(MO
, GetExternalSymbolSymbol(MO
));
277 case MachineOperand::MO_MCSymbol
:
278 // This is currently used only for LSDA symbols (GCC_except_table),
279 // because global addresses or other external symbols are handled above.
280 assert(MO
.getTargetFlags() == 0 &&
281 "WebAssembly does not use target flags on MCSymbol");
282 MCOp
= lowerSymbolOperand(MO
, MO
.getMCSymbol());
286 OutMI
.addOperand(MCOp
);
289 if (!WasmKeepRegisters
)
290 removeRegisterOperands(MI
, OutMI
);
293 static void removeRegisterOperands(const MachineInstr
*MI
, MCInst
&OutMI
) {
294 // Remove all uses of stackified registers to bring the instruction format
295 // into its final stack form used thruout MC, and transition opcodes to
297 // We do this seperate from the above code that still may need these
298 // registers for e.g. call_indirect signatures.
299 // See comments in lib/Target/WebAssembly/WebAssemblyInstrFormats.td for
301 // TODO: the code above creates new registers which are then removed here.
302 // That code could be slightly simplified by not doing that, though maybe
303 // it is simpler conceptually to keep the code above in "register mode"
304 // until this transition point.
305 // FIXME: we are not processing inline assembly, which contains register
306 // operands, because it is used by later target generic code.
307 if (MI
->isDebugInstr() || MI
->isLabel() || MI
->isInlineAsm())
310 // Transform to _S instruction.
311 auto RegOpcode
= OutMI
.getOpcode();
312 auto StackOpcode
= WebAssembly::getStackOpcode(RegOpcode
);
313 assert(StackOpcode
!= -1 && "Failed to stackify instruction");
314 OutMI
.setOpcode(StackOpcode
);
316 // Remove register operands.
317 for (auto I
= OutMI
.getNumOperands(); I
; --I
) {
318 auto &MO
= OutMI
.getOperand(I
- 1);