Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 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" |
Dan Gohman | 3469ee1 | 2016-01-12 20:30:51 +0000 | [diff] [blame] | 18 | #include "WebAssemblyTargetStreamer.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCInstrInfo.h" |
| 20 | #include "llvm/MC/MCRegisterInfo.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 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 | |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 28 | #define GET_INSTRINFO_MC_DESC |
| 29 | #include "WebAssemblyGenInstrInfo.inc" |
| 30 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 31 | #define GET_SUBTARGETINFO_MC_DESC |
| 32 | #include "WebAssemblyGenSubtargetInfo.inc" |
| 33 | |
JF Bastien | 5ca0bac | 2015-07-10 18:23:10 +0000 | [diff] [blame] | 34 | #define GET_REGINFO_MC_DESC |
| 35 | #include "WebAssemblyGenRegisterInfo.inc" |
| 36 | |
Dan Gohman | cceedf7 | 2016-01-08 00:43:54 +0000 | [diff] [blame] | 37 | static MCAsmInfo *createMCAsmInfo(const MCRegisterInfo & /*MRI*/, |
| 38 | const Triple &TT) { |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 39 | if (TT.isOSBinFormatELF()) |
| 40 | return new WebAssemblyMCAsmInfoELF(TT); |
JF Bastien | ae7eebd | 2015-07-28 17:23:07 +0000 | [diff] [blame] | 41 | return new WebAssemblyMCAsmInfo(TT); |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Rafael Espindola | d86e8bb | 2016-06-30 18:25:11 +0000 | [diff] [blame] | 44 | static void adjustCodeGenOpts(const Triple & /*TT*/, Reloc::Model /*RM*/, |
| 45 | CodeModel::Model &CM) { |
Derek Schuff | 71434ff | 2016-02-17 23:20:43 +0000 | [diff] [blame] | 46 | 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 Schuff | 71434ff | 2016-02-17 23:20:43 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Dan Gohman | cceedf7 | 2016-01-08 00:43:54 +0000 | [diff] [blame] | 53 | static MCInstrInfo *createMCInstrInfo() { |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 54 | MCInstrInfo *X = new MCInstrInfo(); |
| 55 | InitWebAssemblyMCInstrInfo(X); |
| 56 | return X; |
| 57 | } |
| 58 | |
Dan Gohman | 0656f5f | 2016-01-12 21:27:55 +0000 | [diff] [blame] | 59 | static MCRegisterInfo *createMCRegisterInfo(const Triple & /*T*/) { |
| 60 | MCRegisterInfo *X = new MCRegisterInfo(); |
| 61 | InitWebAssemblyMCRegisterInfo(X, 0); |
| 62 | return X; |
| 63 | } |
| 64 | |
Dan Gohman | cceedf7 | 2016-01-08 00:43:54 +0000 | [diff] [blame] | 65 | static MCInstPrinter *createMCInstPrinter(const Triple & /*T*/, |
| 66 | unsigned SyntaxVariant, |
| 67 | const MCAsmInfo &MAI, |
| 68 | const MCInstrInfo &MII, |
| 69 | const MCRegisterInfo &MRI) { |
Dan Gohman | 8394756 | 2016-01-20 05:54:22 +0000 | [diff] [blame] | 70 | assert(SyntaxVariant == 0 && "WebAssembly only has one syntax variant"); |
JF Bastien | ae7eebd | 2015-07-28 17:23:07 +0000 | [diff] [blame] | 71 | return new WebAssemblyInstPrinter(MAI, MII, MRI); |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Dan Gohman | cceedf7 | 2016-01-08 00:43:54 +0000 | [diff] [blame] | 74 | static MCCodeEmitter *createCodeEmitter(const MCInstrInfo &MCII, |
| 75 | const MCRegisterInfo & /*MRI*/, |
Dan Gohman | df4f4d4 | 2017-02-10 00:14:42 +0000 | [diff] [blame] | 76 | MCContext &Ctx) { |
Sam Clegg | 9d24fb7 | 2017-06-16 23:59:10 +0000 | [diff] [blame^] | 77 | return createWebAssemblyMCCodeEmitter(MCII); |
Dan Gohman | cceedf7 | 2016-01-08 00:43:54 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static MCAsmBackend *createAsmBackend(const Target & /*T*/, |
| 81 | const MCRegisterInfo & /*MRI*/, |
David Blaikie | bef810f | 2016-07-25 21:41:42 +0000 | [diff] [blame] | 82 | const Triple &TT, StringRef /*CPU*/, |
| 83 | const MCTargetOptions & /*Options*/) { |
Dan Gohman | cceedf7 | 2016-01-08 00:43:54 +0000 | [diff] [blame] | 84 | return createWebAssemblyAsmBackend(TT); |
| 85 | } |
| 86 | |
Dan Gohman | afd7e3a | 2016-01-12 03:30:06 +0000 | [diff] [blame] | 87 | static MCSubtargetInfo *createMCSubtargetInfo(const Triple &TT, StringRef CPU, |
| 88 | StringRef FS) { |
| 89 | return createWebAssemblyMCSubtargetInfoImpl(TT, CPU, FS); |
| 90 | } |
| 91 | |
Dan Gohman | 3469ee1 | 2016-01-12 20:30:51 +0000 | [diff] [blame] | 92 | static MCTargetStreamer * |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 93 | createObjectTargetStreamer(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 Gohman | 3469ee1 | 2016-01-12 20:30:51 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | static MCTargetStreamer *createAsmTargetStreamer(MCStreamer &S, |
| 102 | formatted_raw_ostream &OS, |
| 103 | MCInstPrinter * /*InstPrint*/, |
| 104 | bool /*isVerboseAsm*/) { |
| 105 | return new WebAssemblyTargetAsmStreamer(S, OS); |
| 106 | } |
| 107 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 108 | // Force static initialization. |
| 109 | extern "C" void LLVMInitializeWebAssemblyTargetMC() { |
Mehdi Amini | f42454b | 2016-10-09 23:00:34 +0000 | [diff] [blame] | 110 | for (Target *T : |
| 111 | {&getTheWebAssemblyTarget32(), &getTheWebAssemblyTarget64()}) { |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 112 | // Register the MC asm info. |
Dan Gohman | cceedf7 | 2016-01-08 00:43:54 +0000 | [diff] [blame] | 113 | RegisterMCAsmInfoFn X(*T, createMCAsmInfo); |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 114 | |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 115 | // Register the MC instruction info. |
Dan Gohman | cceedf7 | 2016-01-08 00:43:54 +0000 | [diff] [blame] | 116 | TargetRegistry::RegisterMCInstrInfo(*T, createMCInstrInfo); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 117 | |
Derek Schuff | 71434ff | 2016-02-17 23:20:43 +0000 | [diff] [blame] | 118 | // Register the MC codegen info. |
Rafael Espindola | d86e8bb | 2016-06-30 18:25:11 +0000 | [diff] [blame] | 119 | TargetRegistry::registerMCAdjustCodeGenOpts(*T, adjustCodeGenOpts); |
Derek Schuff | 71434ff | 2016-02-17 23:20:43 +0000 | [diff] [blame] | 120 | |
Dan Gohman | 0656f5f | 2016-01-12 21:27:55 +0000 | [diff] [blame] | 121 | // Register the MC register info. |
| 122 | TargetRegistry::RegisterMCRegInfo(*T, createMCRegisterInfo); |
| 123 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 124 | // Register the MCInstPrinter. |
Dan Gohman | cceedf7 | 2016-01-08 00:43:54 +0000 | [diff] [blame] | 125 | TargetRegistry::RegisterMCInstPrinter(*T, createMCInstPrinter); |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 126 | |
Dan Gohman | 4ef9943 | 2016-01-08 01:18:00 +0000 | [diff] [blame] | 127 | // Register the MC code emitter. |
Dan Gohman | cceedf7 | 2016-01-08 00:43:54 +0000 | [diff] [blame] | 128 | TargetRegistry::RegisterMCCodeEmitter(*T, createCodeEmitter); |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 129 | |
Dan Gohman | 4ef9943 | 2016-01-08 01:18:00 +0000 | [diff] [blame] | 130 | // Register the ASM Backend. |
Dan Gohman | cceedf7 | 2016-01-08 00:43:54 +0000 | [diff] [blame] | 131 | TargetRegistry::RegisterMCAsmBackend(*T, createAsmBackend); |
Dan Gohman | afd7e3a | 2016-01-12 03:30:06 +0000 | [diff] [blame] | 132 | |
| 133 | // Register the MC subtarget info. |
| 134 | TargetRegistry::RegisterMCSubtargetInfo(*T, createMCSubtargetInfo); |
Dan Gohman | 3469ee1 | 2016-01-12 20:30:51 +0000 | [diff] [blame] | 135 | |
| 136 | // Register the object target streamer. |
| 137 | TargetRegistry::RegisterObjectTargetStreamer(*T, |
| 138 | createObjectTargetStreamer); |
| 139 | // Register the asm target streamer. |
| 140 | TargetRegistry::RegisterAsmTargetStreamer(*T, createAsmTargetStreamer); |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
Dan Gohman | 3acb187 | 2016-10-24 23:27:49 +0000 | [diff] [blame] | 143 | |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 144 | wasm::ValType WebAssembly::toValType(const MVT &Ty) { |
Dan Gohman | 3acb187 | 2016-10-24 23:27:49 +0000 | [diff] [blame] | 145 | switch (Ty.SimpleTy) { |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 146 | 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 Gohman | 3acb187 | 2016-10-24 23:27:49 +0000 | [diff] [blame] | 150 | default: llvm_unreachable("unexpected type"); |
| 151 | } |
| 152 | } |