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