• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /// \brief 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 
adjustCodeGenOpts(const Triple &,Reloc::Model,CodeModel::Model & CM)42 static void adjustCodeGenOpts(const Triple & /*TT*/, Reloc::Model /*RM*/,
43                               CodeModel::Model &CM) {
44   CodeModel::Model M = (CM == CodeModel::Default || CM == CodeModel::JITDefault)
45                            ? CodeModel::Large
46                            : CM;
47   if (M != CodeModel::Large)
48     report_fatal_error("Non-large code models are not supported yet");
49 }
50 
createMCInstrInfo()51 static MCInstrInfo *createMCInstrInfo() {
52   MCInstrInfo *X = new MCInstrInfo();
53   InitWebAssemblyMCInstrInfo(X);
54   return X;
55 }
56 
createMCRegisterInfo(const Triple &)57 static MCRegisterInfo *createMCRegisterInfo(const Triple & /*T*/) {
58   MCRegisterInfo *X = new MCRegisterInfo();
59   InitWebAssemblyMCRegisterInfo(X, 0);
60   return X;
61 }
62 
createMCInstPrinter(const Triple &,unsigned SyntaxVariant,const MCAsmInfo & MAI,const MCInstrInfo & MII,const MCRegisterInfo & MRI)63 static MCInstPrinter *createMCInstPrinter(const Triple & /*T*/,
64                                           unsigned SyntaxVariant,
65                                           const MCAsmInfo &MAI,
66                                           const MCInstrInfo &MII,
67                                           const MCRegisterInfo &MRI) {
68   assert(SyntaxVariant == 0 && "WebAssembly only has one syntax variant");
69   return new WebAssemblyInstPrinter(MAI, MII, MRI);
70 }
71 
createCodeEmitter(const MCInstrInfo & MCII,const MCRegisterInfo &,MCContext &)72 static MCCodeEmitter *createCodeEmitter(const MCInstrInfo &MCII,
73                                         const MCRegisterInfo & /*MRI*/,
74                                         MCContext & /*Ctx*/) {
75   return createWebAssemblyMCCodeEmitter(MCII);
76 }
77 
createAsmBackend(const Target &,const MCRegisterInfo &,const Triple & TT,StringRef)78 static MCAsmBackend *createAsmBackend(const Target & /*T*/,
79                                       const MCRegisterInfo & /*MRI*/,
80                                       const Triple &TT, StringRef /*CPU*/) {
81   return createWebAssemblyAsmBackend(TT);
82 }
83 
createMCSubtargetInfo(const Triple & TT,StringRef CPU,StringRef FS)84 static MCSubtargetInfo *createMCSubtargetInfo(const Triple &TT, StringRef CPU,
85                                               StringRef FS) {
86   return createWebAssemblyMCSubtargetInfoImpl(TT, CPU, FS);
87 }
88 
89 static MCTargetStreamer *
createObjectTargetStreamer(MCStreamer & S,const MCSubtargetInfo &)90 createObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo & /*STI*/) {
91   return new WebAssemblyTargetELFStreamer(S);
92 }
93 
createAsmTargetStreamer(MCStreamer & S,formatted_raw_ostream & OS,MCInstPrinter *,bool)94 static MCTargetStreamer *createAsmTargetStreamer(MCStreamer &S,
95                                                  formatted_raw_ostream &OS,
96                                                  MCInstPrinter * /*InstPrint*/,
97                                                  bool /*isVerboseAsm*/) {
98   return new WebAssemblyTargetAsmStreamer(S, OS);
99 }
100 
101 // Force static initialization.
LLVMInitializeWebAssemblyTargetMC()102 extern "C" void LLVMInitializeWebAssemblyTargetMC() {
103   for (Target *T : {&TheWebAssemblyTarget32, &TheWebAssemblyTarget64}) {
104     // Register the MC asm info.
105     RegisterMCAsmInfoFn X(*T, createMCAsmInfo);
106 
107     // Register the MC instruction info.
108     TargetRegistry::RegisterMCInstrInfo(*T, createMCInstrInfo);
109 
110     // Register the MC codegen info.
111     TargetRegistry::registerMCAdjustCodeGenOpts(*T, adjustCodeGenOpts);
112 
113     // Register the MC register info.
114     TargetRegistry::RegisterMCRegInfo(*T, createMCRegisterInfo);
115 
116     // Register the MCInstPrinter.
117     TargetRegistry::RegisterMCInstPrinter(*T, createMCInstPrinter);
118 
119     // Register the MC code emitter.
120     TargetRegistry::RegisterMCCodeEmitter(*T, createCodeEmitter);
121 
122     // Register the ASM Backend.
123     TargetRegistry::RegisterMCAsmBackend(*T, createAsmBackend);
124 
125     // Register the MC subtarget info.
126     TargetRegistry::RegisterMCSubtargetInfo(*T, createMCSubtargetInfo);
127 
128     // Register the object target streamer.
129     TargetRegistry::RegisterObjectTargetStreamer(*T,
130                                                  createObjectTargetStreamer);
131     // Register the asm target streamer.
132     TargetRegistry::RegisterAsmTargetStreamer(*T, createAsmTargetStreamer);
133   }
134 }
135