JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 1 | //===-- WebAssemblyAsmPrinter.cpp - WebAssembly LLVM assembly writer ------===// |
| 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 |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 11 | /// This file contains a printer that converts from our internal |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 12 | /// representation of machine-dependent LLVM code to the WebAssembly assembly |
| 13 | /// language. |
| 14 | /// |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 17 | #include "WebAssemblyAsmPrinter.h" |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 18 | #include "InstPrinter/WebAssemblyInstPrinter.h" |
| 19 | #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" |
Dan Gohman | 3469ee1 | 2016-01-12 20:30:51 +0000 | [diff] [blame] | 20 | #include "MCTargetDesc/WebAssemblyTargetStreamer.h" |
Derek Schuff | c64d765 | 2016-08-01 22:25:02 +0000 | [diff] [blame] | 21 | #include "WebAssembly.h" |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 22 | #include "WebAssemblyMCInstLower.h" |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 23 | #include "WebAssemblyMachineFunctionInfo.h" |
| 24 | #include "WebAssemblyRegisterInfo.h" |
Dan Gohman | e51c058 | 2015-10-06 00:27:55 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
Dan Gohman | 754cd11 | 2015-11-11 01:33:02 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/Analysis.h" |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/AsmPrinter.h" |
JF Bastien | 54be3b1 | 2015-08-25 23:19:49 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineConstantPool.h" |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineInstr.h" |
Dan Gohman | 82607f5 | 2017-02-24 23:46:05 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineModuleInfoImpls.h" |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 31 | #include "llvm/IR/DataLayout.h" |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 32 | #include "llvm/IR/GlobalVariable.h" |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 33 | #include "llvm/MC/MCContext.h" |
Sam Clegg | cfd44a2 | 2018-04-05 17:01:39 +0000 | [diff] [blame] | 34 | #include "llvm/MC/MCSectionWasm.h" |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 35 | #include "llvm/MC/MCStreamer.h" |
JF Bastien | b6091df | 2015-08-25 22:58:05 +0000 | [diff] [blame] | 36 | #include "llvm/MC/MCSymbol.h" |
Sam Clegg | cfd44a2 | 2018-04-05 17:01:39 +0000 | [diff] [blame] | 37 | #include "llvm/MC/MCSymbolWasm.h" |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Debug.h" |
| 39 | #include "llvm/Support/TargetRegistry.h" |
| 40 | #include "llvm/Support/raw_ostream.h" |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 41 | using namespace llvm; |
| 42 | |
| 43 | #define DEBUG_TYPE "asm-printer" |
| 44 | |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 45 | //===----------------------------------------------------------------------===// |
JF Bastien | 45479f6 | 2015-08-26 22:09:54 +0000 | [diff] [blame] | 46 | // Helpers. |
| 47 | //===----------------------------------------------------------------------===// |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 48 | |
Dan Gohman | 53828fd | 2015-11-23 16:50:18 +0000 | [diff] [blame] | 49 | MVT WebAssemblyAsmPrinter::getRegType(unsigned RegNo) const { |
Krzysztof Parzyszek | c8e8e2a | 2017-04-24 19:51:12 +0000 | [diff] [blame] | 50 | const TargetRegisterInfo *TRI = Subtarget->getRegisterInfo(); |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 51 | const TargetRegisterClass *TRC = MRI->getRegClass(RegNo); |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 52 | 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] | 53 | MVT::v4i32, MVT::v2i64, MVT::v4f32, MVT::v2f64}) |
Krzysztof Parzyszek | c8e8e2a | 2017-04-24 19:51:12 +0000 | [diff] [blame] | 54 | if (TRI->isTypeLegalForClass(*TRC, T)) |
Dan Gohman | 53828fd | 2015-11-23 16:50:18 +0000 | [diff] [blame] | 55 | return T; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 56 | LLVM_DEBUG(errs() << "Unknown type for register number: " << RegNo); |
JF Bastien | 1d20a5e | 2015-10-16 00:53:49 +0000 | [diff] [blame] | 57 | llvm_unreachable("Unknown register type"); |
Dan Gohman | 53828fd | 2015-11-23 16:50:18 +0000 | [diff] [blame] | 58 | return MVT::Other; |
JF Bastien | 1d20a5e | 2015-10-16 00:53:49 +0000 | [diff] [blame] | 59 | } |
| 60 | |
JF Bastien | 1d20a5e | 2015-10-16 00:53:49 +0000 | [diff] [blame] | 61 | std::string WebAssemblyAsmPrinter::regToString(const MachineOperand &MO) { |
| 62 | unsigned RegNo = MO.getReg(); |
Dan Gohman | d962527 | 2015-11-20 03:13:31 +0000 | [diff] [blame] | 63 | assert(TargetRegisterInfo::isVirtualRegister(RegNo) && |
| 64 | "Unlowered physical register encountered during assembly printing"); |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 65 | assert(!MFI->isVRegStackified(RegNo)); |
Dan Gohman | 058fce5 | 2015-11-13 00:21:05 +0000 | [diff] [blame] | 66 | unsigned WAReg = MFI->getWAReg(RegNo); |
| 67 | assert(WAReg != WebAssemblyFunctionInfo::UnusedReg); |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 68 | return '$' + utostr(WAReg); |
Dan Gohman | e51c058 | 2015-10-06 00:27:55 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Dan Gohman | ec977b0 | 2016-01-25 15:12:05 +0000 | [diff] [blame] | 71 | WebAssemblyTargetStreamer *WebAssemblyAsmPrinter::getTargetStreamer() { |
Dan Gohman | 3469ee1 | 2016-01-12 20:30:51 +0000 | [diff] [blame] | 72 | MCTargetStreamer *TS = OutStreamer->getTargetStreamer(); |
| 73 | return static_cast<WebAssemblyTargetStreamer *>(TS); |
JF Bastien | 73ff6af | 2015-08-31 22:24:11 +0000 | [diff] [blame] | 74 | } |
| 75 | |
JF Bastien | 45479f6 | 2015-08-26 22:09:54 +0000 | [diff] [blame] | 76 | //===----------------------------------------------------------------------===// |
| 77 | // WebAssemblyAsmPrinter Implementation. |
| 78 | //===----------------------------------------------------------------------===// |
Derek Schuff | 46e3316 | 2015-11-16 21:12:41 +0000 | [diff] [blame] | 79 | |
Derek Schuff | 5859a9ed | 2016-06-03 18:34:36 +0000 | [diff] [blame] | 80 | void WebAssemblyAsmPrinter::EmitEndOfAsmFile(Module &M) { |
Wouter van Oortmerssen | 3231e51 | 2018-11-02 00:45:00 +0000 | [diff] [blame] | 81 | for (auto &It : OutContext.getSymbols()) { |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 82 | // Emit a .globaltype and .eventtype declaration. |
Wouter van Oortmerssen | 3231e51 | 2018-11-02 00:45:00 +0000 | [diff] [blame] | 83 | auto Sym = cast<MCSymbolWasm>(It.getValue()); |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 84 | if (Sym->getType() == wasm::WASM_SYMBOL_TYPE_GLOBAL) |
Wouter van Oortmerssen | 3231e51 | 2018-11-02 00:45:00 +0000 | [diff] [blame] | 85 | getTargetStreamer()->emitGlobalType(Sym); |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 86 | else if (Sym->getType() == wasm::WASM_SYMBOL_TYPE_EVENT) |
| 87 | getTargetStreamer()->emitEventType(Sym); |
Wouter van Oortmerssen | 3231e51 | 2018-11-02 00:45:00 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Derek Schuff | 5859a9ed | 2016-06-03 18:34:36 +0000 | [diff] [blame] | 90 | for (const auto &F : M) { |
| 91 | // Emit function type info for all undefined functions |
| 92 | if (F.isDeclarationForLinker() && !F.isIntrinsic()) { |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 93 | SmallVector<MVT, 4> Results; |
| 94 | SmallVector<MVT, 4> Params; |
Derek Schuff | 77a7a38 | 2018-10-03 22:22:48 +0000 | [diff] [blame] | 95 | ComputeSignatureVTs(F.getFunctionType(), F, TM, Params, Results); |
| 96 | auto *Sym = cast<MCSymbolWasm>(getSymbol(&F)); |
| 97 | if (!Sym->getSignature()) { |
| 98 | auto Signature = SignatureFromMVTs(Results, Params); |
| 99 | Sym->setSignature(Signature.get()); |
| 100 | addSignature(std::move(Signature)); |
| 101 | } |
| 102 | // FIXME: this was originally intended for post-linking and was only used |
| 103 | // for imports that were only called indirectly (i.e. s2wasm could not |
| 104 | // infer the type from a call). With object files it applies to all |
| 105 | // imports. so fix the names and the tests, or rethink how import |
| 106 | // delcarations work in asm files. |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame^] | 107 | getTargetStreamer()->emitFunctionType(Sym); |
Dan Gohman | db1916a | 2018-02-09 23:13:22 +0000 | [diff] [blame] | 108 | |
Jacob Gravelle | ca358da | 2018-02-12 21:41:12 +0000 | [diff] [blame] | 109 | if (TM.getTargetTriple().isOSBinFormatWasm() && |
| 110 | F.hasFnAttribute("wasm-import-module")) { |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 111 | StringRef Name = |
| 112 | F.getFnAttribute("wasm-import-module").getValueAsString(); |
Derek Schuff | 77a7a38 | 2018-10-03 22:22:48 +0000 | [diff] [blame] | 113 | getTargetStreamer()->emitImportModule(Sym, Name); |
Dan Gohman | db1916a | 2018-02-09 23:13:22 +0000 | [diff] [blame] | 114 | } |
Derek Schuff | 5859a9ed | 2016-06-03 18:34:36 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
Wouter van Oortmerssen | 3231e51 | 2018-11-02 00:45:00 +0000 | [diff] [blame] | 117 | |
Derek Schuff | 7747d703e | 2016-12-01 00:11:15 +0000 | [diff] [blame] | 118 | for (const auto &G : M.globals()) { |
| 119 | if (!G.hasInitializer() && G.hasExternalLinkage()) { |
Dan Gohman | 5cf6473 | 2017-12-07 00:14:30 +0000 | [diff] [blame] | 120 | if (G.getValueType()->isSized()) { |
| 121 | uint16_t Size = M.getDataLayout().getTypeAllocSize(G.getValueType()); |
Dan Gohman | 5cf6473 | 2017-12-07 00:14:30 +0000 | [diff] [blame] | 122 | OutStreamer->emitELFSize(getSymbol(&G), |
| 123 | MCConstantExpr::create(Size, OutContext)); |
| 124 | } |
Derek Schuff | 7747d703e | 2016-12-01 00:11:15 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
Sam Clegg | cfd44a2 | 2018-04-05 17:01:39 +0000 | [diff] [blame] | 127 | |
| 128 | if (const NamedMDNode *Named = M.getNamedMetadata("wasm.custom_sections")) { |
| 129 | for (const Metadata *MD : Named->operands()) { |
| 130 | const MDTuple *Tuple = dyn_cast<MDTuple>(MD); |
| 131 | if (!Tuple || Tuple->getNumOperands() != 2) |
| 132 | continue; |
| 133 | const MDString *Name = dyn_cast<MDString>(Tuple->getOperand(0)); |
| 134 | const MDString *Contents = dyn_cast<MDString>(Tuple->getOperand(1)); |
| 135 | if (!Name || !Contents) |
| 136 | continue; |
| 137 | |
| 138 | OutStreamer->PushSection(); |
| 139 | std::string SectionName = (".custom_section." + Name->getString()).str(); |
| 140 | MCSectionWasm *mySection = |
| 141 | OutContext.getWasmSection(SectionName, SectionKind::getMetadata()); |
| 142 | OutStreamer->SwitchSection(mySection); |
| 143 | OutStreamer->EmitBytes(Contents->getString()); |
| 144 | OutStreamer->PopSection(); |
| 145 | } |
| 146 | } |
Derek Schuff | 5859a9ed | 2016-06-03 18:34:36 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void WebAssemblyAsmPrinter::EmitConstantPool() { |
| 150 | assert(MF->getConstantPool()->getConstants().empty() && |
| 151 | "WebAssembly disables constant pools"); |
| 152 | } |
| 153 | |
| 154 | void WebAssemblyAsmPrinter::EmitJumpTableInfo() { |
| 155 | // Nothing to do; jump tables are incorporated into the instruction stream. |
| 156 | } |
| 157 | |
JF Bastien | b6091df | 2015-08-25 22:58:05 +0000 | [diff] [blame] | 158 | void WebAssemblyAsmPrinter::EmitFunctionBodyStart() { |
David Blaikie | 2110924 | 2017-12-15 23:52:06 +0000 | [diff] [blame] | 159 | const Function &F = MF->getFunction(); |
Derek Schuff | 77a7a38 | 2018-10-03 22:22:48 +0000 | [diff] [blame] | 160 | SmallVector<MVT, 1> ResultVTs; |
| 161 | SmallVector<MVT, 4> ParamVTs; |
| 162 | ComputeSignatureVTs(F.getFunctionType(), F, TM, ParamVTs, ResultVTs); |
| 163 | auto Signature = SignatureFromMVTs(ResultVTs, ParamVTs); |
| 164 | auto *WasmSym = cast<MCSymbolWasm>(CurrentFnSym); |
| 165 | WasmSym->setSignature(Signature.get()); |
| 166 | addSignature(std::move(Signature)); |
| 167 | |
| 168 | // FIXME: clean up how params and results are emitted (use signatures) |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame^] | 169 | getTargetStreamer()->emitFunctionType(WasmSym); |
Derek Schuff | c64d765 | 2016-08-01 22:25:02 +0000 | [diff] [blame] | 170 | |
| 171 | // Emit the function index. |
| 172 | if (MDNode *Idx = F.getMetadata("wasm.index")) { |
| 173 | assert(Idx->getNumOperands() == 1); |
| 174 | |
| 175 | getTargetStreamer()->emitIndIdx(AsmPrinter::lowerConstant( |
| 176 | cast<ConstantAsMetadata>(Idx->getOperand(0))->getValue())); |
| 177 | } |
| 178 | |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame^] | 179 | SmallVector<wasm::ValType, 16> Locals; |
| 180 | ValTypesFromMVTs(MFI->getLocals(), Locals); |
| 181 | getTargetStreamer()->emitLocal(Locals); |
JF Bastien | 1d20a5e | 2015-10-16 00:53:49 +0000 | [diff] [blame] | 182 | |
Dan Gohman | e51c058 | 2015-10-06 00:27:55 +0000 | [diff] [blame] | 183 | AsmPrinter::EmitFunctionBodyStart(); |
JF Bastien | b6091df | 2015-08-25 22:58:05 +0000 | [diff] [blame] | 184 | } |
| 185 | |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 186 | void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 187 | LLVM_DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n'); |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 188 | |
Dan Gohman | e51c058 | 2015-10-06 00:27:55 +0000 | [diff] [blame] | 189 | switch (MI->getOpcode()) { |
Thomas Lively | 0ff82ac | 2018-10-13 07:09:10 +0000 | [diff] [blame] | 190 | case WebAssembly::ARGUMENT_i32: |
| 191 | case WebAssembly::ARGUMENT_i32_S: |
| 192 | case WebAssembly::ARGUMENT_i64: |
| 193 | case WebAssembly::ARGUMENT_i64_S: |
| 194 | case WebAssembly::ARGUMENT_f32: |
| 195 | case WebAssembly::ARGUMENT_f32_S: |
| 196 | case WebAssembly::ARGUMENT_f64: |
| 197 | case WebAssembly::ARGUMENT_f64_S: |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 198 | case WebAssembly::ARGUMENT_v16i8: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 199 | case WebAssembly::ARGUMENT_v16i8_S: |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 200 | case WebAssembly::ARGUMENT_v8i16: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 201 | case WebAssembly::ARGUMENT_v8i16_S: |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 202 | case WebAssembly::ARGUMENT_v4i32: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 203 | case WebAssembly::ARGUMENT_v4i32_S: |
Derek Schuff | 51ed131 | 2018-08-07 21:24:01 +0000 | [diff] [blame] | 204 | case WebAssembly::ARGUMENT_v2i64: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 205 | case WebAssembly::ARGUMENT_v2i64_S: |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 206 | case WebAssembly::ARGUMENT_v4f32: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 207 | case WebAssembly::ARGUMENT_v4f32_S: |
Derek Schuff | 51ed131 | 2018-08-07 21:24:01 +0000 | [diff] [blame] | 208 | case WebAssembly::ARGUMENT_v2f64: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 209 | case WebAssembly::ARGUMENT_v2f64_S: |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 210 | // These represent values which are live into the function entry, so there's |
| 211 | // no instruction to emit. |
Dan Gohman | e51c058 | 2015-10-06 00:27:55 +0000 | [diff] [blame] | 212 | break; |
Dan Gohman | b7c2400 | 2016-05-21 00:21:56 +0000 | [diff] [blame] | 213 | case WebAssembly::FALLTHROUGH_RETURN_I32: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 214 | case WebAssembly::FALLTHROUGH_RETURN_I32_S: |
Dan Gohman | b7c2400 | 2016-05-21 00:21:56 +0000 | [diff] [blame] | 215 | case WebAssembly::FALLTHROUGH_RETURN_I64: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 216 | case WebAssembly::FALLTHROUGH_RETURN_I64_S: |
Dan Gohman | b7c2400 | 2016-05-21 00:21:56 +0000 | [diff] [blame] | 217 | case WebAssembly::FALLTHROUGH_RETURN_F32: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 218 | case WebAssembly::FALLTHROUGH_RETURN_F32_S: |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 219 | case WebAssembly::FALLTHROUGH_RETURN_F64: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 220 | case WebAssembly::FALLTHROUGH_RETURN_F64_S: |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 221 | case WebAssembly::FALLTHROUGH_RETURN_v16i8: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 222 | case WebAssembly::FALLTHROUGH_RETURN_v16i8_S: |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 223 | case WebAssembly::FALLTHROUGH_RETURN_v8i16: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 224 | case WebAssembly::FALLTHROUGH_RETURN_v8i16_S: |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 225 | case WebAssembly::FALLTHROUGH_RETURN_v4i32: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 226 | case WebAssembly::FALLTHROUGH_RETURN_v4i32_S: |
Derek Schuff | 51ed131 | 2018-08-07 21:24:01 +0000 | [diff] [blame] | 227 | case WebAssembly::FALLTHROUGH_RETURN_v2i64: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 228 | case WebAssembly::FALLTHROUGH_RETURN_v2i64_S: |
Derek Schuff | 51ed131 | 2018-08-07 21:24:01 +0000 | [diff] [blame] | 229 | case WebAssembly::FALLTHROUGH_RETURN_v4f32: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 230 | case WebAssembly::FALLTHROUGH_RETURN_v4f32_S: |
| 231 | case WebAssembly::FALLTHROUGH_RETURN_v2f64: |
| 232 | case WebAssembly::FALLTHROUGH_RETURN_v2f64_S: { |
Dan Gohman | b7c2400 | 2016-05-21 00:21:56 +0000 | [diff] [blame] | 233 | // These instructions represent the implicit return at the end of a |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 234 | // function body. Always pops one value off the stack. |
Dan Gohman | b7c2400 | 2016-05-21 00:21:56 +0000 | [diff] [blame] | 235 | if (isVerbose()) { |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 236 | OutStreamer->AddComment("fallthrough-return-value"); |
Dan Gohman | b7c2400 | 2016-05-21 00:21:56 +0000 | [diff] [blame] | 237 | OutStreamer->AddBlankLine(); |
| 238 | } |
| 239 | break; |
| 240 | } |
| 241 | case WebAssembly::FALLTHROUGH_RETURN_VOID: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 242 | case WebAssembly::FALLTHROUGH_RETURN_VOID_S: |
Dan Gohman | b7c2400 | 2016-05-21 00:21:56 +0000 | [diff] [blame] | 243 | // This instruction represents the implicit return at the end of a |
| 244 | // function body with no return value. |
| 245 | if (isVerbose()) { |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 246 | OutStreamer->AddComment("fallthrough-return-void"); |
Dan Gohman | b7c2400 | 2016-05-21 00:21:56 +0000 | [diff] [blame] | 247 | OutStreamer->AddBlankLine(); |
| 248 | } |
| 249 | break; |
Dan Gohman | e51c058 | 2015-10-06 00:27:55 +0000 | [diff] [blame] | 250 | default: { |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 251 | WebAssemblyMCInstLower MCInstLowering(OutContext, *this); |
| 252 | MCInst TmpInst; |
| 253 | MCInstLowering.Lower(MI, TmpInst); |
| 254 | EmitToStreamer(*OutStreamer, TmpInst); |
Dan Gohman | e51c058 | 2015-10-06 00:27:55 +0000 | [diff] [blame] | 255 | break; |
| 256 | } |
Dan Gohman | 4f52e00 | 2015-09-09 00:52:47 +0000 | [diff] [blame] | 257 | } |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 260 | const MCExpr *WebAssemblyAsmPrinter::lowerConstant(const Constant *CV) { |
| 261 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) |
Sam Clegg | 7736855d | 2017-06-13 01:42:21 +0000 | [diff] [blame] | 262 | if (GV->getValueType()->isFunctionTy()) { |
Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 263 | return MCSymbolRefExpr::create( |
Sam Clegg | 9bf73c0 | 2017-07-05 20:25:08 +0000 | [diff] [blame] | 264 | getSymbol(GV), MCSymbolRefExpr::VK_WebAssembly_FUNCTION, OutContext); |
Sam Clegg | 7736855d | 2017-06-13 01:42:21 +0000 | [diff] [blame] | 265 | } |
Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 266 | return AsmPrinter::lowerConstant(CV); |
| 267 | } |
| 268 | |
Dan Gohman | f19ed56 | 2015-11-13 01:42:29 +0000 | [diff] [blame] | 269 | bool WebAssemblyAsmPrinter::PrintAsmOperand(const MachineInstr *MI, |
| 270 | unsigned OpNo, unsigned AsmVariant, |
| 271 | const char *ExtraCode, |
| 272 | raw_ostream &OS) { |
| 273 | if (AsmVariant != 0) |
| 274 | report_fatal_error("There are no defined alternate asm variants"); |
| 275 | |
Dan Gohman | 30a42bf | 2015-12-16 17:15:17 +0000 | [diff] [blame] | 276 | // First try the generic code, which knows about modifiers like 'c' and 'n'. |
| 277 | if (!AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS)) |
| 278 | return false; |
| 279 | |
Dan Gohman | f19ed56 | 2015-11-13 01:42:29 +0000 | [diff] [blame] | 280 | if (!ExtraCode) { |
| 281 | const MachineOperand &MO = MI->getOperand(OpNo); |
Dan Gohman | 30a42bf | 2015-12-16 17:15:17 +0000 | [diff] [blame] | 282 | switch (MO.getType()) { |
| 283 | case MachineOperand::MO_Immediate: |
Dan Gohman | f19ed56 | 2015-11-13 01:42:29 +0000 | [diff] [blame] | 284 | OS << MO.getImm(); |
Dan Gohman | 30a42bf | 2015-12-16 17:15:17 +0000 | [diff] [blame] | 285 | return false; |
| 286 | case MachineOperand::MO_Register: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 287 | // FIXME: only opcode that still contains registers, as required by |
| 288 | // MachineInstr::getDebugVariable(). |
| 289 | assert(MI->getOpcode() == WebAssembly::INLINEASM); |
Dan Gohman | f19ed56 | 2015-11-13 01:42:29 +0000 | [diff] [blame] | 290 | OS << regToString(MO); |
Dan Gohman | 30a42bf | 2015-12-16 17:15:17 +0000 | [diff] [blame] | 291 | return false; |
| 292 | case MachineOperand::MO_GlobalAddress: |
| 293 | getSymbol(MO.getGlobal())->print(OS, MAI); |
| 294 | printOffset(MO.getOffset(), OS); |
| 295 | return false; |
| 296 | case MachineOperand::MO_ExternalSymbol: |
| 297 | GetExternalSymbolSymbol(MO.getSymbolName())->print(OS, MAI); |
| 298 | printOffset(MO.getOffset(), OS); |
| 299 | return false; |
| 300 | case MachineOperand::MO_MachineBasicBlock: |
| 301 | MO.getMBB()->getSymbol()->print(OS, MAI); |
| 302 | return false; |
| 303 | default: |
| 304 | break; |
| 305 | } |
Dan Gohman | f19ed56 | 2015-11-13 01:42:29 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Dan Gohman | 30a42bf | 2015-12-16 17:15:17 +0000 | [diff] [blame] | 308 | return true; |
Dan Gohman | f19ed56 | 2015-11-13 01:42:29 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | bool WebAssemblyAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, |
| 312 | unsigned OpNo, |
| 313 | unsigned AsmVariant, |
| 314 | const char *ExtraCode, |
| 315 | raw_ostream &OS) { |
| 316 | if (AsmVariant != 0) |
| 317 | report_fatal_error("There are no defined alternate asm variants"); |
| 318 | |
Dan Gohman | b465aa0 | 2017-11-08 19:18:08 +0000 | [diff] [blame] | 319 | // The current approach to inline asm is that "r" constraints are expressed |
| 320 | // as local indices, rather than values on the operand stack. This simplifies |
| 321 | // using "r" as it eliminates the need to push and pop the values in a |
| 322 | // particular order, however it also makes it impossible to have an "m" |
| 323 | // constraint. So we don't support it. |
Dan Gohman | f19ed56 | 2015-11-13 01:42:29 +0000 | [diff] [blame] | 324 | |
| 325 | return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, AsmVariant, ExtraCode, OS); |
| 326 | } |
| 327 | |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 328 | // Force static initialization. |
| 329 | extern "C" void LLVMInitializeWebAssemblyAsmPrinter() { |
Mehdi Amini | f42454b | 2016-10-09 23:00:34 +0000 | [diff] [blame] | 330 | RegisterAsmPrinter<WebAssemblyAsmPrinter> X(getTheWebAssemblyTarget32()); |
| 331 | RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(getTheWebAssemblyTarget64()); |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 332 | } |