blob: a51fb9282fee6a9c6c065370605d3542fed59562 [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
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"
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) {
Dan Gohman18eafb62017-02-22 01:23:18 +000039 if (TT.isOSBinFormatELF())
40 return new WebAssemblyMCAsmInfoELF(TT);
JF Bastienae7eebd2015-07-28 17:23:07 +000041 return new WebAssemblyMCAsmInfo(TT);
Dan Gohman10e730a2015-06-29 23:51:55 +000042}
43
Dan Gohmancceedf72016-01-08 00:43:54 +000044static MCInstrInfo *createMCInstrInfo() {
Dan Gohmane9361d52015-11-05 19:28:16 +000045 MCInstrInfo *X = new MCInstrInfo();
46 InitWebAssemblyMCInstrInfo(X);
47 return X;
48}
49
Dan Gohman0656f5f2016-01-12 21:27:55 +000050static MCRegisterInfo *createMCRegisterInfo(const Triple & /*T*/) {
51 MCRegisterInfo *X = new MCRegisterInfo();
52 InitWebAssemblyMCRegisterInfo(X, 0);
53 return X;
54}
55
Dan Gohmancceedf72016-01-08 00:43:54 +000056static MCInstPrinter *createMCInstPrinter(const Triple & /*T*/,
57 unsigned SyntaxVariant,
58 const MCAsmInfo &MAI,
59 const MCInstrInfo &MII,
60 const MCRegisterInfo &MRI) {
Dan Gohman83947562016-01-20 05:54:22 +000061 assert(SyntaxVariant == 0 && "WebAssembly only has one syntax variant");
JF Bastienae7eebd2015-07-28 17:23:07 +000062 return new WebAssemblyInstPrinter(MAI, MII, MRI);
Dan Gohman10e730a2015-06-29 23:51:55 +000063}
64
Dan Gohmancceedf72016-01-08 00:43:54 +000065static MCCodeEmitter *createCodeEmitter(const MCInstrInfo &MCII,
66 const MCRegisterInfo & /*MRI*/,
Dan Gohmandf4f4d42017-02-10 00:14:42 +000067 MCContext &Ctx) {
Sam Clegg9d24fb72017-06-16 23:59:10 +000068 return createWebAssemblyMCCodeEmitter(MCII);
Dan Gohmancceedf72016-01-08 00:43:54 +000069}
70
71static MCAsmBackend *createAsmBackend(const Target & /*T*/,
Alex Bradbury7c093bf2018-01-03 09:30:39 +000072 const MCSubtargetInfo &STI,
Dan Gohmancceedf72016-01-08 00:43:54 +000073 const MCRegisterInfo & /*MRI*/,
David Blaikiebef810f2016-07-25 21:41:42 +000074 const MCTargetOptions & /*Options*/) {
Alex Bradbury7c093bf2018-01-03 09:30:39 +000075 return createWebAssemblyAsmBackend(STI.getTargetTriple());
Dan Gohmancceedf72016-01-08 00:43:54 +000076}
77
Dan Gohmanafd7e3a2016-01-12 03:30:06 +000078static MCSubtargetInfo *createMCSubtargetInfo(const Triple &TT, StringRef CPU,
79 StringRef FS) {
80 return createWebAssemblyMCSubtargetInfoImpl(TT, CPU, FS);
81}
82
Dan Gohman3469ee12016-01-12 20:30:51 +000083static MCTargetStreamer *
Dan Gohman18eafb62017-02-22 01:23:18 +000084createObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) {
85 const Triple &TT = STI.getTargetTriple();
86 if (TT.isOSBinFormatELF())
87 return new WebAssemblyTargetELFStreamer(S);
88
89 return new WebAssemblyTargetWasmStreamer(S);
Dan Gohman3469ee12016-01-12 20:30:51 +000090}
91
92static MCTargetStreamer *createAsmTargetStreamer(MCStreamer &S,
93 formatted_raw_ostream &OS,
94 MCInstPrinter * /*InstPrint*/,
95 bool /*isVerboseAsm*/) {
96 return new WebAssemblyTargetAsmStreamer(S, OS);
97}
98
Dan Gohman10e730a2015-06-29 23:51:55 +000099// Force static initialization.
100extern "C" void LLVMInitializeWebAssemblyTargetMC() {
Mehdi Aminif42454b2016-10-09 23:00:34 +0000101 for (Target *T :
102 {&getTheWebAssemblyTarget32(), &getTheWebAssemblyTarget64()}) {
Dan Gohman10e730a2015-06-29 23:51:55 +0000103 // Register the MC asm info.
Dan Gohmancceedf72016-01-08 00:43:54 +0000104 RegisterMCAsmInfoFn X(*T, createMCAsmInfo);
Dan Gohman10e730a2015-06-29 23:51:55 +0000105
Dan Gohmane9361d52015-11-05 19:28:16 +0000106 // Register the MC instruction info.
Dan Gohmancceedf72016-01-08 00:43:54 +0000107 TargetRegistry::RegisterMCInstrInfo(*T, createMCInstrInfo);
Dan Gohmane9361d52015-11-05 19:28:16 +0000108
Dan Gohman0656f5f2016-01-12 21:27:55 +0000109 // Register the MC register info.
110 TargetRegistry::RegisterMCRegInfo(*T, createMCRegisterInfo);
111
Dan Gohman10e730a2015-06-29 23:51:55 +0000112 // Register the MCInstPrinter.
Dan Gohmancceedf72016-01-08 00:43:54 +0000113 TargetRegistry::RegisterMCInstPrinter(*T, createMCInstPrinter);
Dan Gohman05ac43f2015-12-17 01:39:00 +0000114
Dan Gohman4ef99432016-01-08 01:18:00 +0000115 // Register the MC code emitter.
Dan Gohmancceedf72016-01-08 00:43:54 +0000116 TargetRegistry::RegisterMCCodeEmitter(*T, createCodeEmitter);
Dan Gohman05ac43f2015-12-17 01:39:00 +0000117
Dan Gohman4ef99432016-01-08 01:18:00 +0000118 // Register the ASM Backend.
Dan Gohmancceedf72016-01-08 00:43:54 +0000119 TargetRegistry::RegisterMCAsmBackend(*T, createAsmBackend);
Dan Gohmanafd7e3a2016-01-12 03:30:06 +0000120
121 // Register the MC subtarget info.
122 TargetRegistry::RegisterMCSubtargetInfo(*T, createMCSubtargetInfo);
Dan Gohman3469ee12016-01-12 20:30:51 +0000123
124 // Register the object target streamer.
125 TargetRegistry::RegisterObjectTargetStreamer(*T,
126 createObjectTargetStreamer);
127 // Register the asm target streamer.
128 TargetRegistry::RegisterAsmTargetStreamer(*T, createAsmTargetStreamer);
Dan Gohman10e730a2015-06-29 23:51:55 +0000129 }
130}
Dan Gohman3acb1872016-10-24 23:27:49 +0000131
Derek Schuffe2688c42017-03-14 20:23:22 +0000132wasm::ValType WebAssembly::toValType(const MVT &Ty) {
Dan Gohman3acb1872016-10-24 23:27:49 +0000133 switch (Ty.SimpleTy) {
Derek Schuffe2688c42017-03-14 20:23:22 +0000134 case MVT::i32: return wasm::ValType::I32;
135 case MVT::i64: return wasm::ValType::I64;
136 case MVT::f32: return wasm::ValType::F32;
137 case MVT::f64: return wasm::ValType::F64;
Heejin Ahn0de58722018-03-08 04:05:37 +0000138 case MVT::ExceptRef: return wasm::ValType::EXCEPT_REF;
Dan Gohman3acb1872016-10-24 23:27:49 +0000139 default: llvm_unreachable("unexpected type");
140 }
141}