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 /// \brief 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/MCSectionELF.h" 21 #include "llvm/MC/MCSectionWasm.h" 22 #include "llvm/MC/MCSubtargetInfo.h" 23 #include "llvm/MC/MCSymbolELF.h" 24 #include "llvm/MC/MCSymbolWasm.h" 25 #include "llvm/Support/Casting.h" 26 #include "llvm/Support/ErrorHandling.h" 27 #include "llvm/Support/FormattedStream.h" 28 using namespace llvm; 29 30 WebAssemblyTargetStreamer::WebAssemblyTargetStreamer(MCStreamer &S) 31 : MCTargetStreamer(S) {} 32 33 void WebAssemblyTargetStreamer::emitValueType(wasm::ValType Type) { 34 Streamer.EmitSLEB128IntValue(int32_t(Type)); 35 } 36 37 WebAssemblyTargetAsmStreamer::WebAssemblyTargetAsmStreamer( 38 MCStreamer &S, formatted_raw_ostream &OS) 39 : WebAssemblyTargetStreamer(S), OS(OS) {} 40 41 WebAssemblyTargetELFStreamer::WebAssemblyTargetELFStreamer(MCStreamer &S) 42 : WebAssemblyTargetStreamer(S) {} 43 44 WebAssemblyTargetWasmStreamer::WebAssemblyTargetWasmStreamer(MCStreamer &S) 45 : WebAssemblyTargetStreamer(S) {} 46 47 static void PrintTypes(formatted_raw_ostream &OS, ArrayRef<MVT> Types) { 48 bool First = true; 49 for (MVT Type : Types) { 50 if (First) 51 First = false; 52 else 53 OS << ", "; 54 OS << WebAssembly::TypeToString(Type); 55 } 56 OS << '\n'; 57 } 58 59 void WebAssemblyTargetAsmStreamer::emitParam(MCSymbol *Symbol, 60 ArrayRef<MVT> Types) { 61 if (!Types.empty()) { 62 OS << "\t.param \t"; 63 64 // FIXME: Currently this applies to the "current" function; it may 65 // be cleaner to specify an explicit symbol as part of the directive. 66 67 PrintTypes(OS, Types); 68 } 69 } 70 71 void WebAssemblyTargetAsmStreamer::emitResult(MCSymbol *Symbol, 72 ArrayRef<MVT> Types) { 73 if (!Types.empty()) { 74 OS << "\t.result \t"; 75 76 // FIXME: Currently this applies to the "current" function; it may 77 // be cleaner to specify an explicit symbol as part of the directive. 78 79 PrintTypes(OS, Types); 80 } 81 } 82 83 void WebAssemblyTargetAsmStreamer::emitLocal(ArrayRef<MVT> Types) { 84 if (!Types.empty()) { 85 OS << "\t.local \t"; 86 PrintTypes(OS, Types); 87 } 88 } 89 90 void WebAssemblyTargetAsmStreamer::emitGlobal( 91 ArrayRef<wasm::Global> Globals) { 92 if (!Globals.empty()) { 93 OS << "\t.globalvar \t"; 94 95 bool First = true; 96 for (const wasm::Global &G : Globals) { 97 if (First) 98 First = false; 99 else 100 OS << ", "; 101 OS << WebAssembly::TypeToString(G.Type); 102 if (!G.InitialModule.empty()) 103 OS << '=' << G.InitialModule << ':' << G.InitialName; 104 else 105 OS << '=' << G.InitialValue; 106 } 107 OS << '\n'; 108 } 109 } 110 111 void WebAssemblyTargetAsmStreamer::emitEndFunc() { OS << "\t.endfunc\n"; } 112 113 void WebAssemblyTargetAsmStreamer::emitIndirectFunctionType( 114 MCSymbol *Symbol, SmallVectorImpl<MVT> &Params, SmallVectorImpl<MVT> &Results) { 115 OS << "\t.functype\t" << Symbol->getName(); 116 if (Results.empty()) 117 OS << ", void"; 118 else { 119 assert(Results.size() == 1); 120 OS << ", " << WebAssembly::TypeToString(Results.front()); 121 } 122 for (auto Ty : Params) 123 OS << ", " << WebAssembly::TypeToString(Ty); 124 OS << '\n'; 125 } 126 127 void WebAssemblyTargetAsmStreamer::emitGlobalImport(StringRef name) { 128 OS << "\t.import_global\t" << name << '\n'; 129 } 130 131 void WebAssemblyTargetAsmStreamer::emitIndIdx(const MCExpr *Value) { 132 OS << "\t.indidx \t" << *Value << '\n'; 133 } 134 135 void WebAssemblyTargetELFStreamer::emitParam(MCSymbol *Symbol, 136 ArrayRef<MVT> Types) { 137 // Nothing to emit; params are declared as part of the function signature. 138 } 139 140 void WebAssemblyTargetELFStreamer::emitResult(MCSymbol *Symbol, 141 ArrayRef<MVT> Types) { 142 // Nothing to emit; results are declared as part of the function signature. 143 } 144 145 void WebAssemblyTargetELFStreamer::emitLocal(ArrayRef<MVT> Types) { 146 Streamer.EmitULEB128IntValue(Types.size()); 147 for (MVT Type : Types) 148 emitValueType(WebAssembly::toValType(Type)); 149 } 150 151 void WebAssemblyTargetELFStreamer::emitGlobal( 152 ArrayRef<wasm::Global> Globals) { 153 llvm_unreachable(".globalvar encoding not yet implemented"); 154 } 155 156 void WebAssemblyTargetELFStreamer::emitEndFunc() { 157 Streamer.EmitIntValue(WebAssembly::End, 1); 158 } 159 160 void WebAssemblyTargetELFStreamer::emitIndIdx(const MCExpr *Value) { 161 llvm_unreachable(".indidx encoding not yet implemented"); 162 } 163 164 void WebAssemblyTargetELFStreamer::emitIndirectFunctionType( 165 MCSymbol *Symbol, SmallVectorImpl<MVT> &Params, SmallVectorImpl<MVT> &Results) { 166 // Nothing to emit here. TODO: Re-design how linking works and re-evaluate 167 // whether it's necessary for .o files to declare indirect function types. 168 } 169 170 void WebAssemblyTargetELFStreamer::emitGlobalImport(StringRef name) { 171 } 172 173 void WebAssemblyTargetWasmStreamer::emitParam(MCSymbol *Symbol, 174 ArrayRef<MVT> Types) { 175 SmallVector<wasm::ValType, 4> Params; 176 for (MVT Ty : Types) 177 Params.push_back(WebAssembly::toValType(Ty)); 178 179 cast<MCSymbolWasm>(Symbol)->setParams(std::move(Params)); 180 } 181 182 void WebAssemblyTargetWasmStreamer::emitResult(MCSymbol *Symbol, 183 ArrayRef<MVT> Types) { 184 SmallVector<wasm::ValType, 4> Returns; 185 for (MVT Ty : Types) 186 Returns.push_back(WebAssembly::toValType(Ty)); 187 188 cast<MCSymbolWasm>(Symbol)->setReturns(std::move(Returns)); 189 } 190 191 void WebAssemblyTargetWasmStreamer::emitLocal(ArrayRef<MVT> Types) { 192 SmallVector<std::pair<MVT, uint32_t>, 4> Grouped; 193 for (MVT Type : Types) { 194 if (Grouped.empty() || Grouped.back().first != Type) 195 Grouped.push_back(std::make_pair(Type, 1)); 196 else 197 ++Grouped.back().second; 198 } 199 200 Streamer.EmitULEB128IntValue(Grouped.size()); 201 for (auto Pair : Grouped) { 202 Streamer.EmitULEB128IntValue(Pair.second); 203 emitValueType(WebAssembly::toValType(Pair.first)); 204 } 205 } 206 207 void WebAssemblyTargetWasmStreamer::emitGlobal( 208 ArrayRef<wasm::Global> Globals) { 209 // Encode the globals use by the funciton into the special .global_variables 210 // section. This will later be decoded and turned into contents for the 211 // Globals Section. 212 Streamer.PushSection(); 213 Streamer.SwitchSection(Streamer.getContext().getWasmSection( 214 ".global_variables", SectionKind::getMetadata())); 215 for (const wasm::Global &G : Globals) { 216 Streamer.EmitIntValue(int32_t(G.Type), 1); 217 Streamer.EmitIntValue(G.Mutable, 1); 218 if (G.InitialModule.empty()) { 219 Streamer.EmitIntValue(0, 1); // indicate that we have an int value 220 Streamer.EmitSLEB128IntValue(0); 221 } else { 222 Streamer.EmitIntValue(1, 1); // indicate that we have a module import 223 Streamer.EmitBytes(G.InitialModule); 224 Streamer.EmitIntValue(0, 1); // nul-terminate 225 Streamer.EmitBytes(G.InitialName); 226 Streamer.EmitIntValue(0, 1); // nul-terminate 227 } 228 } 229 Streamer.PopSection(); 230 } 231 232 void WebAssemblyTargetWasmStreamer::emitEndFunc() { 233 llvm_unreachable(".end_func is not needed for direct wasm output"); 234 } 235 236 void WebAssemblyTargetWasmStreamer::emitIndIdx(const MCExpr *Value) { 237 llvm_unreachable(".indidx encoding not yet implemented"); 238 } 239 240 void WebAssemblyTargetWasmStreamer::emitIndirectFunctionType( 241 MCSymbol *Symbol, SmallVectorImpl<MVT> &Params, 242 SmallVectorImpl<MVT> &Results) { 243 MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Symbol); 244 if (WasmSym->isFunction()) { 245 // Symbol already has its arguments and result set. 246 return; 247 } 248 249 SmallVector<wasm::ValType, 4> ValParams; 250 for (MVT Ty : Params) 251 ValParams.push_back(WebAssembly::toValType(Ty)); 252 253 SmallVector<wasm::ValType, 1> ValResults; 254 for (MVT Ty : Results) 255 ValResults.push_back(WebAssembly::toValType(Ty)); 256 257 WasmSym->setParams(std::move(ValParams)); 258 WasmSym->setReturns(std::move(ValResults)); 259 WasmSym->setIsFunction(true); 260 } 261 262 void WebAssemblyTargetWasmStreamer::emitGlobalImport(StringRef name) { 263 llvm_unreachable(".global_import is not needed for direct wasm output"); 264 } 265