JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 1 | //===-- WebAssemblyAsmPrinter.cpp - WebAssembly LLVM assembly writer ------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 10 | /// This file contains a printer that converts from our internal |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 11 | /// representation of machine-dependent LLVM code to the WebAssembly assembly |
| 12 | /// language. |
| 13 | /// |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 16 | #include "WebAssemblyAsmPrinter.h" |
David L. Jones | a263aa2 | 2019-05-13 03:32:41 +0000 | [diff] [blame] | 17 | #include "MCTargetDesc/WebAssemblyInstPrinter.h" |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 18 | #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" |
Dan Gohman | 3469ee1 | 2016-01-12 20:30:51 +0000 | [diff] [blame] | 19 | #include "MCTargetDesc/WebAssemblyTargetStreamer.h" |
Derek Schuff | c64d765 | 2016-08-01 22:25:02 +0000 | [diff] [blame] | 20 | #include "WebAssembly.h" |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 21 | #include "WebAssemblyMCInstLower.h" |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 22 | #include "WebAssemblyMachineFunctionInfo.h" |
| 23 | #include "WebAssemblyRegisterInfo.h" |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 24 | #include "WebAssemblyTargetMachine.h" |
Thomas Lively | cbda16e | 2019-01-17 02:29:55 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallSet.h" |
Dan Gohman | e51c058 | 2015-10-06 00:27:55 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/StringExtras.h" |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 27 | #include "llvm/BinaryFormat/Wasm.h" |
Dan Gohman | 754cd11 | 2015-11-11 01:33:02 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/Analysis.h" |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/AsmPrinter.h" |
JF Bastien | 54be3b1 | 2015-08-25 23:19:49 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineConstantPool.h" |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineInstr.h" |
Dan Gohman | 82607f5 | 2017-02-24 23:46:05 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineModuleInfoImpls.h" |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 33 | #include "llvm/IR/DataLayout.h" |
Thomas Lively | c6795e0 | 2019-01-18 02:47:48 +0000 | [diff] [blame] | 34 | #include "llvm/IR/DebugInfoMetadata.h" |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 35 | #include "llvm/IR/GlobalVariable.h" |
Thomas Lively | 3f34e1b8 | 2019-03-29 00:14:01 +0000 | [diff] [blame] | 36 | #include "llvm/IR/Metadata.h" |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 37 | #include "llvm/MC/MCContext.h" |
Sam Clegg | cfd44a2 | 2018-04-05 17:01:39 +0000 | [diff] [blame] | 38 | #include "llvm/MC/MCSectionWasm.h" |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 39 | #include "llvm/MC/MCStreamer.h" |
JF Bastien | b6091df | 2015-08-25 22:58:05 +0000 | [diff] [blame] | 40 | #include "llvm/MC/MCSymbol.h" |
Sam Clegg | cfd44a2 | 2018-04-05 17:01:39 +0000 | [diff] [blame] | 41 | #include "llvm/MC/MCSymbolWasm.h" |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 42 | #include "llvm/Support/Debug.h" |
| 43 | #include "llvm/Support/TargetRegistry.h" |
| 44 | #include "llvm/Support/raw_ostream.h" |
Thomas Lively | 2e15040 | 2019-02-19 22:56:19 +0000 | [diff] [blame] | 45 | |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 46 | using namespace llvm; |
| 47 | |
| 48 | #define DEBUG_TYPE "asm-printer" |
| 49 | |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 50 | extern cl::opt<bool> WasmKeepRegisters; |
| 51 | |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 52 | //===----------------------------------------------------------------------===// |
JF Bastien | 45479f6 | 2015-08-26 22:09:54 +0000 | [diff] [blame] | 53 | // Helpers. |
| 54 | //===----------------------------------------------------------------------===// |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 55 | |
Dan Gohman | 53828fd | 2015-11-23 16:50:18 +0000 | [diff] [blame] | 56 | MVT WebAssemblyAsmPrinter::getRegType(unsigned RegNo) const { |
Krzysztof Parzyszek | c8e8e2a | 2017-04-24 19:51:12 +0000 | [diff] [blame] | 57 | const TargetRegisterInfo *TRI = Subtarget->getRegisterInfo(); |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 58 | const TargetRegisterClass *TRC = MRI->getRegClass(RegNo); |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 59 | for (MVT T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64, MVT::v16i8, MVT::v8i16, |
Derek Schuff | 51ed131 | 2018-08-07 21:24:01 +0000 | [diff] [blame] | 60 | MVT::v4i32, MVT::v2i64, MVT::v4f32, MVT::v2f64}) |
Krzysztof Parzyszek | c8e8e2a | 2017-04-24 19:51:12 +0000 | [diff] [blame] | 61 | if (TRI->isTypeLegalForClass(*TRC, T)) |
Dan Gohman | 53828fd | 2015-11-23 16:50:18 +0000 | [diff] [blame] | 62 | return T; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 63 | LLVM_DEBUG(errs() << "Unknown type for register number: " << RegNo); |
JF Bastien | 1d20a5e | 2015-10-16 00:53:49 +0000 | [diff] [blame] | 64 | llvm_unreachable("Unknown register type"); |
Dan Gohman | 53828fd | 2015-11-23 16:50:18 +0000 | [diff] [blame] | 65 | return MVT::Other; |
JF Bastien | 1d20a5e | 2015-10-16 00:53:49 +0000 | [diff] [blame] | 66 | } |
| 67 | |
JF Bastien | 1d20a5e | 2015-10-16 00:53:49 +0000 | [diff] [blame] | 68 | std::string WebAssemblyAsmPrinter::regToString(const MachineOperand &MO) { |
| 69 | unsigned RegNo = MO.getReg(); |
Dan Gohman | d962527 | 2015-11-20 03:13:31 +0000 | [diff] [blame] | 70 | assert(TargetRegisterInfo::isVirtualRegister(RegNo) && |
| 71 | "Unlowered physical register encountered during assembly printing"); |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 72 | assert(!MFI->isVRegStackified(RegNo)); |
Dan Gohman | 058fce5 | 2015-11-13 00:21:05 +0000 | [diff] [blame] | 73 | unsigned WAReg = MFI->getWAReg(RegNo); |
| 74 | assert(WAReg != WebAssemblyFunctionInfo::UnusedReg); |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 75 | return '$' + utostr(WAReg); |
Dan Gohman | e51c058 | 2015-10-06 00:27:55 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Dan Gohman | ec977b0 | 2016-01-25 15:12:05 +0000 | [diff] [blame] | 78 | WebAssemblyTargetStreamer *WebAssemblyAsmPrinter::getTargetStreamer() { |
Dan Gohman | 3469ee1 | 2016-01-12 20:30:51 +0000 | [diff] [blame] | 79 | MCTargetStreamer *TS = OutStreamer->getTargetStreamer(); |
| 80 | return static_cast<WebAssemblyTargetStreamer *>(TS); |
JF Bastien | 73ff6af | 2015-08-31 22:24:11 +0000 | [diff] [blame] | 81 | } |
| 82 | |
JF Bastien | 45479f6 | 2015-08-26 22:09:54 +0000 | [diff] [blame] | 83 | //===----------------------------------------------------------------------===// |
| 84 | // WebAssemblyAsmPrinter Implementation. |
| 85 | //===----------------------------------------------------------------------===// |
Derek Schuff | 46e3316 | 2015-11-16 21:12:41 +0000 | [diff] [blame] | 86 | |
Derek Schuff | 5859a9ed | 2016-06-03 18:34:36 +0000 | [diff] [blame] | 87 | void WebAssemblyAsmPrinter::EmitEndOfAsmFile(Module &M) { |
Wouter van Oortmerssen | 3231e51 | 2018-11-02 00:45:00 +0000 | [diff] [blame] | 88 | for (auto &It : OutContext.getSymbols()) { |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 89 | // Emit a .globaltype and .eventtype declaration. |
Wouter van Oortmerssen | 3231e51 | 2018-11-02 00:45:00 +0000 | [diff] [blame] | 90 | auto Sym = cast<MCSymbolWasm>(It.getValue()); |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 91 | if (Sym->getType() == wasm::WASM_SYMBOL_TYPE_GLOBAL) |
Wouter van Oortmerssen | 3231e51 | 2018-11-02 00:45:00 +0000 | [diff] [blame] | 92 | getTargetStreamer()->emitGlobalType(Sym); |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 93 | else if (Sym->getType() == wasm::WASM_SYMBOL_TYPE_EVENT) |
| 94 | getTargetStreamer()->emitEventType(Sym); |
Wouter van Oortmerssen | 3231e51 | 2018-11-02 00:45:00 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Derek Schuff | 5859a9ed | 2016-06-03 18:34:36 +0000 | [diff] [blame] | 97 | for (const auto &F : M) { |
| 98 | // Emit function type info for all undefined functions |
| 99 | if (F.isDeclarationForLinker() && !F.isIntrinsic()) { |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 100 | SmallVector<MVT, 4> Results; |
| 101 | SmallVector<MVT, 4> Params; |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 102 | computeSignatureVTs(F.getFunctionType(), F, TM, Params, Results); |
Derek Schuff | 77a7a38 | 2018-10-03 22:22:48 +0000 | [diff] [blame] | 103 | auto *Sym = cast<MCSymbolWasm>(getSymbol(&F)); |
Heejin Ahn | 21d45a2 | 2018-12-11 00:53:59 +0000 | [diff] [blame] | 104 | Sym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); |
Derek Schuff | 77a7a38 | 2018-10-03 22:22:48 +0000 | [diff] [blame] | 105 | if (!Sym->getSignature()) { |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 106 | auto Signature = signatureFromMVTs(Results, Params); |
Derek Schuff | 77a7a38 | 2018-10-03 22:22:48 +0000 | [diff] [blame] | 107 | Sym->setSignature(Signature.get()); |
| 108 | addSignature(std::move(Signature)); |
| 109 | } |
| 110 | // FIXME: this was originally intended for post-linking and was only used |
| 111 | // for imports that were only called indirectly (i.e. s2wasm could not |
| 112 | // infer the type from a call). With object files it applies to all |
| 113 | // imports. so fix the names and the tests, or rethink how import |
| 114 | // delcarations work in asm files. |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 115 | getTargetStreamer()->emitFunctionType(Sym); |
Dan Gohman | db1916a | 2018-02-09 23:13:22 +0000 | [diff] [blame] | 116 | |
Jacob Gravelle | ca358da | 2018-02-12 21:41:12 +0000 | [diff] [blame] | 117 | if (TM.getTargetTriple().isOSBinFormatWasm() && |
| 118 | F.hasFnAttribute("wasm-import-module")) { |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 119 | StringRef Name = |
| 120 | F.getFnAttribute("wasm-import-module").getValueAsString(); |
Dan Gohman | f726e44 | 2019-02-01 22:27:34 +0000 | [diff] [blame] | 121 | Sym->setImportModule(Name); |
Derek Schuff | 77a7a38 | 2018-10-03 22:22:48 +0000 | [diff] [blame] | 122 | getTargetStreamer()->emitImportModule(Sym, Name); |
Dan Gohman | db1916a | 2018-02-09 23:13:22 +0000 | [diff] [blame] | 123 | } |
Dan Gohman | f726e44 | 2019-02-01 22:27:34 +0000 | [diff] [blame] | 124 | if (TM.getTargetTriple().isOSBinFormatWasm() && |
| 125 | F.hasFnAttribute("wasm-import-name")) { |
| 126 | StringRef Name = |
| 127 | F.getFnAttribute("wasm-import-name").getValueAsString(); |
| 128 | Sym->setImportName(Name); |
| 129 | getTargetStreamer()->emitImportName(Sym, Name); |
| 130 | } |
Derek Schuff | 5859a9ed | 2016-06-03 18:34:36 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
Wouter van Oortmerssen | 3231e51 | 2018-11-02 00:45:00 +0000 | [diff] [blame] | 133 | |
Derek Schuff | 7747d703e | 2016-12-01 00:11:15 +0000 | [diff] [blame] | 134 | for (const auto &G : M.globals()) { |
| 135 | if (!G.hasInitializer() && G.hasExternalLinkage()) { |
Dan Gohman | 5cf6473 | 2017-12-07 00:14:30 +0000 | [diff] [blame] | 136 | if (G.getValueType()->isSized()) { |
| 137 | uint16_t Size = M.getDataLayout().getTypeAllocSize(G.getValueType()); |
Dan Gohman | 5cf6473 | 2017-12-07 00:14:30 +0000 | [diff] [blame] | 138 | OutStreamer->emitELFSize(getSymbol(&G), |
| 139 | MCConstantExpr::create(Size, OutContext)); |
| 140 | } |
Derek Schuff | 7747d703e | 2016-12-01 00:11:15 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
Sam Clegg | cfd44a2 | 2018-04-05 17:01:39 +0000 | [diff] [blame] | 143 | |
| 144 | if (const NamedMDNode *Named = M.getNamedMetadata("wasm.custom_sections")) { |
| 145 | for (const Metadata *MD : Named->operands()) { |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 146 | const auto *Tuple = dyn_cast<MDTuple>(MD); |
Sam Clegg | cfd44a2 | 2018-04-05 17:01:39 +0000 | [diff] [blame] | 147 | if (!Tuple || Tuple->getNumOperands() != 2) |
| 148 | continue; |
| 149 | const MDString *Name = dyn_cast<MDString>(Tuple->getOperand(0)); |
| 150 | const MDString *Contents = dyn_cast<MDString>(Tuple->getOperand(1)); |
| 151 | if (!Name || !Contents) |
| 152 | continue; |
| 153 | |
| 154 | OutStreamer->PushSection(); |
| 155 | std::string SectionName = (".custom_section." + Name->getString()).str(); |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 156 | MCSectionWasm *MySection = |
Sam Clegg | cfd44a2 | 2018-04-05 17:01:39 +0000 | [diff] [blame] | 157 | OutContext.getWasmSection(SectionName, SectionKind::getMetadata()); |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 158 | OutStreamer->SwitchSection(MySection); |
Sam Clegg | cfd44a2 | 2018-04-05 17:01:39 +0000 | [diff] [blame] | 159 | OutStreamer->EmitBytes(Contents->getString()); |
| 160 | OutStreamer->PopSection(); |
| 161 | } |
| 162 | } |
Thomas Lively | cbda16e | 2019-01-17 02:29:55 +0000 | [diff] [blame] | 163 | |
Thomas Lively | c6795e0 | 2019-01-18 02:47:48 +0000 | [diff] [blame] | 164 | EmitProducerInfo(M); |
Thomas Lively | 3f34e1b8 | 2019-03-29 00:14:01 +0000 | [diff] [blame] | 165 | EmitTargetFeatures(M); |
Thomas Lively | c6795e0 | 2019-01-18 02:47:48 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void WebAssemblyAsmPrinter::EmitProducerInfo(Module &M) { |
| 169 | llvm::SmallVector<std::pair<std::string, std::string>, 4> Languages; |
| 170 | if (const NamedMDNode *Debug = M.getNamedMetadata("llvm.dbg.cu")) { |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 171 | llvm::SmallSet<StringRef, 4> SeenLanguages; |
| 172 | for (size_t I = 0, E = Debug->getNumOperands(); I < E; ++I) { |
| 173 | const auto *CU = cast<DICompileUnit>(Debug->getOperand(I)); |
Thomas Lively | c6795e0 | 2019-01-18 02:47:48 +0000 | [diff] [blame] | 174 | StringRef Language = dwarf::LanguageString(CU->getSourceLanguage()); |
| 175 | Language.consume_front("DW_LANG_"); |
| 176 | if (SeenLanguages.insert(Language).second) |
| 177 | Languages.emplace_back(Language.str(), ""); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | llvm::SmallVector<std::pair<std::string, std::string>, 4> Tools; |
Thomas Lively | cbda16e | 2019-01-17 02:29:55 +0000 | [diff] [blame] | 182 | if (const NamedMDNode *Ident = M.getNamedMetadata("llvm.ident")) { |
| 183 | llvm::SmallSet<StringRef, 4> SeenTools; |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 184 | for (size_t I = 0, E = Ident->getNumOperands(); I < E; ++I) { |
| 185 | const auto *S = cast<MDString>(Ident->getOperand(I)->getOperand(0)); |
Thomas Lively | cbda16e | 2019-01-17 02:29:55 +0000 | [diff] [blame] | 186 | std::pair<StringRef, StringRef> Field = S->getString().split("version"); |
| 187 | StringRef Name = Field.first.trim(); |
| 188 | StringRef Version = Field.second.trim(); |
Thomas Lively | c6795e0 | 2019-01-18 02:47:48 +0000 | [diff] [blame] | 189 | if (SeenTools.insert(Name).second) |
| 190 | Tools.emplace_back(Name.str(), Version.str()); |
Thomas Lively | cbda16e | 2019-01-17 02:29:55 +0000 | [diff] [blame] | 191 | } |
Thomas Lively | c6795e0 | 2019-01-18 02:47:48 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | int FieldCount = int(!Languages.empty()) + int(!Tools.empty()); |
| 195 | if (FieldCount != 0) { |
Thomas Lively | cbda16e | 2019-01-17 02:29:55 +0000 | [diff] [blame] | 196 | MCSectionWasm *Producers = OutContext.getWasmSection( |
| 197 | ".custom_section.producers", SectionKind::getMetadata()); |
| 198 | OutStreamer->PushSection(); |
| 199 | OutStreamer->SwitchSection(Producers); |
Thomas Lively | c6795e0 | 2019-01-18 02:47:48 +0000 | [diff] [blame] | 200 | OutStreamer->EmitULEB128IntValue(FieldCount); |
| 201 | for (auto &Producers : {std::make_pair("language", &Languages), |
| 202 | std::make_pair("processed-by", &Tools)}) { |
| 203 | if (Producers.second->empty()) |
| 204 | continue; |
| 205 | OutStreamer->EmitULEB128IntValue(strlen(Producers.first)); |
| 206 | OutStreamer->EmitBytes(Producers.first); |
| 207 | OutStreamer->EmitULEB128IntValue(Producers.second->size()); |
| 208 | for (auto &Producer : *Producers.second) { |
| 209 | OutStreamer->EmitULEB128IntValue(Producer.first.size()); |
| 210 | OutStreamer->EmitBytes(Producer.first); |
| 211 | OutStreamer->EmitULEB128IntValue(Producer.second.size()); |
| 212 | OutStreamer->EmitBytes(Producer.second); |
| 213 | } |
Thomas Lively | cbda16e | 2019-01-17 02:29:55 +0000 | [diff] [blame] | 214 | } |
| 215 | OutStreamer->PopSection(); |
| 216 | } |
Derek Schuff | 5859a9ed | 2016-06-03 18:34:36 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Thomas Lively | 3f34e1b8 | 2019-03-29 00:14:01 +0000 | [diff] [blame] | 219 | void WebAssemblyAsmPrinter::EmitTargetFeatures(Module &M) { |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 220 | struct FeatureEntry { |
| 221 | uint8_t Prefix; |
| 222 | StringRef Name; |
| 223 | }; |
| 224 | |
Thomas Lively | 3f34e1b8 | 2019-03-29 00:14:01 +0000 | [diff] [blame] | 225 | // Read target features and linkage policies from module metadata |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 226 | SmallVector<FeatureEntry, 4> EmittedFeatures; |
Thomas Lively | 3f34e1b8 | 2019-03-29 00:14:01 +0000 | [diff] [blame] | 227 | for (const SubtargetFeatureKV &KV : WebAssemblyFeatureKV) { |
| 228 | std::string MDKey = (StringRef("wasm-feature-") + KV.Key).str(); |
| 229 | Metadata *Policy = M.getModuleFlag(MDKey); |
| 230 | if (Policy == nullptr) |
| 231 | continue; |
| 232 | |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 233 | FeatureEntry Entry; |
Thomas Lively | 3f34e1b8 | 2019-03-29 00:14:01 +0000 | [diff] [blame] | 234 | Entry.Prefix = 0; |
| 235 | Entry.Name = KV.Key; |
| 236 | |
| 237 | if (auto *MD = cast<ConstantAsMetadata>(Policy)) |
| 238 | if (auto *I = cast<ConstantInt>(MD->getValue())) |
| 239 | Entry.Prefix = I->getZExtValue(); |
| 240 | |
| 241 | // Silently ignore invalid metadata |
| 242 | if (Entry.Prefix != wasm::WASM_FEATURE_PREFIX_USED && |
| 243 | Entry.Prefix != wasm::WASM_FEATURE_PREFIX_REQUIRED && |
| 244 | Entry.Prefix != wasm::WASM_FEATURE_PREFIX_DISALLOWED) |
| 245 | continue; |
| 246 | |
| 247 | EmittedFeatures.push_back(Entry); |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Thomas Lively | 3f34e1b8 | 2019-03-29 00:14:01 +0000 | [diff] [blame] | 250 | if (EmittedFeatures.size() == 0) |
| 251 | return; |
| 252 | |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 253 | // Emit features and linkage policies into the "target_features" section |
| 254 | MCSectionWasm *FeaturesSection = OutContext.getWasmSection( |
| 255 | ".custom_section.target_features", SectionKind::getMetadata()); |
| 256 | OutStreamer->PushSection(); |
| 257 | OutStreamer->SwitchSection(FeaturesSection); |
| 258 | |
| 259 | OutStreamer->EmitULEB128IntValue(EmittedFeatures.size()); |
| 260 | for (auto &F : EmittedFeatures) { |
| 261 | OutStreamer->EmitIntValue(F.Prefix, 1); |
| 262 | OutStreamer->EmitULEB128IntValue(F.Name.size()); |
| 263 | OutStreamer->EmitBytes(F.Name); |
| 264 | } |
| 265 | |
| 266 | OutStreamer->PopSection(); |
| 267 | } |
| 268 | |
Derek Schuff | 5859a9ed | 2016-06-03 18:34:36 +0000 | [diff] [blame] | 269 | void WebAssemblyAsmPrinter::EmitConstantPool() { |
| 270 | assert(MF->getConstantPool()->getConstants().empty() && |
| 271 | "WebAssembly disables constant pools"); |
| 272 | } |
| 273 | |
| 274 | void WebAssemblyAsmPrinter::EmitJumpTableInfo() { |
| 275 | // Nothing to do; jump tables are incorporated into the instruction stream. |
| 276 | } |
| 277 | |
JF Bastien | b6091df | 2015-08-25 22:58:05 +0000 | [diff] [blame] | 278 | void WebAssemblyAsmPrinter::EmitFunctionBodyStart() { |
David Blaikie | 2110924 | 2017-12-15 23:52:06 +0000 | [diff] [blame] | 279 | const Function &F = MF->getFunction(); |
Derek Schuff | 77a7a38 | 2018-10-03 22:22:48 +0000 | [diff] [blame] | 280 | SmallVector<MVT, 1> ResultVTs; |
| 281 | SmallVector<MVT, 4> ParamVTs; |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 282 | computeSignatureVTs(F.getFunctionType(), F, TM, ParamVTs, ResultVTs); |
| 283 | auto Signature = signatureFromMVTs(ResultVTs, ParamVTs); |
Derek Schuff | 77a7a38 | 2018-10-03 22:22:48 +0000 | [diff] [blame] | 284 | auto *WasmSym = cast<MCSymbolWasm>(CurrentFnSym); |
| 285 | WasmSym->setSignature(Signature.get()); |
| 286 | addSignature(std::move(Signature)); |
Heejin Ahn | 21d45a2 | 2018-12-11 00:53:59 +0000 | [diff] [blame] | 287 | WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); |
Derek Schuff | 77a7a38 | 2018-10-03 22:22:48 +0000 | [diff] [blame] | 288 | |
| 289 | // FIXME: clean up how params and results are emitted (use signatures) |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 290 | getTargetStreamer()->emitFunctionType(WasmSym); |
Derek Schuff | c64d765 | 2016-08-01 22:25:02 +0000 | [diff] [blame] | 291 | |
| 292 | // Emit the function index. |
| 293 | if (MDNode *Idx = F.getMetadata("wasm.index")) { |
| 294 | assert(Idx->getNumOperands() == 1); |
| 295 | |
| 296 | getTargetStreamer()->emitIndIdx(AsmPrinter::lowerConstant( |
| 297 | cast<ConstantAsMetadata>(Idx->getOperand(0))->getValue())); |
| 298 | } |
| 299 | |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 300 | SmallVector<wasm::ValType, 16> Locals; |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 301 | valTypesFromMVTs(MFI->getLocals(), Locals); |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 302 | getTargetStreamer()->emitLocal(Locals); |
JF Bastien | 1d20a5e | 2015-10-16 00:53:49 +0000 | [diff] [blame] | 303 | |
Dan Gohman | e51c058 | 2015-10-06 00:27:55 +0000 | [diff] [blame] | 304 | AsmPrinter::EmitFunctionBodyStart(); |
JF Bastien | b6091df | 2015-08-25 22:58:05 +0000 | [diff] [blame] | 305 | } |
| 306 | |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 307 | void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 308 | LLVM_DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n'); |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 309 | |
Dan Gohman | e51c058 | 2015-10-06 00:27:55 +0000 | [diff] [blame] | 310 | switch (MI->getOpcode()) { |
Thomas Lively | 0ff82ac | 2018-10-13 07:09:10 +0000 | [diff] [blame] | 311 | case WebAssembly::ARGUMENT_i32: |
| 312 | case WebAssembly::ARGUMENT_i32_S: |
| 313 | case WebAssembly::ARGUMENT_i64: |
| 314 | case WebAssembly::ARGUMENT_i64_S: |
| 315 | case WebAssembly::ARGUMENT_f32: |
| 316 | case WebAssembly::ARGUMENT_f32_S: |
| 317 | case WebAssembly::ARGUMENT_f64: |
| 318 | case WebAssembly::ARGUMENT_f64_S: |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 319 | case WebAssembly::ARGUMENT_v16i8: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 320 | case WebAssembly::ARGUMENT_v16i8_S: |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 321 | case WebAssembly::ARGUMENT_v8i16: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 322 | case WebAssembly::ARGUMENT_v8i16_S: |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 323 | case WebAssembly::ARGUMENT_v4i32: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 324 | case WebAssembly::ARGUMENT_v4i32_S: |
Derek Schuff | 51ed131 | 2018-08-07 21:24:01 +0000 | [diff] [blame] | 325 | case WebAssembly::ARGUMENT_v2i64: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 326 | case WebAssembly::ARGUMENT_v2i64_S: |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 327 | case WebAssembly::ARGUMENT_v4f32: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 328 | case WebAssembly::ARGUMENT_v4f32_S: |
Derek Schuff | 51ed131 | 2018-08-07 21:24:01 +0000 | [diff] [blame] | 329 | case WebAssembly::ARGUMENT_v2f64: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 330 | case WebAssembly::ARGUMENT_v2f64_S: |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 331 | // These represent values which are live into the function entry, so there's |
| 332 | // no instruction to emit. |
Dan Gohman | e51c058 | 2015-10-06 00:27:55 +0000 | [diff] [blame] | 333 | break; |
Dan Gohman | b7c2400 | 2016-05-21 00:21:56 +0000 | [diff] [blame] | 334 | case WebAssembly::FALLTHROUGH_RETURN_I32: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 335 | case WebAssembly::FALLTHROUGH_RETURN_I32_S: |
Dan Gohman | b7c2400 | 2016-05-21 00:21:56 +0000 | [diff] [blame] | 336 | case WebAssembly::FALLTHROUGH_RETURN_I64: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 337 | case WebAssembly::FALLTHROUGH_RETURN_I64_S: |
Dan Gohman | b7c2400 | 2016-05-21 00:21:56 +0000 | [diff] [blame] | 338 | case WebAssembly::FALLTHROUGH_RETURN_F32: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 339 | case WebAssembly::FALLTHROUGH_RETURN_F32_S: |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 340 | case WebAssembly::FALLTHROUGH_RETURN_F64: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 341 | case WebAssembly::FALLTHROUGH_RETURN_F64_S: |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 342 | case WebAssembly::FALLTHROUGH_RETURN_v16i8: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 343 | case WebAssembly::FALLTHROUGH_RETURN_v16i8_S: |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 344 | case WebAssembly::FALLTHROUGH_RETURN_v8i16: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 345 | case WebAssembly::FALLTHROUGH_RETURN_v8i16_S: |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 346 | case WebAssembly::FALLTHROUGH_RETURN_v4i32: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 347 | case WebAssembly::FALLTHROUGH_RETURN_v4i32_S: |
Derek Schuff | 51ed131 | 2018-08-07 21:24:01 +0000 | [diff] [blame] | 348 | case WebAssembly::FALLTHROUGH_RETURN_v2i64: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 349 | case WebAssembly::FALLTHROUGH_RETURN_v2i64_S: |
Derek Schuff | 51ed131 | 2018-08-07 21:24:01 +0000 | [diff] [blame] | 350 | case WebAssembly::FALLTHROUGH_RETURN_v4f32: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 351 | case WebAssembly::FALLTHROUGH_RETURN_v4f32_S: |
| 352 | case WebAssembly::FALLTHROUGH_RETURN_v2f64: |
| 353 | case WebAssembly::FALLTHROUGH_RETURN_v2f64_S: { |
Dan Gohman | b7c2400 | 2016-05-21 00:21:56 +0000 | [diff] [blame] | 354 | // These instructions represent the implicit return at the end of a |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 355 | // function body. Always pops one value off the stack. |
Dan Gohman | b7c2400 | 2016-05-21 00:21:56 +0000 | [diff] [blame] | 356 | if (isVerbose()) { |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 357 | OutStreamer->AddComment("fallthrough-return-value"); |
Dan Gohman | b7c2400 | 2016-05-21 00:21:56 +0000 | [diff] [blame] | 358 | OutStreamer->AddBlankLine(); |
| 359 | } |
| 360 | break; |
| 361 | } |
| 362 | case WebAssembly::FALLTHROUGH_RETURN_VOID: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 363 | case WebAssembly::FALLTHROUGH_RETURN_VOID_S: |
Dan Gohman | b7c2400 | 2016-05-21 00:21:56 +0000 | [diff] [blame] | 364 | // This instruction represents the implicit return at the end of a |
| 365 | // function body with no return value. |
| 366 | if (isVerbose()) { |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 367 | OutStreamer->AddComment("fallthrough-return-void"); |
Dan Gohman | b7c2400 | 2016-05-21 00:21:56 +0000 | [diff] [blame] | 368 | OutStreamer->AddBlankLine(); |
| 369 | } |
| 370 | break; |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 371 | case WebAssembly::EXTRACT_EXCEPTION_I32: |
| 372 | case WebAssembly::EXTRACT_EXCEPTION_I32_S: |
| 373 | // These are pseudo instructions that simulates popping values from stack. |
| 374 | // We print these only when we have -wasm-keep-registers on for assembly |
| 375 | // readability. |
| 376 | if (!WasmKeepRegisters) |
| 377 | break; |
| 378 | LLVM_FALLTHROUGH; |
Dan Gohman | e51c058 | 2015-10-06 00:27:55 +0000 | [diff] [blame] | 379 | default: { |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 380 | WebAssemblyMCInstLower MCInstLowering(OutContext, *this); |
| 381 | MCInst TmpInst; |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 382 | MCInstLowering.lower(MI, TmpInst); |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 383 | EmitToStreamer(*OutStreamer, TmpInst); |
Dan Gohman | e51c058 | 2015-10-06 00:27:55 +0000 | [diff] [blame] | 384 | break; |
| 385 | } |
Dan Gohman | 4f52e00 | 2015-09-09 00:52:47 +0000 | [diff] [blame] | 386 | } |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Dan Gohman | f19ed56 | 2015-11-13 01:42:29 +0000 | [diff] [blame] | 389 | bool WebAssemblyAsmPrinter::PrintAsmOperand(const MachineInstr *MI, |
Nick Desaulniers | 5277b3f | 2019-04-10 16:38:43 +0000 | [diff] [blame] | 390 | unsigned OpNo, |
Dan Gohman | f19ed56 | 2015-11-13 01:42:29 +0000 | [diff] [blame] | 391 | const char *ExtraCode, |
| 392 | raw_ostream &OS) { |
Dan Gohman | 30a42bf | 2015-12-16 17:15:17 +0000 | [diff] [blame] | 393 | // First try the generic code, which knows about modifiers like 'c' and 'n'. |
Nick Desaulniers | 5277b3f | 2019-04-10 16:38:43 +0000 | [diff] [blame] | 394 | if (!AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, OS)) |
Dan Gohman | 30a42bf | 2015-12-16 17:15:17 +0000 | [diff] [blame] | 395 | return false; |
| 396 | |
Dan Gohman | f19ed56 | 2015-11-13 01:42:29 +0000 | [diff] [blame] | 397 | if (!ExtraCode) { |
| 398 | const MachineOperand &MO = MI->getOperand(OpNo); |
Dan Gohman | 30a42bf | 2015-12-16 17:15:17 +0000 | [diff] [blame] | 399 | switch (MO.getType()) { |
| 400 | case MachineOperand::MO_Immediate: |
Dan Gohman | f19ed56 | 2015-11-13 01:42:29 +0000 | [diff] [blame] | 401 | OS << MO.getImm(); |
Dan Gohman | 30a42bf | 2015-12-16 17:15:17 +0000 | [diff] [blame] | 402 | return false; |
| 403 | case MachineOperand::MO_Register: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 404 | // FIXME: only opcode that still contains registers, as required by |
| 405 | // MachineInstr::getDebugVariable(). |
| 406 | assert(MI->getOpcode() == WebAssembly::INLINEASM); |
Dan Gohman | f19ed56 | 2015-11-13 01:42:29 +0000 | [diff] [blame] | 407 | OS << regToString(MO); |
Dan Gohman | 30a42bf | 2015-12-16 17:15:17 +0000 | [diff] [blame] | 408 | return false; |
| 409 | case MachineOperand::MO_GlobalAddress: |
Nick Desaulniers | 7ab164c | 2019-04-26 18:45:04 +0000 | [diff] [blame] | 410 | PrintSymbolOperand(MO, OS); |
Dan Gohman | 30a42bf | 2015-12-16 17:15:17 +0000 | [diff] [blame] | 411 | return false; |
| 412 | case MachineOperand::MO_ExternalSymbol: |
| 413 | GetExternalSymbolSymbol(MO.getSymbolName())->print(OS, MAI); |
| 414 | printOffset(MO.getOffset(), OS); |
| 415 | return false; |
| 416 | case MachineOperand::MO_MachineBasicBlock: |
| 417 | MO.getMBB()->getSymbol()->print(OS, MAI); |
| 418 | return false; |
| 419 | default: |
| 420 | break; |
| 421 | } |
Dan Gohman | f19ed56 | 2015-11-13 01:42:29 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Dan Gohman | 30a42bf | 2015-12-16 17:15:17 +0000 | [diff] [blame] | 424 | return true; |
Dan Gohman | f19ed56 | 2015-11-13 01:42:29 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | bool WebAssemblyAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, |
| 428 | unsigned OpNo, |
Dan Gohman | f19ed56 | 2015-11-13 01:42:29 +0000 | [diff] [blame] | 429 | const char *ExtraCode, |
| 430 | raw_ostream &OS) { |
Dan Gohman | b465aa0 | 2017-11-08 19:18:08 +0000 | [diff] [blame] | 431 | // The current approach to inline asm is that "r" constraints are expressed |
| 432 | // as local indices, rather than values on the operand stack. This simplifies |
| 433 | // using "r" as it eliminates the need to push and pop the values in a |
| 434 | // particular order, however it also makes it impossible to have an "m" |
| 435 | // constraint. So we don't support it. |
Dan Gohman | f19ed56 | 2015-11-13 01:42:29 +0000 | [diff] [blame] | 436 | |
Nick Desaulniers | 5277b3f | 2019-04-10 16:38:43 +0000 | [diff] [blame] | 437 | return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, ExtraCode, OS); |
Dan Gohman | f19ed56 | 2015-11-13 01:42:29 +0000 | [diff] [blame] | 438 | } |
| 439 | |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 440 | // Force static initialization. |
| 441 | extern "C" void LLVMInitializeWebAssemblyAsmPrinter() { |
Mehdi Amini | f42454b | 2016-10-09 23:00:34 +0000 | [diff] [blame] | 442 | RegisterAsmPrinter<WebAssemblyAsmPrinter> X(getTheWebAssemblyTarget32()); |
| 443 | RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(getTheWebAssemblyTarget64()); |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 444 | } |