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