blob: 24dd5238f71334146c357004d6aa54e6c093ae06 [file] [log] [blame]
Dan Gohman10e730a2015-06-29 23:51:55 +00001//===-- 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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000011/// This file provides WebAssembly-specific target descriptions.
Dan Gohman10e730a2015-06-29 23:51:55 +000012///
13//===----------------------------------------------------------------------===//
14
15#include "WebAssemblyMCTargetDesc.h"
16#include "InstPrinter/WebAssemblyInstPrinter.h"
17#include "WebAssemblyMCAsmInfo.h"
Dan Gohman3469ee12016-01-12 20:30:51 +000018#include "WebAssemblyTargetStreamer.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000019#include "llvm/MC/MCInstrInfo.h"
20#include "llvm/MC/MCRegisterInfo.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000021#include "llvm/MC/MCSubtargetInfo.h"
22#include "llvm/Support/ErrorHandling.h"
23#include "llvm/Support/TargetRegistry.h"
24using namespace llvm;
25
26#define DEBUG_TYPE "wasm-mc-target-desc"
27
JF Bastienb9073fb2015-07-22 21:28:15 +000028#define GET_INSTRINFO_MC_DESC
29#include "WebAssemblyGenInstrInfo.inc"
30
Dan Gohman10e730a2015-06-29 23:51:55 +000031#define GET_SUBTARGETINFO_MC_DESC
32#include "WebAssemblyGenSubtargetInfo.inc"
33
JF Bastien5ca0bac2015-07-10 18:23:10 +000034#define GET_REGINFO_MC_DESC
35#include "WebAssemblyGenRegisterInfo.inc"
36
Dan Gohmancceedf72016-01-08 00:43:54 +000037static MCAsmInfo *createMCAsmInfo(const MCRegisterInfo & /*MRI*/,
38 const Triple &TT) {
JF Bastienae7eebd2015-07-28 17:23:07 +000039 return new WebAssemblyMCAsmInfo(TT);
Dan Gohman10e730a2015-06-29 23:51:55 +000040}
41
Dan Gohmancceedf72016-01-08 00:43:54 +000042static MCInstrInfo *createMCInstrInfo() {
Dan Gohmane9361d52015-11-05 19:28:16 +000043 MCInstrInfo *X = new MCInstrInfo();
44 InitWebAssemblyMCInstrInfo(X);
45 return X;
46}
47
Dan Gohman0656f5f2016-01-12 21:27:55 +000048static MCRegisterInfo *createMCRegisterInfo(const Triple & /*T*/) {
49 MCRegisterInfo *X = new MCRegisterInfo();
50 InitWebAssemblyMCRegisterInfo(X, 0);
51 return X;
52}
53
Dan Gohmancceedf72016-01-08 00:43:54 +000054static MCInstPrinter *createMCInstPrinter(const Triple & /*T*/,
55 unsigned SyntaxVariant,
56 const MCAsmInfo &MAI,
57 const MCInstrInfo &MII,
58 const MCRegisterInfo &MRI) {
Dan Gohman83947562016-01-20 05:54:22 +000059 assert(SyntaxVariant == 0 && "WebAssembly only has one syntax variant");
JF Bastienae7eebd2015-07-28 17:23:07 +000060 return new WebAssemblyInstPrinter(MAI, MII, MRI);
Dan Gohman10e730a2015-06-29 23:51:55 +000061}
62
Dan Gohmancceedf72016-01-08 00:43:54 +000063static MCCodeEmitter *createCodeEmitter(const MCInstrInfo &MCII,
64 const MCRegisterInfo & /*MRI*/,
Dan Gohmandf4f4d42017-02-10 00:14:42 +000065 MCContext &Ctx) {
Sam Clegg9d24fb72017-06-16 23:59:10 +000066 return createWebAssemblyMCCodeEmitter(MCII);
Dan Gohmancceedf72016-01-08 00:43:54 +000067}
68
69static MCAsmBackend *createAsmBackend(const Target & /*T*/,
Alex Bradbury7c093bf2018-01-03 09:30:39 +000070 const MCSubtargetInfo &STI,
Dan Gohmancceedf72016-01-08 00:43:54 +000071 const MCRegisterInfo & /*MRI*/,
David Blaikiebef810f2016-07-25 21:41:42 +000072 const MCTargetOptions & /*Options*/) {
Alex Bradbury7c093bf2018-01-03 09:30:39 +000073 return createWebAssemblyAsmBackend(STI.getTargetTriple());
Dan Gohmancceedf72016-01-08 00:43:54 +000074}
75
Dan Gohmanafd7e3a2016-01-12 03:30:06 +000076static MCSubtargetInfo *createMCSubtargetInfo(const Triple &TT, StringRef CPU,
77 StringRef FS) {
78 return createWebAssemblyMCSubtargetInfoImpl(TT, CPU, FS);
79}
80
Dan Gohman3469ee12016-01-12 20:30:51 +000081static MCTargetStreamer *
Dan Gohman18eafb62017-02-22 01:23:18 +000082createObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) {
Dan Gohman18eafb62017-02-22 01:23:18 +000083 return new WebAssemblyTargetWasmStreamer(S);
Dan Gohman3469ee12016-01-12 20:30:51 +000084}
85
86static MCTargetStreamer *createAsmTargetStreamer(MCStreamer &S,
87 formatted_raw_ostream &OS,
88 MCInstPrinter * /*InstPrint*/,
89 bool /*isVerboseAsm*/) {
90 return new WebAssemblyTargetAsmStreamer(S, OS);
91}
92
Dan Gohman10e730a2015-06-29 23:51:55 +000093// Force static initialization.
94extern "C" void LLVMInitializeWebAssemblyTargetMC() {
Mehdi Aminif42454b2016-10-09 23:00:34 +000095 for (Target *T :
96 {&getTheWebAssemblyTarget32(), &getTheWebAssemblyTarget64()}) {
Dan Gohman10e730a2015-06-29 23:51:55 +000097 // Register the MC asm info.
Dan Gohmancceedf72016-01-08 00:43:54 +000098 RegisterMCAsmInfoFn X(*T, createMCAsmInfo);
Dan Gohman10e730a2015-06-29 23:51:55 +000099
Dan Gohmane9361d52015-11-05 19:28:16 +0000100 // Register the MC instruction info.
Dan Gohmancceedf72016-01-08 00:43:54 +0000101 TargetRegistry::RegisterMCInstrInfo(*T, createMCInstrInfo);
Dan Gohmane9361d52015-11-05 19:28:16 +0000102
Dan Gohman0656f5f2016-01-12 21:27:55 +0000103 // Register the MC register info.
104 TargetRegistry::RegisterMCRegInfo(*T, createMCRegisterInfo);
105
Dan Gohman10e730a2015-06-29 23:51:55 +0000106 // Register the MCInstPrinter.
Dan Gohmancceedf72016-01-08 00:43:54 +0000107 TargetRegistry::RegisterMCInstPrinter(*T, createMCInstPrinter);
Dan Gohman05ac43f2015-12-17 01:39:00 +0000108
Dan Gohman4ef99432016-01-08 01:18:00 +0000109 // Register the MC code emitter.
Dan Gohmancceedf72016-01-08 00:43:54 +0000110 TargetRegistry::RegisterMCCodeEmitter(*T, createCodeEmitter);
Dan Gohman05ac43f2015-12-17 01:39:00 +0000111
Dan Gohman4ef99432016-01-08 01:18:00 +0000112 // Register the ASM Backend.
Dan Gohmancceedf72016-01-08 00:43:54 +0000113 TargetRegistry::RegisterMCAsmBackend(*T, createAsmBackend);
Dan Gohmanafd7e3a2016-01-12 03:30:06 +0000114
115 // Register the MC subtarget info.
116 TargetRegistry::RegisterMCSubtargetInfo(*T, createMCSubtargetInfo);
Dan Gohman3469ee12016-01-12 20:30:51 +0000117
118 // Register the object target streamer.
119 TargetRegistry::RegisterObjectTargetStreamer(*T,
120 createObjectTargetStreamer);
121 // Register the asm target streamer.
122 TargetRegistry::RegisterAsmTargetStreamer(*T, createAsmTargetStreamer);
Dan Gohman10e730a2015-06-29 23:51:55 +0000123 }
124}
Dan Gohman3acb1872016-10-24 23:27:49 +0000125
Derek Schuffe2688c42017-03-14 20:23:22 +0000126wasm::ValType WebAssembly::toValType(const MVT &Ty) {
Dan Gohman3acb1872016-10-24 23:27:49 +0000127 switch (Ty.SimpleTy) {
Heejin Ahnf208f632018-09-05 01:27:38 +0000128 case MVT::i32:
129 return wasm::ValType::I32;
130 case MVT::i64:
131 return wasm::ValType::I64;
132 case MVT::f32:
133 return wasm::ValType::F32;
134 case MVT::f64:
135 return wasm::ValType::F64;
Thomas Lively6f21a132018-09-20 22:04:44 +0000136 case MVT::v16i8:
137 case MVT::v8i16:
138 case MVT::v4i32:
139 case MVT::v2i64:
140 case MVT::v4f32:
141 case MVT::v2f64:
142 return wasm::ValType::V128;
Heejin Ahnf208f632018-09-05 01:27:38 +0000143 case MVT::ExceptRef:
144 return wasm::ValType::EXCEPT_REF;
145 default:
146 llvm_unreachable("unexpected type");
Dan Gohman3acb1872016-10-24 23:27:49 +0000147 }
148}