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/MCSubtargetInfo.h"
22 #include "llvm/MC/MCSymbolELF.h"
23 #include "llvm/Support/ELF.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/FormattedStream.h"
26 using namespace llvm;
27
WebAssemblyTargetStreamer(MCStreamer & S)28 WebAssemblyTargetStreamer::WebAssemblyTargetStreamer(MCStreamer &S)
29 : MCTargetStreamer(S) {}
30
WebAssemblyTargetAsmStreamer(MCStreamer & S,formatted_raw_ostream & OS)31 WebAssemblyTargetAsmStreamer::WebAssemblyTargetAsmStreamer(
32 MCStreamer &S, formatted_raw_ostream &OS)
33 : WebAssemblyTargetStreamer(S), OS(OS) {}
34
WebAssemblyTargetELFStreamer(MCStreamer & S)35 WebAssemblyTargetELFStreamer::WebAssemblyTargetELFStreamer(MCStreamer &S)
36 : WebAssemblyTargetStreamer(S) {}
37
PrintTypes(formatted_raw_ostream & OS,ArrayRef<MVT> Types)38 static void PrintTypes(formatted_raw_ostream &OS, ArrayRef<MVT> Types) {
39 bool First = true;
40 for (MVT Type : Types) {
41 if (First)
42 First = false;
43 else
44 OS << ", ";
45 OS << WebAssembly::TypeToString(Type);
46 }
47 OS << '\n';
48 }
49
emitParam(ArrayRef<MVT> Types)50 void WebAssemblyTargetAsmStreamer::emitParam(ArrayRef<MVT> Types) {
51 OS << "\t.param \t";
52 PrintTypes(OS, Types);
53 }
54
emitResult(ArrayRef<MVT> Types)55 void WebAssemblyTargetAsmStreamer::emitResult(ArrayRef<MVT> Types) {
56 OS << "\t.result \t";
57 PrintTypes(OS, Types);
58 }
59
emitLocal(ArrayRef<MVT> Types)60 void WebAssemblyTargetAsmStreamer::emitLocal(ArrayRef<MVT> Types) {
61 OS << "\t.local \t";
62 PrintTypes(OS, Types);
63 }
64
emitEndFunc()65 void WebAssemblyTargetAsmStreamer::emitEndFunc() { OS << "\t.endfunc\n"; }
66
emitIndirectFunctionType(StringRef name,SmallVectorImpl<MVT> & SignatureVTs,size_t NumResults)67 void WebAssemblyTargetAsmStreamer::emitIndirectFunctionType(
68 StringRef name, SmallVectorImpl<MVT> &SignatureVTs, size_t NumResults) {
69 OS << "\t.functype\t" << name;
70 if (NumResults == 0) OS << ", void";
71 for (auto Ty : SignatureVTs) {
72 OS << ", " << WebAssembly::TypeToString(Ty);
73 }
74 OS << "\n";
75 }
76
77 // FIXME: What follows is not the real binary encoding.
78
EncodeTypes(MCStreamer & Streamer,ArrayRef<MVT> Types)79 static void EncodeTypes(MCStreamer &Streamer, ArrayRef<MVT> Types) {
80 Streamer.EmitIntValue(Types.size(), sizeof(uint64_t));
81 for (MVT Type : Types)
82 Streamer.EmitIntValue(Type.SimpleTy, sizeof(uint64_t));
83 }
84
emitParam(ArrayRef<MVT> Types)85 void WebAssemblyTargetELFStreamer::emitParam(ArrayRef<MVT> Types) {
86 Streamer.EmitIntValue(WebAssembly::DotParam, sizeof(uint64_t));
87 EncodeTypes(Streamer, Types);
88 }
89
emitResult(ArrayRef<MVT> Types)90 void WebAssemblyTargetELFStreamer::emitResult(ArrayRef<MVT> Types) {
91 Streamer.EmitIntValue(WebAssembly::DotResult, sizeof(uint64_t));
92 EncodeTypes(Streamer, Types);
93 }
94
emitLocal(ArrayRef<MVT> Types)95 void WebAssemblyTargetELFStreamer::emitLocal(ArrayRef<MVT> Types) {
96 Streamer.EmitIntValue(WebAssembly::DotLocal, sizeof(uint64_t));
97 EncodeTypes(Streamer, Types);
98 }
99
emitEndFunc()100 void WebAssemblyTargetELFStreamer::emitEndFunc() {
101 Streamer.EmitIntValue(WebAssembly::DotEndFunc, sizeof(uint64_t));
102 }
103