xref: /freebsd/contrib/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
10b57cec5SDimitry Andric // WebAssemblyMCInstLower.cpp - Convert WebAssembly MachineInstr to an MCInst //
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric ///
90b57cec5SDimitry Andric /// \file
100b57cec5SDimitry Andric /// This file contains code to lower WebAssembly MachineInstrs to their
110b57cec5SDimitry Andric /// corresponding MCInst records.
120b57cec5SDimitry Andric ///
130b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #include "WebAssemblyMCInstLower.h"
165ffd83dbSDimitry Andric #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
175ffd83dbSDimitry Andric #include "TargetInfo/WebAssemblyTargetInfo.h"
180b57cec5SDimitry Andric #include "WebAssemblyAsmPrinter.h"
190b57cec5SDimitry Andric #include "WebAssemblyMachineFunctionInfo.h"
200b57cec5SDimitry Andric #include "WebAssemblyRuntimeLibcallSignatures.h"
210b57cec5SDimitry Andric #include "llvm/CodeGen/AsmPrinter.h"
220b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
230b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
240b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
250b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
260b57cec5SDimitry Andric #include "llvm/MC/MCExpr.h"
270b57cec5SDimitry Andric #include "llvm/MC/MCInst.h"
280b57cec5SDimitry Andric #include "llvm/MC/MCSymbolWasm.h"
290b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
300b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
310b57cec5SDimitry Andric using namespace llvm;
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric // This disables the removal of registers when lowering into MC, as required
340b57cec5SDimitry Andric // by some current tests.
350b57cec5SDimitry Andric cl::opt<bool>
360b57cec5SDimitry Andric     WasmKeepRegisters("wasm-keep-registers", cl::Hidden,
370b57cec5SDimitry Andric                       cl::desc("WebAssembly: output stack registers in"
380b57cec5SDimitry Andric                                " instruction output for test purposes only."),
390b57cec5SDimitry Andric                       cl::init(false));
400b57cec5SDimitry Andric 
41*e8d8bef9SDimitry Andric extern cl::opt<bool> EnableEmException;
42*e8d8bef9SDimitry Andric extern cl::opt<bool> EnableEmSjLj;
43*e8d8bef9SDimitry Andric 
440b57cec5SDimitry Andric static void removeRegisterOperands(const MachineInstr *MI, MCInst &OutMI);
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric MCSymbol *
470b57cec5SDimitry Andric WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {
480b57cec5SDimitry Andric   const GlobalValue *Global = MO.getGlobal();
49*e8d8bef9SDimitry Andric   if (!isa<Function>(Global))
50*e8d8bef9SDimitry Andric     return cast<MCSymbolWasm>(Printer.getSymbol(Global));
510b57cec5SDimitry Andric 
52*e8d8bef9SDimitry Andric   const auto *FuncTy = cast<FunctionType>(Global->getValueType());
530b57cec5SDimitry Andric   const MachineFunction &MF = *MO.getParent()->getParent()->getParent();
540b57cec5SDimitry Andric   const TargetMachine &TM = MF.getTarget();
550b57cec5SDimitry Andric   const Function &CurrentFunc = MF.getFunction();
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric   SmallVector<MVT, 1> ResultMVTs;
580b57cec5SDimitry Andric   SmallVector<MVT, 4> ParamMVTs;
595ffd83dbSDimitry Andric   const auto *const F = dyn_cast<Function>(Global);
605ffd83dbSDimitry Andric   computeSignatureVTs(FuncTy, F, CurrentFunc, TM, ParamMVTs, ResultMVTs);
610b57cec5SDimitry Andric   auto Signature = signatureFromMVTs(ResultMVTs, ParamMVTs);
62*e8d8bef9SDimitry Andric 
63*e8d8bef9SDimitry Andric   bool InvokeDetected = false;
64*e8d8bef9SDimitry Andric   auto *WasmSym = Printer.getMCSymbolForFunction(
65*e8d8bef9SDimitry Andric       F, EnableEmException || EnableEmSjLj, Signature.get(), InvokeDetected);
660b57cec5SDimitry Andric   WasmSym->setSignature(Signature.get());
670b57cec5SDimitry Andric   Printer.addSignature(std::move(Signature));
680b57cec5SDimitry Andric   WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
690b57cec5SDimitry Andric   return WasmSym;
700b57cec5SDimitry Andric }
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric MCSymbol *WebAssemblyMCInstLower::GetExternalSymbolSymbol(
730b57cec5SDimitry Andric     const MachineOperand &MO) const {
740b57cec5SDimitry Andric   const char *Name = MO.getSymbolName();
750b57cec5SDimitry Andric   auto *WasmSym = cast<MCSymbolWasm>(Printer.GetExternalSymbolSymbol(Name));
760b57cec5SDimitry Andric   const WebAssemblySubtarget &Subtarget = Printer.getSubtarget();
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric   // Except for certain known symbols, all symbols used by CodeGen are
790b57cec5SDimitry Andric   // functions. It's OK to hardcode knowledge of specific symbols here; this
800b57cec5SDimitry Andric   // method is precisely there for fetching the signatures of known
810b57cec5SDimitry Andric   // Clang-provided symbols.
820b57cec5SDimitry Andric   if (strcmp(Name, "__stack_pointer") == 0 || strcmp(Name, "__tls_base") == 0 ||
830b57cec5SDimitry Andric       strcmp(Name, "__memory_base") == 0 || strcmp(Name, "__table_base") == 0 ||
848bcb0991SDimitry Andric       strcmp(Name, "__tls_size") == 0 || strcmp(Name, "__tls_align") == 0) {
850b57cec5SDimitry Andric     bool Mutable =
860b57cec5SDimitry Andric         strcmp(Name, "__stack_pointer") == 0 || strcmp(Name, "__tls_base") == 0;
870b57cec5SDimitry Andric     WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
880b57cec5SDimitry Andric     WasmSym->setGlobalType(wasm::WasmGlobalType{
895ffd83dbSDimitry Andric         uint8_t(Subtarget.hasAddr64() && strcmp(Name, "__table_base") != 0
905ffd83dbSDimitry Andric                     ? wasm::WASM_TYPE_I64
910b57cec5SDimitry Andric                     : wasm::WASM_TYPE_I32),
920b57cec5SDimitry Andric         Mutable});
930b57cec5SDimitry Andric     return WasmSym;
940b57cec5SDimitry Andric   }
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric   SmallVector<wasm::ValType, 4> Returns;
970b57cec5SDimitry Andric   SmallVector<wasm::ValType, 4> Params;
980b57cec5SDimitry Andric   if (strcmp(Name, "__cpp_exception") == 0) {
990b57cec5SDimitry Andric     WasmSym->setType(wasm::WASM_SYMBOL_TYPE_EVENT);
1000b57cec5SDimitry Andric     // We can't confirm its signature index for now because there can be
1010b57cec5SDimitry Andric     // imported exceptions. Set it to be 0 for now.
1020b57cec5SDimitry Andric     WasmSym->setEventType(
1030b57cec5SDimitry Andric         {wasm::WASM_EVENT_ATTRIBUTE_EXCEPTION, /* SigIndex */ 0});
1040b57cec5SDimitry Andric     // We may have multiple C++ compilation units to be linked together, each of
1050b57cec5SDimitry Andric     // which defines the exception symbol. To resolve them, we declare them as
1060b57cec5SDimitry Andric     // weak.
1070b57cec5SDimitry Andric     WasmSym->setWeak(true);
1080b57cec5SDimitry Andric     WasmSym->setExternal(true);
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric     // All C++ exceptions are assumed to have a single i32 (for wasm32) or i64
1110b57cec5SDimitry Andric     // (for wasm64) param type and void return type. The reaon is, all C++
1120b57cec5SDimitry Andric     // exception values are pointers, and to share the type section with
1130b57cec5SDimitry Andric     // functions, exceptions are assumed to have void return type.
1140b57cec5SDimitry Andric     Params.push_back(Subtarget.hasAddr64() ? wasm::ValType::I64
1150b57cec5SDimitry Andric                                            : wasm::ValType::I32);
1160b57cec5SDimitry Andric   } else { // Function symbols
1170b57cec5SDimitry Andric     WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
1180b57cec5SDimitry Andric     getLibcallSignature(Subtarget, Name, Returns, Params);
1190b57cec5SDimitry Andric   }
1200b57cec5SDimitry Andric   auto Signature =
1218bcb0991SDimitry Andric       std::make_unique<wasm::WasmSignature>(std::move(Returns), std::move(Params));
1220b57cec5SDimitry Andric   WasmSym->setSignature(Signature.get());
1230b57cec5SDimitry Andric   Printer.addSignature(std::move(Signature));
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric   return WasmSym;
1260b57cec5SDimitry Andric }
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric MCOperand WebAssemblyMCInstLower::lowerSymbolOperand(const MachineOperand &MO,
1290b57cec5SDimitry Andric                                                      MCSymbol *Sym) const {
1300b57cec5SDimitry Andric   MCSymbolRefExpr::VariantKind Kind = MCSymbolRefExpr::VK_None;
1310b57cec5SDimitry Andric   unsigned TargetFlags = MO.getTargetFlags();
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric   switch (TargetFlags) {
1340b57cec5SDimitry Andric     case WebAssemblyII::MO_NO_FLAG:
1350b57cec5SDimitry Andric       break;
1360b57cec5SDimitry Andric     case WebAssemblyII::MO_GOT:
1370b57cec5SDimitry Andric       Kind = MCSymbolRefExpr::VK_GOT;
1380b57cec5SDimitry Andric       break;
1390b57cec5SDimitry Andric     case WebAssemblyII::MO_MEMORY_BASE_REL:
1400b57cec5SDimitry Andric       Kind = MCSymbolRefExpr::VK_WASM_MBREL;
1410b57cec5SDimitry Andric       break;
142*e8d8bef9SDimitry Andric     case WebAssemblyII::MO_TLS_BASE_REL:
143*e8d8bef9SDimitry Andric       Kind = MCSymbolRefExpr::VK_WASM_TLSREL;
144*e8d8bef9SDimitry Andric       break;
1450b57cec5SDimitry Andric     case WebAssemblyII::MO_TABLE_BASE_REL:
1460b57cec5SDimitry Andric       Kind = MCSymbolRefExpr::VK_WASM_TBREL;
1470b57cec5SDimitry Andric       break;
1480b57cec5SDimitry Andric     default:
1490b57cec5SDimitry Andric       llvm_unreachable("Unknown target flag on GV operand");
1500b57cec5SDimitry Andric   }
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric   const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Kind, Ctx);
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric   if (MO.getOffset() != 0) {
1550b57cec5SDimitry Andric     const auto *WasmSym = cast<MCSymbolWasm>(Sym);
1560b57cec5SDimitry Andric     if (TargetFlags == WebAssemblyII::MO_GOT)
1570b57cec5SDimitry Andric       report_fatal_error("GOT symbol references do not support offsets");
1580b57cec5SDimitry Andric     if (WasmSym->isFunction())
1590b57cec5SDimitry Andric       report_fatal_error("Function addresses with offsets not supported");
1600b57cec5SDimitry Andric     if (WasmSym->isGlobal())
1610b57cec5SDimitry Andric       report_fatal_error("Global indexes with offsets not supported");
1620b57cec5SDimitry Andric     if (WasmSym->isEvent())
1630b57cec5SDimitry Andric       report_fatal_error("Event indexes with offsets not supported");
1640b57cec5SDimitry Andric 
1650b57cec5SDimitry Andric     Expr = MCBinaryExpr::createAdd(
1660b57cec5SDimitry Andric         Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
1670b57cec5SDimitry Andric   }
1680b57cec5SDimitry Andric 
1690b57cec5SDimitry Andric   return MCOperand::createExpr(Expr);
1700b57cec5SDimitry Andric }
1710b57cec5SDimitry Andric 
1728bcb0991SDimitry Andric MCOperand WebAssemblyMCInstLower::lowerTypeIndexOperand(
1738bcb0991SDimitry Andric     SmallVector<wasm::ValType, 1> &&Returns,
1748bcb0991SDimitry Andric     SmallVector<wasm::ValType, 4> &&Params) const {
1758bcb0991SDimitry Andric   auto Signature = std::make_unique<wasm::WasmSignature>(std::move(Returns),
1768bcb0991SDimitry Andric                                                          std::move(Params));
1778bcb0991SDimitry Andric   MCSymbol *Sym = Printer.createTempSymbol("typeindex");
1788bcb0991SDimitry Andric   auto *WasmSym = cast<MCSymbolWasm>(Sym);
1798bcb0991SDimitry Andric   WasmSym->setSignature(Signature.get());
1808bcb0991SDimitry Andric   Printer.addSignature(std::move(Signature));
1818bcb0991SDimitry Andric   WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
1828bcb0991SDimitry Andric   const MCExpr *Expr =
1838bcb0991SDimitry Andric       MCSymbolRefExpr::create(WasmSym, MCSymbolRefExpr::VK_WASM_TYPEINDEX, Ctx);
1848bcb0991SDimitry Andric   return MCOperand::createExpr(Expr);
1858bcb0991SDimitry Andric }
1868bcb0991SDimitry Andric 
1870b57cec5SDimitry Andric // Return the WebAssembly type associated with the given register class.
1880b57cec5SDimitry Andric static wasm::ValType getType(const TargetRegisterClass *RC) {
1890b57cec5SDimitry Andric   if (RC == &WebAssembly::I32RegClass)
1900b57cec5SDimitry Andric     return wasm::ValType::I32;
1910b57cec5SDimitry Andric   if (RC == &WebAssembly::I64RegClass)
1920b57cec5SDimitry Andric     return wasm::ValType::I64;
1930b57cec5SDimitry Andric   if (RC == &WebAssembly::F32RegClass)
1940b57cec5SDimitry Andric     return wasm::ValType::F32;
1950b57cec5SDimitry Andric   if (RC == &WebAssembly::F64RegClass)
1960b57cec5SDimitry Andric     return wasm::ValType::F64;
1970b57cec5SDimitry Andric   if (RC == &WebAssembly::V128RegClass)
1980b57cec5SDimitry Andric     return wasm::ValType::V128;
1990b57cec5SDimitry Andric   llvm_unreachable("Unexpected register class");
2000b57cec5SDimitry Andric }
2010b57cec5SDimitry Andric 
2028bcb0991SDimitry Andric static void getFunctionReturns(const MachineInstr *MI,
2038bcb0991SDimitry Andric                                SmallVectorImpl<wasm::ValType> &Returns) {
2048bcb0991SDimitry Andric   const Function &F = MI->getMF()->getFunction();
2058bcb0991SDimitry Andric   const TargetMachine &TM = MI->getMF()->getTarget();
2068bcb0991SDimitry Andric   Type *RetTy = F.getReturnType();
2078bcb0991SDimitry Andric   SmallVector<MVT, 4> CallerRetTys;
2088bcb0991SDimitry Andric   computeLegalValueVTs(F, TM, RetTy, CallerRetTys);
2098bcb0991SDimitry Andric   valTypesFromMVTs(CallerRetTys, Returns);
2108bcb0991SDimitry Andric }
2118bcb0991SDimitry Andric 
2120b57cec5SDimitry Andric void WebAssemblyMCInstLower::lower(const MachineInstr *MI,
2130b57cec5SDimitry Andric                                    MCInst &OutMI) const {
2140b57cec5SDimitry Andric   OutMI.setOpcode(MI->getOpcode());
2150b57cec5SDimitry Andric 
2160b57cec5SDimitry Andric   const MCInstrDesc &Desc = MI->getDesc();
2175ffd83dbSDimitry Andric   unsigned NumVariadicDefs = MI->getNumExplicitDefs() - Desc.getNumDefs();
2180b57cec5SDimitry Andric   for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
2190b57cec5SDimitry Andric     const MachineOperand &MO = MI->getOperand(I);
2200b57cec5SDimitry Andric 
2210b57cec5SDimitry Andric     MCOperand MCOp;
2220b57cec5SDimitry Andric     switch (MO.getType()) {
2230b57cec5SDimitry Andric     default:
2240b57cec5SDimitry Andric       MI->print(errs());
2250b57cec5SDimitry Andric       llvm_unreachable("unknown operand type");
2260b57cec5SDimitry Andric     case MachineOperand::MO_MachineBasicBlock:
2270b57cec5SDimitry Andric       MI->print(errs());
2280b57cec5SDimitry Andric       llvm_unreachable("MachineBasicBlock operand should have been rewritten");
2290b57cec5SDimitry Andric     case MachineOperand::MO_Register: {
2300b57cec5SDimitry Andric       // Ignore all implicit register operands.
2310b57cec5SDimitry Andric       if (MO.isImplicit())
2320b57cec5SDimitry Andric         continue;
2330b57cec5SDimitry Andric       const WebAssemblyFunctionInfo &MFI =
2340b57cec5SDimitry Andric           *MI->getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>();
2350b57cec5SDimitry Andric       unsigned WAReg = MFI.getWAReg(MO.getReg());
2360b57cec5SDimitry Andric       MCOp = MCOperand::createReg(WAReg);
2370b57cec5SDimitry Andric       break;
2380b57cec5SDimitry Andric     }
2395ffd83dbSDimitry Andric     case MachineOperand::MO_Immediate: {
2405ffd83dbSDimitry Andric       unsigned DescIndex = I - NumVariadicDefs;
2415ffd83dbSDimitry Andric       if (DescIndex < Desc.NumOperands) {
2425ffd83dbSDimitry Andric         const MCOperandInfo &Info = Desc.OpInfo[DescIndex];
2430b57cec5SDimitry Andric         if (Info.OperandType == WebAssembly::OPERAND_TYPEINDEX) {
2440b57cec5SDimitry Andric           SmallVector<wasm::ValType, 4> Returns;
2450b57cec5SDimitry Andric           SmallVector<wasm::ValType, 4> Params;
2460b57cec5SDimitry Andric 
2470b57cec5SDimitry Andric           const MachineRegisterInfo &MRI =
2480b57cec5SDimitry Andric               MI->getParent()->getParent()->getRegInfo();
2490b57cec5SDimitry Andric           for (const MachineOperand &MO : MI->defs())
2500b57cec5SDimitry Andric             Returns.push_back(getType(MRI.getRegClass(MO.getReg())));
2510b57cec5SDimitry Andric           for (const MachineOperand &MO : MI->explicit_uses())
2520b57cec5SDimitry Andric             if (MO.isReg())
2530b57cec5SDimitry Andric               Params.push_back(getType(MRI.getRegClass(MO.getReg())));
2540b57cec5SDimitry Andric 
2550b57cec5SDimitry Andric           // call_indirect instructions have a callee operand at the end which
2560b57cec5SDimitry Andric           // doesn't count as a param.
2570b57cec5SDimitry Andric           if (WebAssembly::isCallIndirect(MI->getOpcode()))
2580b57cec5SDimitry Andric             Params.pop_back();
2590b57cec5SDimitry Andric 
2608bcb0991SDimitry Andric           // return_call_indirect instructions have the return type of the
2618bcb0991SDimitry Andric           // caller
2628bcb0991SDimitry Andric           if (MI->getOpcode() == WebAssembly::RET_CALL_INDIRECT)
2638bcb0991SDimitry Andric             getFunctionReturns(MI, Returns);
2640b57cec5SDimitry Andric 
2658bcb0991SDimitry Andric           MCOp = lowerTypeIndexOperand(std::move(Returns), std::move(Params));
2660b57cec5SDimitry Andric           break;
2678bcb0991SDimitry Andric         } else if (Info.OperandType == WebAssembly::OPERAND_SIGNATURE) {
2688bcb0991SDimitry Andric           auto BT = static_cast<WebAssembly::BlockType>(MO.getImm());
2698bcb0991SDimitry Andric           assert(BT != WebAssembly::BlockType::Invalid);
2708bcb0991SDimitry Andric           if (BT == WebAssembly::BlockType::Multivalue) {
2718bcb0991SDimitry Andric             SmallVector<wasm::ValType, 1> Returns;
2728bcb0991SDimitry Andric             getFunctionReturns(MI, Returns);
2738bcb0991SDimitry Andric             MCOp = lowerTypeIndexOperand(std::move(Returns),
2748bcb0991SDimitry Andric                                          SmallVector<wasm::ValType, 4>());
2758bcb0991SDimitry Andric             break;
2768bcb0991SDimitry Andric           }
277*e8d8bef9SDimitry Andric         } else if (Info.OperandType == WebAssembly::OPERAND_HEAPTYPE) {
278*e8d8bef9SDimitry Andric           assert(static_cast<WebAssembly::HeapType>(MO.getImm()) !=
279*e8d8bef9SDimitry Andric                  WebAssembly::HeapType::Invalid);
280*e8d8bef9SDimitry Andric           // With typed function references, this will need a case for type
281*e8d8bef9SDimitry Andric           // index operands.  Otherwise, fall through.
2820b57cec5SDimitry Andric         }
2830b57cec5SDimitry Andric       }
2840b57cec5SDimitry Andric       MCOp = MCOperand::createImm(MO.getImm());
2850b57cec5SDimitry Andric       break;
2865ffd83dbSDimitry Andric     }
2870b57cec5SDimitry Andric     case MachineOperand::MO_FPImmediate: {
2880b57cec5SDimitry Andric       // TODO: MC converts all floating point immediate operands to double.
2890b57cec5SDimitry Andric       // This is fine for numeric values, but may cause NaNs to change bits.
2900b57cec5SDimitry Andric       const ConstantFP *Imm = MO.getFPImm();
2910b57cec5SDimitry Andric       if (Imm->getType()->isFloatTy())
2920b57cec5SDimitry Andric         MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToFloat());
2930b57cec5SDimitry Andric       else if (Imm->getType()->isDoubleTy())
2940b57cec5SDimitry Andric         MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToDouble());
2950b57cec5SDimitry Andric       else
2960b57cec5SDimitry Andric         llvm_unreachable("unknown floating point immediate type");
2970b57cec5SDimitry Andric       break;
2980b57cec5SDimitry Andric     }
2990b57cec5SDimitry Andric     case MachineOperand::MO_GlobalAddress:
3000b57cec5SDimitry Andric       MCOp = lowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
3010b57cec5SDimitry Andric       break;
3020b57cec5SDimitry Andric     case MachineOperand::MO_ExternalSymbol:
3030b57cec5SDimitry Andric       // The target flag indicates whether this is a symbol for a
3040b57cec5SDimitry Andric       // variable or a function.
3050b57cec5SDimitry Andric       assert(MO.getTargetFlags() == 0 &&
3060b57cec5SDimitry Andric              "WebAssembly uses only symbol flags on ExternalSymbols");
3070b57cec5SDimitry Andric       MCOp = lowerSymbolOperand(MO, GetExternalSymbolSymbol(MO));
3080b57cec5SDimitry Andric       break;
3090b57cec5SDimitry Andric     case MachineOperand::MO_MCSymbol:
3100b57cec5SDimitry Andric       // This is currently used only for LSDA symbols (GCC_except_table),
3110b57cec5SDimitry Andric       // because global addresses or other external symbols are handled above.
3120b57cec5SDimitry Andric       assert(MO.getTargetFlags() == 0 &&
3130b57cec5SDimitry Andric              "WebAssembly does not use target flags on MCSymbol");
3140b57cec5SDimitry Andric       MCOp = lowerSymbolOperand(MO, MO.getMCSymbol());
3150b57cec5SDimitry Andric       break;
3160b57cec5SDimitry Andric     }
3170b57cec5SDimitry Andric 
3180b57cec5SDimitry Andric     OutMI.addOperand(MCOp);
3190b57cec5SDimitry Andric   }
3200b57cec5SDimitry Andric 
3210b57cec5SDimitry Andric   if (!WasmKeepRegisters)
3220b57cec5SDimitry Andric     removeRegisterOperands(MI, OutMI);
3235ffd83dbSDimitry Andric   else if (Desc.variadicOpsAreDefs())
3245ffd83dbSDimitry Andric     OutMI.insert(OutMI.begin(), MCOperand::createImm(MI->getNumExplicitDefs()));
3250b57cec5SDimitry Andric }
3260b57cec5SDimitry Andric 
3270b57cec5SDimitry Andric static void removeRegisterOperands(const MachineInstr *MI, MCInst &OutMI) {
3280b57cec5SDimitry Andric   // Remove all uses of stackified registers to bring the instruction format
3290b57cec5SDimitry Andric   // into its final stack form used thruout MC, and transition opcodes to
3300b57cec5SDimitry Andric   // their _S variant.
3315ffd83dbSDimitry Andric   // We do this separate from the above code that still may need these
3320b57cec5SDimitry Andric   // registers for e.g. call_indirect signatures.
3330b57cec5SDimitry Andric   // See comments in lib/Target/WebAssembly/WebAssemblyInstrFormats.td for
3340b57cec5SDimitry Andric   // details.
3350b57cec5SDimitry Andric   // TODO: the code above creates new registers which are then removed here.
3360b57cec5SDimitry Andric   // That code could be slightly simplified by not doing that, though maybe
3370b57cec5SDimitry Andric   // it is simpler conceptually to keep the code above in "register mode"
3380b57cec5SDimitry Andric   // until this transition point.
3390b57cec5SDimitry Andric   // FIXME: we are not processing inline assembly, which contains register
3400b57cec5SDimitry Andric   // operands, because it is used by later target generic code.
3410b57cec5SDimitry Andric   if (MI->isDebugInstr() || MI->isLabel() || MI->isInlineAsm())
3420b57cec5SDimitry Andric     return;
3430b57cec5SDimitry Andric 
3440b57cec5SDimitry Andric   // Transform to _S instruction.
3450b57cec5SDimitry Andric   auto RegOpcode = OutMI.getOpcode();
3460b57cec5SDimitry Andric   auto StackOpcode = WebAssembly::getStackOpcode(RegOpcode);
3470b57cec5SDimitry Andric   assert(StackOpcode != -1 && "Failed to stackify instruction");
3480b57cec5SDimitry Andric   OutMI.setOpcode(StackOpcode);
3490b57cec5SDimitry Andric 
3500b57cec5SDimitry Andric   // Remove register operands.
3510b57cec5SDimitry Andric   for (auto I = OutMI.getNumOperands(); I; --I) {
3520b57cec5SDimitry Andric     auto &MO = OutMI.getOperand(I - 1);
3530b57cec5SDimitry Andric     if (MO.isReg()) {
3540b57cec5SDimitry Andric       OutMI.erase(&MO);
3550b57cec5SDimitry Andric     }
3560b57cec5SDimitry Andric   }
3570b57cec5SDimitry Andric }
358