1 //==-- WebAssemblyTargetStreamer.cpp - WebAssembly Target Streamer Methods --=// 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 /// This file defines WebAssembly-specific target streamer classes. 12 /// These are for implementing support for target-specific assembly directives. 13 /// 14 //===----------------------------------------------------------------------===// 15 16 #include "WebAssemblyTargetStreamer.h" 17 #include "InstPrinter/WebAssemblyInstPrinter.h" 18 #include "WebAssemblyMCTargetDesc.h" 19 #include "llvm/MC/MCContext.h" 20 #include "llvm/MC/MCSectionWasm.h" 21 #include "llvm/MC/MCSubtargetInfo.h" 22 #include "llvm/MC/MCSymbolWasm.h" 23 #include "llvm/Support/Casting.h" 24 #include "llvm/Support/ErrorHandling.h" 25 #include "llvm/Support/FormattedStream.h" 26 using namespace llvm; 27 28 WebAssemblyTargetStreamer::WebAssemblyTargetStreamer(MCStreamer &S) 29 : MCTargetStreamer(S) {} 30 31 void WebAssemblyTargetStreamer::emitValueType(wasm::ValType Type) { 32 Streamer.EmitIntValue(uint8_t(Type), 1); 33 } 34 35 WebAssemblyTargetAsmStreamer::WebAssemblyTargetAsmStreamer( 36 MCStreamer &S, formatted_raw_ostream &OS) 37 : WebAssemblyTargetStreamer(S), OS(OS) {} 38 39 WebAssemblyTargetWasmStreamer::WebAssemblyTargetWasmStreamer(MCStreamer &S) 40 : WebAssemblyTargetStreamer(S) {} 41 42 static void printTypes(formatted_raw_ostream &OS, 43 ArrayRef<wasm::ValType> Types) { 44 bool First = true; 45 for (auto Type : Types) { 46 if (First) 47 First = false; 48 else 49 OS << ", "; 50 OS << WebAssembly::typeToString(Type); 51 } 52 OS << '\n'; 53 } 54 55 void WebAssemblyTargetAsmStreamer::emitLocal(ArrayRef<wasm::ValType> Types) { 56 if (!Types.empty()) { 57 OS << "\t.local \t"; 58 printTypes(OS, Types); 59 } 60 } 61 62 void WebAssemblyTargetAsmStreamer::emitEndFunc() { OS << "\t.endfunc\n"; } 63 64 void WebAssemblyTargetAsmStreamer::emitSignature( 65 const wasm::WasmSignature *Sig) { 66 OS << "("; 67 emitParamList(Sig); 68 OS << ") -> ("; 69 emitReturnList(Sig); 70 OS << ")"; 71 } 72 73 void WebAssemblyTargetAsmStreamer::emitParamList( 74 const wasm::WasmSignature *Sig) { 75 auto &Params = Sig->Params; 76 for (auto &Ty : Params) { 77 if (&Ty != &Params[0]) 78 OS << ", "; 79 OS << WebAssembly::typeToString(Ty); 80 } 81 } 82 83 void WebAssemblyTargetAsmStreamer::emitReturnList( 84 const wasm::WasmSignature *Sig) { 85 auto &Returns = Sig->Returns; 86 for (auto &Ty : Returns) { 87 if (&Ty != &Returns[0]) 88 OS << ", "; 89 OS << WebAssembly::typeToString(Ty); 90 } 91 } 92 93 void WebAssemblyTargetAsmStreamer::emitFunctionType(const MCSymbolWasm *Sym) { 94 assert(Sym->isFunction()); 95 OS << "\t.functype\t" << Sym->getName() << " "; 96 emitSignature(Sym->getSignature()); 97 OS << "\n"; 98 } 99 100 void WebAssemblyTargetAsmStreamer::emitGlobalType(const MCSymbolWasm *Sym) { 101 assert(Sym->isGlobal()); 102 OS << "\t.globaltype\t" << Sym->getName() << ", " 103 << WebAssembly::typeToString( 104 static_cast<wasm::ValType>(Sym->getGlobalType().Type)) 105 << '\n'; 106 } 107 108 void WebAssemblyTargetAsmStreamer::emitEventType(const MCSymbolWasm *Sym) { 109 assert(Sym->isEvent()); 110 OS << "\t.eventtype\t" << Sym->getName() << " "; 111 emitParamList(Sym->getSignature()); 112 OS << "\n"; 113 } 114 115 void WebAssemblyTargetAsmStreamer::emitImportModule(const MCSymbolWasm *Sym, 116 StringRef ModuleName) { 117 OS << "\t.import_module\t" << Sym->getName() << ", " << ModuleName << '\n'; 118 } 119 120 void WebAssemblyTargetAsmStreamer::emitIndIdx(const MCExpr *Value) { 121 OS << "\t.indidx \t" << *Value << '\n'; 122 } 123 124 void WebAssemblyTargetWasmStreamer::emitLocal(ArrayRef<wasm::ValType> Types) { 125 SmallVector<std::pair<wasm::ValType, uint32_t>, 4> Grouped; 126 for (auto Type : Types) { 127 if (Grouped.empty() || Grouped.back().first != Type) 128 Grouped.push_back(std::make_pair(Type, 1)); 129 else 130 ++Grouped.back().second; 131 } 132 133 Streamer.EmitULEB128IntValue(Grouped.size()); 134 for (auto Pair : Grouped) { 135 Streamer.EmitULEB128IntValue(Pair.second); 136 emitValueType(Pair.first); 137 } 138 } 139 140 void WebAssemblyTargetWasmStreamer::emitEndFunc() { 141 llvm_unreachable(".end_func is not needed for direct wasm output"); 142 } 143 144 void WebAssemblyTargetWasmStreamer::emitIndIdx(const MCExpr *Value) { 145 llvm_unreachable(".indidx encoding not yet implemented"); 146 } 147