10b57cec5SDimitry Andric //===-- WebAssemblyMCTargetDesc.cpp - WebAssembly Target Descriptions -----===//
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 provides WebAssembly-specific target descriptions.
110b57cec5SDimitry Andric ///
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric
140b57cec5SDimitry Andric #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
150b57cec5SDimitry Andric #include "MCTargetDesc/WebAssemblyInstPrinter.h"
160b57cec5SDimitry Andric #include "MCTargetDesc/WebAssemblyMCAsmInfo.h"
170b57cec5SDimitry Andric #include "MCTargetDesc/WebAssemblyTargetStreamer.h"
180b57cec5SDimitry Andric #include "TargetInfo/WebAssemblyTargetInfo.h"
190b57cec5SDimitry Andric #include "llvm/MC/MCInstrInfo.h"
200b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
210b57cec5SDimitry Andric #include "llvm/MC/MCSubtargetInfo.h"
22349cc55cSDimitry Andric #include "llvm/MC/TargetRegistry.h"
230b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
240b57cec5SDimitry Andric using namespace llvm;
250b57cec5SDimitry Andric
260b57cec5SDimitry Andric #define DEBUG_TYPE "wasm-mc-target-desc"
270b57cec5SDimitry Andric
280b57cec5SDimitry Andric #define GET_INSTRINFO_MC_DESC
29753f127fSDimitry Andric #define ENABLE_INSTR_PREDICATE_VERIFIER
300b57cec5SDimitry Andric #include "WebAssemblyGenInstrInfo.inc"
310b57cec5SDimitry Andric
320b57cec5SDimitry Andric #define GET_SUBTARGETINFO_MC_DESC
330b57cec5SDimitry Andric #include "WebAssemblyGenSubtargetInfo.inc"
340b57cec5SDimitry Andric
350b57cec5SDimitry Andric #define GET_REGINFO_MC_DESC
360b57cec5SDimitry Andric #include "WebAssemblyGenRegisterInfo.inc"
370b57cec5SDimitry Andric
3806c3fb27SDimitry Andric // Exception handling & setjmp-longjmp handling related options.
3906c3fb27SDimitry Andric
4006c3fb27SDimitry Andric // Emscripten's asm.js-style exception handling
4106c3fb27SDimitry Andric cl::opt<bool> WebAssembly::WasmEnableEmEH(
4206c3fb27SDimitry Andric "enable-emscripten-cxx-exceptions",
4306c3fb27SDimitry Andric cl::desc("WebAssembly Emscripten-style exception handling"),
4406c3fb27SDimitry Andric cl::init(false));
4506c3fb27SDimitry Andric // Emscripten's asm.js-style setjmp/longjmp handling
4606c3fb27SDimitry Andric cl::opt<bool> WebAssembly::WasmEnableEmSjLj(
4706c3fb27SDimitry Andric "enable-emscripten-sjlj",
4806c3fb27SDimitry Andric cl::desc("WebAssembly Emscripten-style setjmp/longjmp handling"),
4906c3fb27SDimitry Andric cl::init(false));
5006c3fb27SDimitry Andric // Exception handling using wasm EH instructions
5106c3fb27SDimitry Andric cl::opt<bool>
5206c3fb27SDimitry Andric WebAssembly::WasmEnableEH("wasm-enable-eh",
5306c3fb27SDimitry Andric cl::desc("WebAssembly exception handling"));
5406c3fb27SDimitry Andric // setjmp/longjmp handling using wasm EH instrutions
5506c3fb27SDimitry Andric cl::opt<bool> WebAssembly::WasmEnableSjLj(
5606c3fb27SDimitry Andric "wasm-enable-sjlj", cl::desc("WebAssembly setjmp/longjmp handling"));
57*0fca6ea1SDimitry Andric // Whether we use the new exnref Wasm EH proposal adopted on Oct 2023.
58*0fca6ea1SDimitry Andric // Should be used with -wasm-enable-eh.
59*0fca6ea1SDimitry Andric // Currently set to false by default, but will later change to true and then
60*0fca6ea1SDimitry Andric // later can be removed after the legacy WAsm EH instructions are removed.
61*0fca6ea1SDimitry Andric cl::opt<bool> WebAssembly::WasmEnableExnref(
62*0fca6ea1SDimitry Andric "wasm-enable-exnref", cl::desc("WebAssembly exception handling (exnref)"),
63*0fca6ea1SDimitry Andric cl::init(false));
6406c3fb27SDimitry Andric
createMCAsmInfo(const MCRegisterInfo &,const Triple & TT,const MCTargetOptions & Options)650b57cec5SDimitry Andric static MCAsmInfo *createMCAsmInfo(const MCRegisterInfo & /*MRI*/,
66480093f4SDimitry Andric const Triple &TT,
67480093f4SDimitry Andric const MCTargetOptions &Options) {
68480093f4SDimitry Andric return new WebAssemblyMCAsmInfo(TT, Options);
690b57cec5SDimitry Andric }
700b57cec5SDimitry Andric
createMCInstrInfo()710b57cec5SDimitry Andric static MCInstrInfo *createMCInstrInfo() {
720b57cec5SDimitry Andric auto *X = new MCInstrInfo();
730b57cec5SDimitry Andric InitWebAssemblyMCInstrInfo(X);
740b57cec5SDimitry Andric return X;
750b57cec5SDimitry Andric }
760b57cec5SDimitry Andric
createMCRegisterInfo(const Triple &)770b57cec5SDimitry Andric static MCRegisterInfo *createMCRegisterInfo(const Triple & /*T*/) {
780b57cec5SDimitry Andric auto *X = new MCRegisterInfo();
790b57cec5SDimitry Andric InitWebAssemblyMCRegisterInfo(X, 0);
800b57cec5SDimitry Andric return X;
810b57cec5SDimitry Andric }
820b57cec5SDimitry Andric
createMCInstPrinter(const Triple &,unsigned SyntaxVariant,const MCAsmInfo & MAI,const MCInstrInfo & MII,const MCRegisterInfo & MRI)830b57cec5SDimitry Andric static MCInstPrinter *createMCInstPrinter(const Triple & /*T*/,
840b57cec5SDimitry Andric unsigned SyntaxVariant,
850b57cec5SDimitry Andric const MCAsmInfo &MAI,
860b57cec5SDimitry Andric const MCInstrInfo &MII,
870b57cec5SDimitry Andric const MCRegisterInfo &MRI) {
880b57cec5SDimitry Andric assert(SyntaxVariant == 0 && "WebAssembly only has one syntax variant");
890b57cec5SDimitry Andric return new WebAssemblyInstPrinter(MAI, MII, MRI);
900b57cec5SDimitry Andric }
910b57cec5SDimitry Andric
createCodeEmitter(const MCInstrInfo & MCII,MCContext & Ctx)920b57cec5SDimitry Andric static MCCodeEmitter *createCodeEmitter(const MCInstrInfo &MCII,
930b57cec5SDimitry Andric MCContext &Ctx) {
9406c3fb27SDimitry Andric return createWebAssemblyMCCodeEmitter(MCII, Ctx);
950b57cec5SDimitry Andric }
960b57cec5SDimitry Andric
createAsmBackend(const Target &,const MCSubtargetInfo & STI,const MCRegisterInfo &,const MCTargetOptions &)970b57cec5SDimitry Andric static MCAsmBackend *createAsmBackend(const Target & /*T*/,
980b57cec5SDimitry Andric const MCSubtargetInfo &STI,
990b57cec5SDimitry Andric const MCRegisterInfo & /*MRI*/,
1000b57cec5SDimitry Andric const MCTargetOptions & /*Options*/) {
1010b57cec5SDimitry Andric return createWebAssemblyAsmBackend(STI.getTargetTriple());
1020b57cec5SDimitry Andric }
1030b57cec5SDimitry Andric
createMCSubtargetInfo(const Triple & TT,StringRef CPU,StringRef FS)1040b57cec5SDimitry Andric static MCSubtargetInfo *createMCSubtargetInfo(const Triple &TT, StringRef CPU,
1050b57cec5SDimitry Andric StringRef FS) {
106e8d8bef9SDimitry Andric return createWebAssemblyMCSubtargetInfoImpl(TT, CPU, /*TuneCPU*/ CPU, FS);
1070b57cec5SDimitry Andric }
1080b57cec5SDimitry Andric
1090b57cec5SDimitry Andric static MCTargetStreamer *
createObjectTargetStreamer(MCStreamer & S,const MCSubtargetInfo & STI)1100b57cec5SDimitry Andric createObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) {
1110b57cec5SDimitry Andric return new WebAssemblyTargetWasmStreamer(S);
1120b57cec5SDimitry Andric }
1130b57cec5SDimitry Andric
114*0fca6ea1SDimitry Andric static MCTargetStreamer *
createAsmTargetStreamer(MCStreamer & S,formatted_raw_ostream & OS,MCInstPrinter *)115*0fca6ea1SDimitry Andric createAsmTargetStreamer(MCStreamer &S, formatted_raw_ostream &OS,
116*0fca6ea1SDimitry Andric MCInstPrinter * /*InstPrint*/) {
1170b57cec5SDimitry Andric return new WebAssemblyTargetAsmStreamer(S, OS);
1180b57cec5SDimitry Andric }
1190b57cec5SDimitry Andric
createNullTargetStreamer(MCStreamer & S)1200b57cec5SDimitry Andric static MCTargetStreamer *createNullTargetStreamer(MCStreamer &S) {
1210b57cec5SDimitry Andric return new WebAssemblyTargetNullStreamer(S);
1220b57cec5SDimitry Andric }
1230b57cec5SDimitry Andric
1240b57cec5SDimitry Andric // Force static initialization.
LLVMInitializeWebAssemblyTargetMC()125480093f4SDimitry Andric extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeWebAssemblyTargetMC() {
1260b57cec5SDimitry Andric for (Target *T :
1270b57cec5SDimitry Andric {&getTheWebAssemblyTarget32(), &getTheWebAssemblyTarget64()}) {
1280b57cec5SDimitry Andric // Register the MC asm info.
1290b57cec5SDimitry Andric RegisterMCAsmInfoFn X(*T, createMCAsmInfo);
1300b57cec5SDimitry Andric
1310b57cec5SDimitry Andric // Register the MC instruction info.
1320b57cec5SDimitry Andric TargetRegistry::RegisterMCInstrInfo(*T, createMCInstrInfo);
1330b57cec5SDimitry Andric
1340b57cec5SDimitry Andric // Register the MC register info.
1350b57cec5SDimitry Andric TargetRegistry::RegisterMCRegInfo(*T, createMCRegisterInfo);
1360b57cec5SDimitry Andric
1370b57cec5SDimitry Andric // Register the MCInstPrinter.
1380b57cec5SDimitry Andric TargetRegistry::RegisterMCInstPrinter(*T, createMCInstPrinter);
1390b57cec5SDimitry Andric
1400b57cec5SDimitry Andric // Register the MC code emitter.
1410b57cec5SDimitry Andric TargetRegistry::RegisterMCCodeEmitter(*T, createCodeEmitter);
1420b57cec5SDimitry Andric
1430b57cec5SDimitry Andric // Register the ASM Backend.
1440b57cec5SDimitry Andric TargetRegistry::RegisterMCAsmBackend(*T, createAsmBackend);
1450b57cec5SDimitry Andric
1460b57cec5SDimitry Andric // Register the MC subtarget info.
1470b57cec5SDimitry Andric TargetRegistry::RegisterMCSubtargetInfo(*T, createMCSubtargetInfo);
1480b57cec5SDimitry Andric
1490b57cec5SDimitry Andric // Register the object target streamer.
1500b57cec5SDimitry Andric TargetRegistry::RegisterObjectTargetStreamer(*T,
1510b57cec5SDimitry Andric createObjectTargetStreamer);
1520b57cec5SDimitry Andric // Register the asm target streamer.
1530b57cec5SDimitry Andric TargetRegistry::RegisterAsmTargetStreamer(*T, createAsmTargetStreamer);
1540b57cec5SDimitry Andric // Register the null target streamer.
1550b57cec5SDimitry Andric TargetRegistry::RegisterNullTargetStreamer(*T, createNullTargetStreamer);
1560b57cec5SDimitry Andric }
1570b57cec5SDimitry Andric }
158