blob: 9580eeaa33d7347ec46d838cc092f67908b36af0 [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
Rafael Espindolad86e8bb2016-06-30 18:25:11 +000044static void adjustCodeGenOpts(const Triple & /*TT*/, Reloc::Model /*RM*/,
45 CodeModel::Model &CM) {
Derek Schuff71434ff2016-02-17 23:20:43 +000046 CodeModel::Model M = (CM == CodeModel::Default || CM == CodeModel::JITDefault)
47 ? CodeModel::Large
48 : CM;
49 if (M != CodeModel::Large)
50 report_fatal_error("Non-large code models are not supported yet");
Derek Schuff71434ff2016-02-17 23:20:43 +000051}
52
Dan Gohmancceedf72016-01-08 00:43:54 +000053static MCInstrInfo *createMCInstrInfo() {
Dan Gohmane9361d52015-11-05 19:28:16 +000054 MCInstrInfo *X = new MCInstrInfo();
55 InitWebAssemblyMCInstrInfo(X);
56 return X;
57}
58
Dan Gohman0656f5f2016-01-12 21:27:55 +000059static MCRegisterInfo *createMCRegisterInfo(const Triple & /*T*/) {
60 MCRegisterInfo *X = new MCRegisterInfo();
61 InitWebAssemblyMCRegisterInfo(X, 0);
62 return X;
63}
64
Dan Gohmancceedf72016-01-08 00:43:54 +000065static MCInstPrinter *createMCInstPrinter(const Triple & /*T*/,
66 unsigned SyntaxVariant,
67 const MCAsmInfo &MAI,
68 const MCInstrInfo &MII,
69 const MCRegisterInfo &MRI) {
Dan Gohman83947562016-01-20 05:54:22 +000070 assert(SyntaxVariant == 0 && "WebAssembly only has one syntax variant");
JF Bastienae7eebd2015-07-28 17:23:07 +000071 return new WebAssemblyInstPrinter(MAI, MII, MRI);
Dan Gohman10e730a2015-06-29 23:51:55 +000072}
73
Dan Gohmancceedf72016-01-08 00:43:54 +000074static MCCodeEmitter *createCodeEmitter(const MCInstrInfo &MCII,
75 const MCRegisterInfo & /*MRI*/,
Dan Gohmandf4f4d42017-02-10 00:14:42 +000076 MCContext &Ctx) {
Sam Clegg9d24fb72017-06-16 23:59:10 +000077 return createWebAssemblyMCCodeEmitter(MCII);
Dan Gohmancceedf72016-01-08 00:43:54 +000078}
79
80static MCAsmBackend *createAsmBackend(const Target & /*T*/,
81 const MCRegisterInfo & /*MRI*/,
David Blaikiebef810f2016-07-25 21:41:42 +000082 const Triple &TT, StringRef /*CPU*/,
83 const MCTargetOptions & /*Options*/) {
Dan Gohmancceedf72016-01-08 00:43:54 +000084 return createWebAssemblyAsmBackend(TT);
85}
86
Dan Gohmanafd7e3a2016-01-12 03:30:06 +000087static MCSubtargetInfo *createMCSubtargetInfo(const Triple &TT, StringRef CPU,
88 StringRef FS) {
89 return createWebAssemblyMCSubtargetInfoImpl(TT, CPU, FS);
90}
91
Dan Gohman3469ee12016-01-12 20:30:51 +000092static MCTargetStreamer *
Dan Gohman18eafb62017-02-22 01:23:18 +000093createObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) {
94 const Triple &TT = STI.getTargetTriple();
95 if (TT.isOSBinFormatELF())
96 return new WebAssemblyTargetELFStreamer(S);
97
98 return new WebAssemblyTargetWasmStreamer(S);
Dan Gohman3469ee12016-01-12 20:30:51 +000099}
100
101static MCTargetStreamer *createAsmTargetStreamer(MCStreamer &S,
102 formatted_raw_ostream &OS,
103 MCInstPrinter * /*InstPrint*/,
104 bool /*isVerboseAsm*/) {
105 return new WebAssemblyTargetAsmStreamer(S, OS);
106}
107
Dan Gohman10e730a2015-06-29 23:51:55 +0000108// Force static initialization.
109extern "C" void LLVMInitializeWebAssemblyTargetMC() {
Mehdi Aminif42454b2016-10-09 23:00:34 +0000110 for (Target *T :
111 {&getTheWebAssemblyTarget32(), &getTheWebAssemblyTarget64()}) {
Dan Gohman10e730a2015-06-29 23:51:55 +0000112 // Register the MC asm info.
Dan Gohmancceedf72016-01-08 00:43:54 +0000113 RegisterMCAsmInfoFn X(*T, createMCAsmInfo);
Dan Gohman10e730a2015-06-29 23:51:55 +0000114
Dan Gohmane9361d52015-11-05 19:28:16 +0000115 // Register the MC instruction info.
Dan Gohmancceedf72016-01-08 00:43:54 +0000116 TargetRegistry::RegisterMCInstrInfo(*T, createMCInstrInfo);
Dan Gohmane9361d52015-11-05 19:28:16 +0000117
Derek Schuff71434ff2016-02-17 23:20:43 +0000118 // Register the MC codegen info.
Rafael Espindolad86e8bb2016-06-30 18:25:11 +0000119 TargetRegistry::registerMCAdjustCodeGenOpts(*T, adjustCodeGenOpts);
Derek Schuff71434ff2016-02-17 23:20:43 +0000120
Dan Gohman0656f5f2016-01-12 21:27:55 +0000121 // Register the MC register info.
122 TargetRegistry::RegisterMCRegInfo(*T, createMCRegisterInfo);
123
Dan Gohman10e730a2015-06-29 23:51:55 +0000124 // Register the MCInstPrinter.
Dan Gohmancceedf72016-01-08 00:43:54 +0000125 TargetRegistry::RegisterMCInstPrinter(*T, createMCInstPrinter);
Dan Gohman05ac43f2015-12-17 01:39:00 +0000126
Dan Gohman4ef99432016-01-08 01:18:00 +0000127 // Register the MC code emitter.
Dan Gohmancceedf72016-01-08 00:43:54 +0000128 TargetRegistry::RegisterMCCodeEmitter(*T, createCodeEmitter);
Dan Gohman05ac43f2015-12-17 01:39:00 +0000129
Dan Gohman4ef99432016-01-08 01:18:00 +0000130 // Register the ASM Backend.
Dan Gohmancceedf72016-01-08 00:43:54 +0000131 TargetRegistry::RegisterMCAsmBackend(*T, createAsmBackend);
Dan Gohmanafd7e3a2016-01-12 03:30:06 +0000132
133 // Register the MC subtarget info.
134 TargetRegistry::RegisterMCSubtargetInfo(*T, createMCSubtargetInfo);
Dan Gohman3469ee12016-01-12 20:30:51 +0000135
136 // Register the object target streamer.
137 TargetRegistry::RegisterObjectTargetStreamer(*T,
138 createObjectTargetStreamer);
139 // Register the asm target streamer.
140 TargetRegistry::RegisterAsmTargetStreamer(*T, createAsmTargetStreamer);
Dan Gohman10e730a2015-06-29 23:51:55 +0000141 }
142}
Dan Gohman3acb1872016-10-24 23:27:49 +0000143
Derek Schuffe2688c42017-03-14 20:23:22 +0000144wasm::ValType WebAssembly::toValType(const MVT &Ty) {
Dan Gohman3acb1872016-10-24 23:27:49 +0000145 switch (Ty.SimpleTy) {
Derek Schuffe2688c42017-03-14 20:23:22 +0000146 case MVT::i32: return wasm::ValType::I32;
147 case MVT::i64: return wasm::ValType::I64;
148 case MVT::f32: return wasm::ValType::F32;
149 case MVT::f64: return wasm::ValType::F64;
Dan Gohman3acb1872016-10-24 23:27:49 +0000150 default: llvm_unreachable("unexpected type");
151 }
152}