1//===-- WebAssemblyMCTargetDesc.cpp - WebAssembly Target Descriptions -----===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file provides WebAssembly-specific target descriptions.
11///
12//===----------------------------------------------------------------------===//
13
14#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
15#include "MCTargetDesc/WebAssemblyInstPrinter.h"
16#include "MCTargetDesc/WebAssemblyMCAsmInfo.h"
17#include "MCTargetDesc/WebAssemblyTargetStreamer.h"
18#include "TargetInfo/WebAssemblyTargetInfo.h"
19#include "llvm/MC/MCInstrInfo.h"
20#include "llvm/MC/MCRegisterInfo.h"
21#include "llvm/MC/MCSubtargetInfo.h"
22#include "llvm/MC/TargetRegistry.h"
23#include "llvm/Support/Compiler.h"
24#include "llvm/Support/ErrorHandling.h"
25using namespace llvm;
26
27#define DEBUG_TYPE "wasm-mc-target-desc"
28
29#define GET_INSTRINFO_MC_DESC
30#define ENABLE_INSTR_PREDICATE_VERIFIER
31#include "WebAssemblyGenInstrInfo.inc"
32
33#define GET_SUBTARGETINFO_MC_DESC
34#include "WebAssemblyGenSubtargetInfo.inc"
35
36#define GET_REGINFO_MC_DESC
37#include "WebAssemblyGenRegisterInfo.inc"
38
39// Exception handling & setjmp-longjmp handling related options.
40
41// Emscripten's asm.js-style exception handling
42cl::opt<bool> WebAssembly::WasmEnableEmEH(
43 "enable-emscripten-cxx-exceptions",
44 cl::desc("WebAssembly Emscripten-style exception handling"),
45 cl::init(Val: false));
46// Emscripten's asm.js-style setjmp/longjmp handling
47cl::opt<bool> WebAssembly::WasmEnableEmSjLj(
48 "enable-emscripten-sjlj",
49 cl::desc("WebAssembly Emscripten-style setjmp/longjmp handling"),
50 cl::init(Val: false));
51// Exception handling using wasm EH instructions
52cl::opt<bool>
53 WebAssembly::WasmEnableEH("wasm-enable-eh",
54 cl::desc("WebAssembly exception handling"));
55// setjmp/longjmp handling using wasm EH instrutions
56cl::opt<bool> WebAssembly::WasmEnableSjLj(
57 "wasm-enable-sjlj", cl::desc("WebAssembly setjmp/longjmp handling"));
58// If true, use the legacy Wasm EH proposal:
59// https://github.com/WebAssembly/exception-handling/blob/main/proposals/exception-handling/legacy/Exceptions.md
60// And if false, use the standardized Wasm EH proposal:
61// https://github.com/WebAssembly/exception-handling/blob/main/proposals/exception-handling/Exceptions.md
62// Currently set to true by default because not all major web browsers turn on
63// the new standard proposal by default, but will later change to false.
64cl::opt<bool> WebAssembly::WasmUseLegacyEH(
65 "wasm-use-legacy-eh", cl::desc("WebAssembly exception handling (legacy)"),
66 cl::init(Val: true));
67
68static MCAsmInfo *createMCAsmInfo(const MCRegisterInfo & /*MRI*/,
69 const Triple &TT,
70 const MCTargetOptions &Options) {
71 return new WebAssemblyMCAsmInfo(TT, Options);
72}
73
74static MCInstrInfo *createMCInstrInfo() {
75 auto *X = new MCInstrInfo();
76 InitWebAssemblyMCInstrInfo(II: X);
77 return X;
78}
79
80static MCRegisterInfo *createMCRegisterInfo(const Triple & /*T*/) {
81 auto *X = new MCRegisterInfo();
82 InitWebAssemblyMCRegisterInfo(RI: X, RA: 0);
83 return X;
84}
85
86static MCInstPrinter *createMCInstPrinter(const Triple & /*T*/,
87 unsigned SyntaxVariant,
88 const MCAsmInfo &MAI,
89 const MCInstrInfo &MII,
90 const MCRegisterInfo &MRI) {
91 assert(SyntaxVariant == 0 && "WebAssembly only has one syntax variant");
92 return new WebAssemblyInstPrinter(MAI, MII, MRI);
93}
94
95static MCCodeEmitter *createCodeEmitter(const MCInstrInfo &MCII,
96 MCContext &Ctx) {
97 return createWebAssemblyMCCodeEmitter(MCII, Ctx);
98}
99
100static MCAsmBackend *createAsmBackend(const Target & /*T*/,
101 const MCSubtargetInfo &STI,
102 const MCRegisterInfo & /*MRI*/,
103 const MCTargetOptions & /*Options*/) {
104 return createWebAssemblyAsmBackend(TT: STI.getTargetTriple());
105}
106
107static MCSubtargetInfo *createMCSubtargetInfo(const Triple &TT, StringRef CPU,
108 StringRef FS) {
109 return createWebAssemblyMCSubtargetInfoImpl(TT, CPU, /*TuneCPU*/ CPU, FS);
110}
111
112static MCTargetStreamer *
113createObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) {
114 return new WebAssemblyTargetWasmStreamer(S);
115}
116
117static MCTargetStreamer *
118createAsmTargetStreamer(MCStreamer &S, formatted_raw_ostream &OS,
119 MCInstPrinter * /*InstPrint*/) {
120 return new WebAssemblyTargetAsmStreamer(S, OS);
121}
122
123static MCTargetStreamer *createNullTargetStreamer(MCStreamer &S) {
124 return new WebAssemblyTargetNullStreamer(S);
125}
126
127// Force static initialization.
128extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void
129LLVMInitializeWebAssemblyTargetMC() {
130 for (Target *T :
131 {&getTheWebAssemblyTarget32(), &getTheWebAssemblyTarget64()}) {
132 // Register the MC asm info.
133 RegisterMCAsmInfoFn X(*T, createMCAsmInfo);
134
135 // Register the MC instruction info.
136 TargetRegistry::RegisterMCInstrInfo(T&: *T, Fn: createMCInstrInfo);
137
138 // Register the MC register info.
139 TargetRegistry::RegisterMCRegInfo(T&: *T, Fn: createMCRegisterInfo);
140
141 // Register the MCInstPrinter.
142 TargetRegistry::RegisterMCInstPrinter(T&: *T, Fn: createMCInstPrinter);
143
144 // Register the MC code emitter.
145 TargetRegistry::RegisterMCCodeEmitter(T&: *T, Fn: createCodeEmitter);
146
147 // Register the ASM Backend.
148 TargetRegistry::RegisterMCAsmBackend(T&: *T, Fn: createAsmBackend);
149
150 // Register the MC subtarget info.
151 TargetRegistry::RegisterMCSubtargetInfo(T&: *T, Fn: createMCSubtargetInfo);
152
153 // Register the object target streamer.
154 TargetRegistry::RegisterObjectTargetStreamer(T&: *T,
155 Fn: createObjectTargetStreamer);
156 // Register the asm target streamer.
157 TargetRegistry::RegisterAsmTargetStreamer(T&: *T, Fn: createAsmTargetStreamer);
158 // Register the null target streamer.
159 TargetRegistry::RegisterNullTargetStreamer(T&: *T, Fn: createNullTargetStreamer);
160 }
161}
162