llvm.org GIT mirror llvm / afe8111
[WebAssembly] Add ObjectYAML support for wasm name section Differential Revision: https://reviews.llvm.org/D32841 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@302266 91177308-0d34-0410-b5e6-96231b3b80d8 Sam Clegg 2 years ago
5 changed file(s) with 120 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
9696 yaml::BinaryRef Content;
9797 };
9898
99 struct NameEntry {
100 uint32_t Index;
101 StringRef Name;
102 };
103
99104 struct Signature {
100105 Signature() : Form(wasm::WASM_TYPE_FUNC) {}
101106
121126
122127 StringRef Name;
123128 yaml::BinaryRef Payload;
129
130 // The follow is used by the "name" custom section.
131 // TODO(sbc): Add support for more then just functions names. The wasm
132 // name section can support multiple sub-sections.
133 std::vector FunctionNames;
124134 };
125135
126136 struct TypeSection : Section {
243253 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Function)
244254 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::LocalDecl)
245255 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Relocation)
256 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::NameEntry)
246257 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(uint32_t)
247258
248259 namespace llvm {
296307 static void mapping(IO &IO, WasmYAML::Relocation &Relocation);
297308 };
298309
310 template <> struct MappingTraits {
311 static void mapping(IO &IO, WasmYAML::NameEntry &NameEntry);
312 };
313
299314 template <> struct MappingTraits {
300315 static void mapping(IO &IO, WasmYAML::LocalDecl &LocalDecl);
301316 };
4949 static void sectionMapping(IO &IO, WasmYAML::CustomSection &Section) {
5050 commonSectionMapping(IO, Section);
5151 IO.mapRequired("Name", Section.Name);
52 IO.mapRequired("Payload", Section.Payload);
52 if (Section.Name == "name") {
53 IO.mapOptional("FunctionNames", Section.FunctionNames);
54 } else {
55 IO.mapRequired("Payload", Section.Payload);
56 }
5357 }
5458
5559 static void sectionMapping(IO &IO, WasmYAML::TypeSection &Section) {
225229 IO.mapOptional("Addend", Relocation.Addend, 0);
226230 }
227231
232 void MappingTraits::mapping(
233 IO &IO, WasmYAML::NameEntry &NameEntry) {
234 IO.mapRequired("Index", NameEntry.Index);
235 IO.mapRequired("Name", NameEntry.Name);
236 }
237
228238 void MappingTraits::mapping(
229239 IO &IO, WasmYAML::LocalDecl &LocalDecl) {
230240 IO.mapRequired("Type", LocalDecl.Type);
0 # RUN: yaml2obj %s | obj2yaml | FileCheck %s
1 --- !WASM
2 FileHeader:
3 Version: 0x00000001
4 Sections:
5 - Type: TYPE
6 Signatures:
7 - ReturnType: I32
8 ParamTypes:
9 - I32
10 - Type: IMPORT
11 Imports:
12 - Module: foo
13 Field: a
14 Kind: FUNCTION
15 SigIndex: 0
16 - Module: foo
17 Field: b
18 Kind: FUNCTION
19 SigIndex: 0
20 - Module: foo
21 Field: c
22 Kind: FUNCTION
23 SigIndex: 0
24 - Type: CUSTOM
25 Name: name
26 FunctionNames:
27 - Index: 1
28 Name: foo
29 - Index: 0
30 Name: bar
31 ...
32 # CHECK: - Type: CUSTOM
33 # CHECK-NEXT: Name: name
34 # CHECK-NEXT: FunctionNames:
35 # CHECK-NEXT: - Index: 1
36 # CHECK-NEXT: Name: foo
37 # CHECK-NEXT: - Index: 0
38 # CHECK-NEXT: Name: bar
39 # CHECK: ...
4343 }
4444 auto CustomSec = make_unique();
4545 CustomSec->Name = WasmSec.Name;
46 CustomSec->Payload = yaml::BinaryRef(WasmSec.Content);
46 if (CustomSec->Name == "name") {
47 for (const object::SymbolRef& Sym: Obj.symbols()) {
48 uint32_t Flags = Sym.getFlags();
49 // Skip over symbols that come from imports or exports
50 if (Flags &
51 (object::SymbolRef::SF_Global | object::SymbolRef::SF_Undefined))
52 continue;
53 Expected NameOrError = Sym.getName();
54 if (!NameOrError)
55 continue;
56 WasmYAML::NameEntry NameEntry;
57 NameEntry.Name = *NameOrError;
58 NameEntry.Index = Sym.getValue();
59 CustomSec->FunctionNames.push_back(NameEntry);
60 }
61 } else {
62 CustomSec->Payload = yaml::BinaryRef(WasmSec.Content);
63 }
4764 S = std::move(CustomSec);
4865 break;
4966 }
2626 WasmWriter(WasmYAML::Object &Obj) : Obj(Obj) {}
2727 int writeWasm(raw_ostream &OS);
2828 int writeRelocSection(raw_ostream &OS, WasmYAML::Section &Sec);
29 int writeNameSection(raw_ostream &OS, WasmYAML::CustomSection &Section);
30
2931 int writeSectionContent(raw_ostream &OS, WasmYAML::CustomSection &Section);
3032 int writeSectionContent(raw_ostream &OS, WasmYAML::TypeSection &Section);
3133 int writeSectionContent(raw_ostream &OS, WasmYAML::ImportSection &Section);
6466 return 0;
6567 }
6668
67 static int writeStringRef(StringRef &Str, raw_ostream &OS) {
69 static int writeStringRef(const StringRef &Str, raw_ostream &OS) {
6870 encodeULEB128(Str.size(), OS);
6971 OS << Str;
7072 return 0;
7173 }
7274
73 static int writeLimits(WasmYAML::Limits Lim, raw_ostream &OS) {
75 static int writeLimits(const WasmYAML::Limits &Lim, raw_ostream &OS) {
7476 encodeULEB128(Lim.Flags, OS);
7577 encodeULEB128(Lim.Initial, OS);
7678 if (Lim.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX)
7880 return 0;
7981 }
8082
81 static int writeInitExpr(wasm::WasmInitExpr InitExpr, raw_ostream &OS) {
83 static int writeInitExpr(const wasm::WasmInitExpr &InitExpr, raw_ostream &OS) {
8284 writeUint8(OS, InitExpr.Opcode);
8385 switch (InitExpr.Opcode) {
8486 case wasm::WASM_OPCODE_I32_CONST:
104106 return 0;
105107 }
106108
109 int WasmWriter::writeNameSection(raw_ostream &OS,
110 WasmYAML::CustomSection &Section) {
111 writeStringRef(Section.Name, OS);
112 if (Section.FunctionNames.size()) {
113 encodeULEB128(wasm::WASM_NAMES_FUNCTION, OS);
114
115 std::string OutString;
116 raw_string_ostream StringStream(OutString);
117
118 encodeULEB128(Section.FunctionNames.size(), StringStream);
119 for (const WasmYAML::NameEntry &NameEntry : Section.FunctionNames) {
120 encodeULEB128(NameEntry.Index, StringStream);
121 writeStringRef(NameEntry.Name, StringStream);
122 }
123
124 StringStream.flush();
125 encodeULEB128(OutString.size(), OS);
126 OS << OutString;
127 }
128 return 0;
129 }
130
107131 int WasmWriter::writeSectionContent(raw_ostream &OS,
108132 WasmYAML::CustomSection &Section) {
109 // writeStringRef(Section.Name, OS);
110 // encodeULEB128(Section.Payload.binary_size(), OS);
111 Section.Payload.writeAsBinary(OS);
133 if (Section.Name == "name") {
134 writeNameSection(OS, Section);
135 } else {
136 Section.Payload.writeAsBinary(OS);
137 }
112138 return 0;
113139 }
114140
115141 int WasmWriter::writeSectionContent(raw_ostream &OS,
116142 WasmYAML::TypeSection &Section) {
117143 encodeULEB128(Section.Signatures.size(), OS);
118 for (auto &Sig : Section.Signatures) {
144 for (const WasmYAML::Signature &Sig : Section.Signatures) {
119145 encodeSLEB128(Sig.Form, OS);
120146 encodeULEB128(Sig.ParamTypes.size(), OS);
121147 for (auto ParamType : Sig.ParamTypes)
133159 int WasmWriter::writeSectionContent(raw_ostream &OS,
134160 WasmYAML::ImportSection &Section) {
135161 encodeULEB128(Section.Imports.size(), OS);
136 for (auto &Import : Section.Imports) {
162 for (const WasmYAML::Import &Import : Section.Imports) {
137163 writeStringRef(Import.Module, OS);
138164 writeStringRef(Import.Field, OS);
139165 encodeULEB128(Import.Kind, OS);
165191 int WasmWriter::writeSectionContent(raw_ostream &OS,
166192 WasmYAML::ExportSection &Section) {
167193 encodeULEB128(Section.Exports.size(), OS);
168 for (auto &Export : Section.Exports) {
194 for (const WasmYAML::Export &Export : Section.Exports) {
169195 writeStringRef(Export.Name, OS);
170196 encodeULEB128(Export.Kind, OS);
171197 encodeULEB128(Export.Index, OS);
192218 int WasmWriter::writeSectionContent(raw_ostream &OS,
193219 WasmYAML::MemorySection &Section) {
194220 encodeULEB128(Section.Memories.size(), OS);
195 for (auto &Mem : Section.Memories) {
221 for (const WasmYAML::Limits &Mem : Section.Memories) {
196222 writeLimits(Mem, OS);
197223 }
198224 return 0;