1 //===-- WebAssemblyMCTargetDesc.cpp - WebAssembly Target Descriptions -----===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// This file provides WebAssembly-specific target descriptions.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #include "WebAssemblyMCTargetDesc.h"
16 #include "InstPrinter/WebAssemblyInstPrinter.h"
17 #include "WebAssemblyMCAsmInfo.h"
18 #include "WebAssemblyTargetStreamer.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 
createMCAsmInfo(const MCRegisterInfo &,const Triple & TT)37 static MCAsmInfo *createMCAsmInfo(const MCRegisterInfo & /*MRI*/,
38                                   const Triple &TT) {
39   return new WebAssemblyMCAsmInfo(TT);
40 }
41 
createMCInstrInfo()42 static MCInstrInfo *createMCInstrInfo() {
43   MCInstrInfo *X = new MCInstrInfo();
44   InitWebAssemblyMCInstrInfo(X);
45   return X;
46 }
47 
createMCRegisterInfo(const Triple &)48 static MCRegisterInfo *createMCRegisterInfo(const Triple & /*T*/) {
49   MCRegisterInfo *X = new MCRegisterInfo();
50   InitWebAssemblyMCRegisterInfo(X, 0);
51   return X;
52 }
53 
createMCInstPrinter(const Triple &,unsigned SyntaxVariant,const MCAsmInfo & MAI,const MCInstrInfo & MII,const MCRegisterInfo & MRI)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 
createCodeEmitter(const MCInstrInfo & MCII,const MCRegisterInfo &,MCContext & Ctx)63 static MCCodeEmitter *createCodeEmitter(const MCInstrInfo &MCII,
64                                         const MCRegisterInfo & /*MRI*/,
65                                         MCContext &Ctx) {
66   return createWebAssemblyMCCodeEmitter(MCII);
67 }
68 
createAsmBackend(const Target &,const MCSubtargetInfo & STI,const MCRegisterInfo &,const MCTargetOptions &)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 
createMCSubtargetInfo(const Triple & TT,StringRef CPU,StringRef FS)76 static MCSubtargetInfo *createMCSubtargetInfo(const Triple &TT, StringRef CPU,
77                                               StringRef FS) {
78   return createWebAssemblyMCSubtargetInfoImpl(TT, CPU, FS);
79 }
80 
81 static MCTargetStreamer *
createObjectTargetStreamer(MCStreamer & S,const MCSubtargetInfo & STI)82 createObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) {
83   return new WebAssemblyTargetWasmStreamer(S);
84 }
85 
createAsmTargetStreamer(MCStreamer & S,formatted_raw_ostream & OS,MCInstPrinter *,bool)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 // Force static initialization.
LLVMInitializeWebAssemblyTargetMC()94 extern "C" void LLVMInitializeWebAssemblyTargetMC() {
95   for (Target *T :
96        {&getTheWebAssemblyTarget32(), &getTheWebAssemblyTarget64()}) {
97     // Register the MC asm info.
98     RegisterMCAsmInfoFn X(*T, createMCAsmInfo);
99 
100     // Register the MC instruction info.
101     TargetRegistry::RegisterMCInstrInfo(*T, createMCInstrInfo);
102 
103     // Register the MC register info.
104     TargetRegistry::RegisterMCRegInfo(*T, createMCRegisterInfo);
105 
106     // Register the MCInstPrinter.
107     TargetRegistry::RegisterMCInstPrinter(*T, createMCInstPrinter);
108 
109     // Register the MC code emitter.
110     TargetRegistry::RegisterMCCodeEmitter(*T, createCodeEmitter);
111 
112     // Register the ASM Backend.
113     TargetRegistry::RegisterMCAsmBackend(*T, createAsmBackend);
114 
115     // Register the MC subtarget info.
116     TargetRegistry::RegisterMCSubtargetInfo(*T, createMCSubtargetInfo);
117 
118     // Register the object target streamer.
119     TargetRegistry::RegisterObjectTargetStreamer(*T,
120                                                  createObjectTargetStreamer);
121     // Register the asm target streamer.
122     TargetRegistry::RegisterAsmTargetStreamer(*T, createAsmTargetStreamer);
123   }
124 }
125 
toValType(const MVT & Ty)126 wasm::ValType WebAssembly::toValType(const MVT &Ty) {
127   switch (Ty.SimpleTy) {
128   case MVT::i32: return wasm::ValType::I32;
129   case MVT::i64: return wasm::ValType::I64;
130   case MVT::f32: return wasm::ValType::F32;
131   case MVT::f64: return wasm::ValType::F64;
132   case MVT::ExceptRef: return wasm::ValType::EXCEPT_REF;
133   default: llvm_unreachable("unexpected type");
134   }
135 }
136