blob: a641ad0ab715b907ec8748b7848afb7184787530 [file] [log] [blame]
Dan Gohman10e730a2015-06-29 23:51:55 +00001//===-- WebAssemblyMCTargetDesc.cpp - WebAssembly Target Descriptions -----===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Dan Gohman10e730a2015-06-29 23:51:55 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// This file provides WebAssembly-specific target descriptions.
Dan Gohman10e730a2015-06-29 23:51:55 +000011///
12//===----------------------------------------------------------------------===//
13
14#include "WebAssemblyMCTargetDesc.h"
15#include "InstPrinter/WebAssemblyInstPrinter.h"
16#include "WebAssemblyMCAsmInfo.h"
Dan Gohman3469ee12016-01-12 20:30:51 +000017#include "WebAssemblyTargetStreamer.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000018#include "llvm/MC/MCInstrInfo.h"
19#include "llvm/MC/MCRegisterInfo.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000020#include "llvm/MC/MCSubtargetInfo.h"
21#include "llvm/Support/ErrorHandling.h"
22#include "llvm/Support/TargetRegistry.h"
23using namespace llvm;
24
25#define DEBUG_TYPE "wasm-mc-target-desc"
26
JF Bastienb9073fb2015-07-22 21:28:15 +000027#define GET_INSTRINFO_MC_DESC
28#include "WebAssemblyGenInstrInfo.inc"
29
Dan Gohman10e730a2015-06-29 23:51:55 +000030#define GET_SUBTARGETINFO_MC_DESC
31#include "WebAssemblyGenSubtargetInfo.inc"
32
JF Bastien5ca0bac2015-07-10 18:23:10 +000033#define GET_REGINFO_MC_DESC
34#include "WebAssemblyGenRegisterInfo.inc"
35
Dan Gohmancceedf72016-01-08 00:43:54 +000036static MCAsmInfo *createMCAsmInfo(const MCRegisterInfo & /*MRI*/,
37 const Triple &TT) {
JF Bastienae7eebd2015-07-28 17:23:07 +000038 return new WebAssemblyMCAsmInfo(TT);
Dan Gohman10e730a2015-06-29 23:51:55 +000039}
40
Dan Gohmancceedf72016-01-08 00:43:54 +000041static MCInstrInfo *createMCInstrInfo() {
Dan Gohmane9361d52015-11-05 19:28:16 +000042 MCInstrInfo *X = new MCInstrInfo();
43 InitWebAssemblyMCInstrInfo(X);
44 return X;
45}
46
Dan Gohman0656f5f2016-01-12 21:27:55 +000047static MCRegisterInfo *createMCRegisterInfo(const Triple & /*T*/) {
48 MCRegisterInfo *X = new MCRegisterInfo();
49 InitWebAssemblyMCRegisterInfo(X, 0);
50 return X;
51}
52
Dan Gohmancceedf72016-01-08 00:43:54 +000053static MCInstPrinter *createMCInstPrinter(const Triple & /*T*/,
54 unsigned SyntaxVariant,
55 const MCAsmInfo &MAI,
56 const MCInstrInfo &MII,
57 const MCRegisterInfo &MRI) {
Dan Gohman83947562016-01-20 05:54:22 +000058 assert(SyntaxVariant == 0 && "WebAssembly only has one syntax variant");
JF Bastienae7eebd2015-07-28 17:23:07 +000059 return new WebAssemblyInstPrinter(MAI, MII, MRI);
Dan Gohman10e730a2015-06-29 23:51:55 +000060}
61
Dan Gohmancceedf72016-01-08 00:43:54 +000062static MCCodeEmitter *createCodeEmitter(const MCInstrInfo &MCII,
63 const MCRegisterInfo & /*MRI*/,
Dan Gohmandf4f4d42017-02-10 00:14:42 +000064 MCContext &Ctx) {
Sam Clegg9d24fb72017-06-16 23:59:10 +000065 return createWebAssemblyMCCodeEmitter(MCII);
Dan Gohmancceedf72016-01-08 00:43:54 +000066}
67
68static MCAsmBackend *createAsmBackend(const Target & /*T*/,
Alex Bradbury7c093bf2018-01-03 09:30:39 +000069 const MCSubtargetInfo &STI,
Dan Gohmancceedf72016-01-08 00:43:54 +000070 const MCRegisterInfo & /*MRI*/,
David Blaikiebef810f2016-07-25 21:41:42 +000071 const MCTargetOptions & /*Options*/) {
Alex Bradbury7c093bf2018-01-03 09:30:39 +000072 return createWebAssemblyAsmBackend(STI.getTargetTriple());
Dan Gohmancceedf72016-01-08 00:43:54 +000073}
74
Dan Gohmanafd7e3a2016-01-12 03:30:06 +000075static MCSubtargetInfo *createMCSubtargetInfo(const Triple &TT, StringRef CPU,
76 StringRef FS) {
77 return createWebAssemblyMCSubtargetInfoImpl(TT, CPU, FS);
78}
79
Dan Gohman3469ee12016-01-12 20:30:51 +000080static MCTargetStreamer *
Dan Gohman18eafb62017-02-22 01:23:18 +000081createObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) {
Dan Gohman18eafb62017-02-22 01:23:18 +000082 return new WebAssemblyTargetWasmStreamer(S);
Dan Gohman3469ee12016-01-12 20:30:51 +000083}
84
85static MCTargetStreamer *createAsmTargetStreamer(MCStreamer &S,
86 formatted_raw_ostream &OS,
87 MCInstPrinter * /*InstPrint*/,
88 bool /*isVerboseAsm*/) {
89 return new WebAssemblyTargetAsmStreamer(S, OS);
90}
91
Heejin Ahne0f8b9b2018-11-18 11:58:47 +000092static MCTargetStreamer *createNullTargetStreamer(MCStreamer &S) {
93 return new WebAssemblyTargetNullStreamer(S);
94}
95
Dan Gohman10e730a2015-06-29 23:51:55 +000096// Force static initialization.
97extern "C" void LLVMInitializeWebAssemblyTargetMC() {
Mehdi Aminif42454b2016-10-09 23:00:34 +000098 for (Target *T :
99 {&getTheWebAssemblyTarget32(), &getTheWebAssemblyTarget64()}) {
Dan Gohman10e730a2015-06-29 23:51:55 +0000100 // Register the MC asm info.
Dan Gohmancceedf72016-01-08 00:43:54 +0000101 RegisterMCAsmInfoFn X(*T, createMCAsmInfo);
Dan Gohman10e730a2015-06-29 23:51:55 +0000102
Dan Gohmane9361d52015-11-05 19:28:16 +0000103 // Register the MC instruction info.
Dan Gohmancceedf72016-01-08 00:43:54 +0000104 TargetRegistry::RegisterMCInstrInfo(*T, createMCInstrInfo);
Dan Gohmane9361d52015-11-05 19:28:16 +0000105
Dan Gohman0656f5f2016-01-12 21:27:55 +0000106 // Register the MC register info.
107 TargetRegistry::RegisterMCRegInfo(*T, createMCRegisterInfo);
108
Dan Gohman10e730a2015-06-29 23:51:55 +0000109 // Register the MCInstPrinter.
Dan Gohmancceedf72016-01-08 00:43:54 +0000110 TargetRegistry::RegisterMCInstPrinter(*T, createMCInstPrinter);
Dan Gohman05ac43f2015-12-17 01:39:00 +0000111
Dan Gohman4ef99432016-01-08 01:18:00 +0000112 // Register the MC code emitter.
Dan Gohmancceedf72016-01-08 00:43:54 +0000113 TargetRegistry::RegisterMCCodeEmitter(*T, createCodeEmitter);
Dan Gohman05ac43f2015-12-17 01:39:00 +0000114
Dan Gohman4ef99432016-01-08 01:18:00 +0000115 // Register the ASM Backend.
Dan Gohmancceedf72016-01-08 00:43:54 +0000116 TargetRegistry::RegisterMCAsmBackend(*T, createAsmBackend);
Dan Gohmanafd7e3a2016-01-12 03:30:06 +0000117
118 // Register the MC subtarget info.
119 TargetRegistry::RegisterMCSubtargetInfo(*T, createMCSubtargetInfo);
Dan Gohman3469ee12016-01-12 20:30:51 +0000120
121 // Register the object target streamer.
122 TargetRegistry::RegisterObjectTargetStreamer(*T,
123 createObjectTargetStreamer);
124 // Register the asm target streamer.
125 TargetRegistry::RegisterAsmTargetStreamer(*T, createAsmTargetStreamer);
Heejin Ahne0f8b9b2018-11-18 11:58:47 +0000126 // Register the null target streamer.
127 TargetRegistry::RegisterNullTargetStreamer(*T, createNullTargetStreamer);
Dan Gohman10e730a2015-06-29 23:51:55 +0000128 }
129}
Dan Gohman3acb1872016-10-24 23:27:49 +0000130
Derek Schuffe2688c42017-03-14 20:23:22 +0000131wasm::ValType WebAssembly::toValType(const MVT &Ty) {
Dan Gohman3acb1872016-10-24 23:27:49 +0000132 switch (Ty.SimpleTy) {
Heejin Ahnf208f632018-09-05 01:27:38 +0000133 case MVT::i32:
134 return wasm::ValType::I32;
135 case MVT::i64:
136 return wasm::ValType::I64;
137 case MVT::f32:
138 return wasm::ValType::F32;
139 case MVT::f64:
140 return wasm::ValType::F64;
Thomas Lively6f21a132018-09-20 22:04:44 +0000141 case MVT::v16i8:
142 case MVT::v8i16:
143 case MVT::v4i32:
144 case MVT::v2i64:
145 case MVT::v4f32:
146 case MVT::v2f64:
147 return wasm::ValType::V128;
Heejin Ahnf208f632018-09-05 01:27:38 +0000148 case MVT::ExceptRef:
149 return wasm::ValType::EXCEPT_REF;
150 default:
151 llvm_unreachable("unexpected type");
Dan Gohman3acb1872016-10-24 23:27:49 +0000152 }
153}