xref: /freebsd/contrib/llvm-project/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.cpp (revision 1719886f6d08408b834d270c59ffcfd821c8f63a)
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/ErrorHandling.h"
24 using namespace llvm;
25 
26 #define DEBUG_TYPE "wasm-mc-target-desc"
27 
28 #define GET_INSTRINFO_MC_DESC
29 #define ENABLE_INSTR_PREDICATE_VERIFIER
30 #include "WebAssemblyGenInstrInfo.inc"
31 
32 #define GET_SUBTARGETINFO_MC_DESC
33 #include "WebAssemblyGenSubtargetInfo.inc"
34 
35 #define GET_REGINFO_MC_DESC
36 #include "WebAssemblyGenRegisterInfo.inc"
37 
38 // Exception handling & setjmp-longjmp handling related options.
39 
40 // Emscripten's asm.js-style exception handling
41 cl::opt<bool> WebAssembly::WasmEnableEmEH(
42     "enable-emscripten-cxx-exceptions",
43     cl::desc("WebAssembly Emscripten-style exception handling"),
44     cl::init(false));
45 // Emscripten's asm.js-style setjmp/longjmp handling
46 cl::opt<bool> WebAssembly::WasmEnableEmSjLj(
47     "enable-emscripten-sjlj",
48     cl::desc("WebAssembly Emscripten-style setjmp/longjmp handling"),
49     cl::init(false));
50 // Exception handling using wasm EH instructions
51 cl::opt<bool>
52     WebAssembly::WasmEnableEH("wasm-enable-eh",
53                               cl::desc("WebAssembly exception handling"));
54 // setjmp/longjmp handling using wasm EH instrutions
55 cl::opt<bool> WebAssembly::WasmEnableSjLj(
56     "wasm-enable-sjlj", cl::desc("WebAssembly setjmp/longjmp handling"));
57 
58 static MCAsmInfo *createMCAsmInfo(const MCRegisterInfo & /*MRI*/,
59                                   const Triple &TT,
60                                   const MCTargetOptions &Options) {
61   return new WebAssemblyMCAsmInfo(TT, Options);
62 }
63 
64 static MCInstrInfo *createMCInstrInfo() {
65   auto *X = new MCInstrInfo();
66   InitWebAssemblyMCInstrInfo(X);
67   return X;
68 }
69 
70 static MCRegisterInfo *createMCRegisterInfo(const Triple & /*T*/) {
71   auto *X = new MCRegisterInfo();
72   InitWebAssemblyMCRegisterInfo(X, 0);
73   return X;
74 }
75 
76 static MCInstPrinter *createMCInstPrinter(const Triple & /*T*/,
77                                           unsigned SyntaxVariant,
78                                           const MCAsmInfo &MAI,
79                                           const MCInstrInfo &MII,
80                                           const MCRegisterInfo &MRI) {
81   assert(SyntaxVariant == 0 && "WebAssembly only has one syntax variant");
82   return new WebAssemblyInstPrinter(MAI, MII, MRI);
83 }
84 
85 static MCCodeEmitter *createCodeEmitter(const MCInstrInfo &MCII,
86                                         MCContext &Ctx) {
87   return createWebAssemblyMCCodeEmitter(MCII, Ctx);
88 }
89 
90 static MCAsmBackend *createAsmBackend(const Target & /*T*/,
91                                       const MCSubtargetInfo &STI,
92                                       const MCRegisterInfo & /*MRI*/,
93                                       const MCTargetOptions & /*Options*/) {
94   return createWebAssemblyAsmBackend(STI.getTargetTriple());
95 }
96 
97 static MCSubtargetInfo *createMCSubtargetInfo(const Triple &TT, StringRef CPU,
98                                               StringRef FS) {
99   return createWebAssemblyMCSubtargetInfoImpl(TT, CPU, /*TuneCPU*/ CPU, FS);
100 }
101 
102 static MCTargetStreamer *
103 createObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) {
104   return new WebAssemblyTargetWasmStreamer(S);
105 }
106 
107 static MCTargetStreamer *createAsmTargetStreamer(MCStreamer &S,
108                                                  formatted_raw_ostream &OS,
109                                                  MCInstPrinter * /*InstPrint*/,
110                                                  bool /*isVerboseAsm*/) {
111   return new WebAssemblyTargetAsmStreamer(S, OS);
112 }
113 
114 static MCTargetStreamer *createNullTargetStreamer(MCStreamer &S) {
115   return new WebAssemblyTargetNullStreamer(S);
116 }
117 
118 // Force static initialization.
119 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeWebAssemblyTargetMC() {
120   for (Target *T :
121        {&getTheWebAssemblyTarget32(), &getTheWebAssemblyTarget64()}) {
122     // Register the MC asm info.
123     RegisterMCAsmInfoFn X(*T, createMCAsmInfo);
124 
125     // Register the MC instruction info.
126     TargetRegistry::RegisterMCInstrInfo(*T, createMCInstrInfo);
127 
128     // Register the MC register info.
129     TargetRegistry::RegisterMCRegInfo(*T, createMCRegisterInfo);
130 
131     // Register the MCInstPrinter.
132     TargetRegistry::RegisterMCInstPrinter(*T, createMCInstPrinter);
133 
134     // Register the MC code emitter.
135     TargetRegistry::RegisterMCCodeEmitter(*T, createCodeEmitter);
136 
137     // Register the ASM Backend.
138     TargetRegistry::RegisterMCAsmBackend(*T, createAsmBackend);
139 
140     // Register the MC subtarget info.
141     TargetRegistry::RegisterMCSubtargetInfo(*T, createMCSubtargetInfo);
142 
143     // Register the object target streamer.
144     TargetRegistry::RegisterObjectTargetStreamer(*T,
145                                                  createObjectTargetStreamer);
146     // Register the asm target streamer.
147     TargetRegistry::RegisterAsmTargetStreamer(*T, createAsmTargetStreamer);
148     // Register the null target streamer.
149     TargetRegistry::RegisterNullTargetStreamer(*T, createNullTargetStreamer);
150   }
151 }
152