Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 1 | //===- yaml2wasm - Convert YAML to a Wasm object file --------------------===// |
| 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 | /// The Wasm component of yaml2obj. |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | // |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 15 | |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 16 | #include "llvm/ObjectYAML/ObjectYAML.h" |
| 17 | #include "llvm/Support/Endian.h" |
| 18 | #include "llvm/Support/LEB128.h" |
| 19 | |
| 20 | using namespace llvm; |
| 21 | |
| 22 | /// This parses a yaml stream that represents a Wasm object file. |
| 23 | /// See docs/yaml2obj for the yaml scheema. |
| 24 | class WasmWriter { |
| 25 | public: |
| 26 | WasmWriter(WasmYAML::Object &Obj) : Obj(Obj) {} |
| 27 | int writeWasm(raw_ostream &OS); |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 28 | |
| 29 | private: |
Sam Clegg | 6f08c84 | 2018-04-24 18:11:36 +0000 | [diff] [blame] | 30 | int writeRelocSection(raw_ostream &OS, WasmYAML::Section &Sec, |
| 31 | uint32_t SectionIndex); |
Sam Clegg | 03cdd12 | 2017-05-05 18:12:34 +0000 | [diff] [blame] | 32 | |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 33 | int writeSectionContent(raw_ostream &OS, WasmYAML::CustomSection &Section); |
| 34 | int writeSectionContent(raw_ostream &OS, WasmYAML::TypeSection &Section); |
| 35 | int writeSectionContent(raw_ostream &OS, WasmYAML::ImportSection &Section); |
| 36 | int writeSectionContent(raw_ostream &OS, WasmYAML::FunctionSection &Section); |
| 37 | int writeSectionContent(raw_ostream &OS, WasmYAML::TableSection &Section); |
| 38 | int writeSectionContent(raw_ostream &OS, WasmYAML::MemorySection &Section); |
| 39 | int writeSectionContent(raw_ostream &OS, WasmYAML::GlobalSection &Section); |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 40 | int writeSectionContent(raw_ostream &OS, WasmYAML::EventSection &Section); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 41 | int writeSectionContent(raw_ostream &OS, WasmYAML::ExportSection &Section); |
| 42 | int writeSectionContent(raw_ostream &OS, WasmYAML::StartSection &Section); |
| 43 | int writeSectionContent(raw_ostream &OS, WasmYAML::ElemSection &Section); |
| 44 | int writeSectionContent(raw_ostream &OS, WasmYAML::CodeSection &Section); |
| 45 | int writeSectionContent(raw_ostream &OS, WasmYAML::DataSection &Section); |
| 46 | |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 47 | // Custom section types |
Sam Clegg | e4afbc6 | 2018-11-14 18:36:24 +0000 | [diff] [blame^] | 48 | int writeSectionContent(raw_ostream &OS, WasmYAML::DylinkSection &Section); |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 49 | int writeSectionContent(raw_ostream &OS, WasmYAML::NameSection &Section); |
| 50 | int writeSectionContent(raw_ostream &OS, WasmYAML::LinkingSection &Section); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 51 | WasmYAML::Object &Obj; |
Sam Clegg | e53af7f | 2018-01-09 21:38:53 +0000 | [diff] [blame] | 52 | uint32_t NumImportedFunctions = 0; |
| 53 | uint32_t NumImportedGlobals = 0; |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 54 | uint32_t NumImportedEvents = 0; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | static int writeUint64(raw_ostream &OS, uint64_t Value) { |
| 58 | char Data[sizeof(Value)]; |
| 59 | support::endian::write64le(Data, Value); |
| 60 | OS.write(Data, sizeof(Data)); |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | static int writeUint32(raw_ostream &OS, uint32_t Value) { |
| 65 | char Data[sizeof(Value)]; |
| 66 | support::endian::write32le(Data, Value); |
| 67 | OS.write(Data, sizeof(Data)); |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | static int writeUint8(raw_ostream &OS, uint8_t Value) { |
| 72 | char Data[sizeof(Value)]; |
| 73 | memcpy(Data, &Value, sizeof(Data)); |
| 74 | OS.write(Data, sizeof(Data)); |
| 75 | return 0; |
| 76 | } |
| 77 | |
Sam Clegg | 03cdd12 | 2017-05-05 18:12:34 +0000 | [diff] [blame] | 78 | static int writeStringRef(const StringRef &Str, raw_ostream &OS) { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 79 | encodeULEB128(Str.size(), OS); |
| 80 | OS << Str; |
| 81 | return 0; |
| 82 | } |
| 83 | |
Sam Clegg | 03cdd12 | 2017-05-05 18:12:34 +0000 | [diff] [blame] | 84 | static int writeLimits(const WasmYAML::Limits &Lim, raw_ostream &OS) { |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 85 | writeUint8(OS, Lim.Flags); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 86 | encodeULEB128(Lim.Initial, OS); |
| 87 | if (Lim.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX) |
| 88 | encodeULEB128(Lim.Maximum, OS); |
| 89 | return 0; |
| 90 | } |
| 91 | |
Sam Clegg | 03cdd12 | 2017-05-05 18:12:34 +0000 | [diff] [blame] | 92 | static int writeInitExpr(const wasm::WasmInitExpr &InitExpr, raw_ostream &OS) { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 93 | writeUint8(OS, InitExpr.Opcode); |
| 94 | switch (InitExpr.Opcode) { |
| 95 | case wasm::WASM_OPCODE_I32_CONST: |
| 96 | encodeSLEB128(InitExpr.Value.Int32, OS); |
| 97 | break; |
| 98 | case wasm::WASM_OPCODE_I64_CONST: |
| 99 | encodeSLEB128(InitExpr.Value.Int64, OS); |
| 100 | break; |
| 101 | case wasm::WASM_OPCODE_F32_CONST: |
| 102 | writeUint32(OS, InitExpr.Value.Float32); |
| 103 | break; |
| 104 | case wasm::WASM_OPCODE_F64_CONST: |
| 105 | writeUint64(OS, InitExpr.Value.Float64); |
| 106 | break; |
| 107 | case wasm::WASM_OPCODE_GET_GLOBAL: |
| 108 | encodeULEB128(InitExpr.Value.Global, OS); |
| 109 | break; |
| 110 | default: |
Sam Clegg | e53af7f | 2018-01-09 21:38:53 +0000 | [diff] [blame] | 111 | errs() << "Unknown opcode in init_expr: " << InitExpr.Opcode << "\n"; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 112 | return 1; |
| 113 | } |
| 114 | writeUint8(OS, wasm::WASM_OPCODE_END); |
| 115 | return 0; |
| 116 | } |
| 117 | |
Sam Clegg | 9e1ade9 | 2017-06-27 20:27:59 +0000 | [diff] [blame] | 118 | class SubSectionWriter { |
| 119 | raw_ostream &OS; |
| 120 | std::string OutString; |
| 121 | raw_string_ostream StringStream; |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 122 | |
Sam Clegg | 9e1ade9 | 2017-06-27 20:27:59 +0000 | [diff] [blame] | 123 | public: |
| 124 | SubSectionWriter(raw_ostream &OS) : OS(OS), StringStream(OutString) {} |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 125 | |
Sam Clegg | 9e1ade9 | 2017-06-27 20:27:59 +0000 | [diff] [blame] | 126 | void Done() { |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 127 | StringStream.flush(); |
| 128 | encodeULEB128(OutString.size(), OS); |
| 129 | OS << OutString; |
Sam Clegg | 9e1ade9 | 2017-06-27 20:27:59 +0000 | [diff] [blame] | 130 | OutString.clear(); |
| 131 | } |
| 132 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 133 | raw_ostream &GetStream() { return StringStream; } |
Sam Clegg | 9e1ade9 | 2017-06-27 20:27:59 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 136 | int WasmWriter::writeSectionContent(raw_ostream &OS, |
Sam Clegg | e4afbc6 | 2018-11-14 18:36:24 +0000 | [diff] [blame^] | 137 | WasmYAML::DylinkSection &Section) { |
| 138 | writeStringRef(Section.Name, OS); |
| 139 | encodeULEB128(Section.MemorySize, OS); |
| 140 | encodeULEB128(Section.MemoryAlignment, OS); |
| 141 | encodeULEB128(Section.TableSize, OS); |
| 142 | encodeULEB128(Section.TableAlignment, OS); |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | int WasmWriter::writeSectionContent(raw_ostream &OS, |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 147 | WasmYAML::LinkingSection &Section) { |
Sam Clegg | 9e1ade9 | 2017-06-27 20:27:59 +0000 | [diff] [blame] | 148 | writeStringRef(Section.Name, OS); |
Sam Clegg | 6bb5a41 | 2018-04-26 18:15:32 +0000 | [diff] [blame] | 149 | encodeULEB128(Section.Version, OS); |
Sam Clegg | 9e1ade9 | 2017-06-27 20:27:59 +0000 | [diff] [blame] | 150 | |
| 151 | SubSectionWriter SubSection(OS); |
| 152 | |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 153 | // SYMBOL_TABLE subsection |
| 154 | if (Section.SymbolTable.size()) { |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 155 | writeUint8(OS, wasm::WASM_SYMBOL_TABLE); |
Sam Clegg | 9e1ade9 | 2017-06-27 20:27:59 +0000 | [diff] [blame] | 156 | |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 157 | encodeULEB128(Section.SymbolTable.size(), SubSection.GetStream()); |
Benjamin Kramer | 8d71fdc | 2018-02-23 12:20:18 +0000 | [diff] [blame] | 158 | #ifndef NDEBUG |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 159 | uint32_t SymbolIndex = 0; |
| 160 | #endif |
| 161 | for (const WasmYAML::SymbolInfo &Info : Section.SymbolTable) { |
| 162 | assert(Info.Index == SymbolIndex++); |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 163 | writeUint8(SubSection.GetStream(), Info.Kind); |
Sam Clegg | 9e1ade9 | 2017-06-27 20:27:59 +0000 | [diff] [blame] | 164 | encodeULEB128(Info.Flags, SubSection.GetStream()); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 165 | switch (Info.Kind) { |
| 166 | case wasm::WASM_SYMBOL_TYPE_FUNCTION: |
| 167 | case wasm::WASM_SYMBOL_TYPE_GLOBAL: |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 168 | case wasm::WASM_SYMBOL_TYPE_EVENT: |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 169 | encodeULEB128(Info.ElementIndex, SubSection.GetStream()); |
| 170 | if ((Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0) |
| 171 | writeStringRef(Info.Name, SubSection.GetStream()); |
| 172 | break; |
| 173 | case wasm::WASM_SYMBOL_TYPE_DATA: |
| 174 | writeStringRef(Info.Name, SubSection.GetStream()); |
| 175 | if ((Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0) { |
| 176 | encodeULEB128(Info.DataRef.Segment, SubSection.GetStream()); |
| 177 | encodeULEB128(Info.DataRef.Offset, SubSection.GetStream()); |
| 178 | encodeULEB128(Info.DataRef.Size, SubSection.GetStream()); |
| 179 | } |
| 180 | break; |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 181 | case wasm::WASM_SYMBOL_TYPE_SECTION: |
| 182 | encodeULEB128(Info.ElementIndex, SubSection.GetStream()); |
| 183 | break; |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 184 | default: |
| 185 | llvm_unreachable("unexpected kind"); |
| 186 | } |
Sam Clegg | 9e1ade9 | 2017-06-27 20:27:59 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | SubSection.Done(); |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 190 | } |
Sam Clegg | d95ed95 | 2017-09-20 19:03:35 +0000 | [diff] [blame] | 191 | |
| 192 | // SEGMENT_NAMES subsection |
Sam Clegg | 63ebb81 | 2017-09-29 16:50:08 +0000 | [diff] [blame] | 193 | if (Section.SegmentInfos.size()) { |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 194 | writeUint8(OS, wasm::WASM_SEGMENT_INFO); |
Sam Clegg | 63ebb81 | 2017-09-29 16:50:08 +0000 | [diff] [blame] | 195 | encodeULEB128(Section.SegmentInfos.size(), SubSection.GetStream()); |
| 196 | for (const WasmYAML::SegmentInfo &SegmentInfo : Section.SegmentInfos) { |
Sam Clegg | 63ebb81 | 2017-09-29 16:50:08 +0000 | [diff] [blame] | 197 | writeStringRef(SegmentInfo.Name, SubSection.GetStream()); |
| 198 | encodeULEB128(SegmentInfo.Alignment, SubSection.GetStream()); |
| 199 | encodeULEB128(SegmentInfo.Flags, SubSection.GetStream()); |
Sam Clegg | d95ed95 | 2017-09-20 19:03:35 +0000 | [diff] [blame] | 200 | } |
| 201 | SubSection.Done(); |
| 202 | } |
Sam Clegg | 4273998 | 2017-12-14 21:10:03 +0000 | [diff] [blame] | 203 | |
| 204 | // INIT_FUNCS subsection |
| 205 | if (Section.InitFunctions.size()) { |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 206 | writeUint8(OS, wasm::WASM_INIT_FUNCS); |
Sam Clegg | 4273998 | 2017-12-14 21:10:03 +0000 | [diff] [blame] | 207 | encodeULEB128(Section.InitFunctions.size(), SubSection.GetStream()); |
| 208 | for (const WasmYAML::InitFunction &Func : Section.InitFunctions) { |
| 209 | encodeULEB128(Func.Priority, SubSection.GetStream()); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 210 | encodeULEB128(Func.Symbol, SubSection.GetStream()); |
Sam Clegg | 4273998 | 2017-12-14 21:10:03 +0000 | [diff] [blame] | 211 | } |
| 212 | SubSection.Done(); |
| 213 | } |
Sam Clegg | ea7cace | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 214 | |
| 215 | // COMDAT_INFO subsection |
| 216 | if (Section.Comdats.size()) { |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 217 | writeUint8(OS, wasm::WASM_COMDAT_INFO); |
Sam Clegg | ea7cace | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 218 | encodeULEB128(Section.Comdats.size(), SubSection.GetStream()); |
| 219 | for (const auto &C : Section.Comdats) { |
| 220 | writeStringRef(C.Name, SubSection.GetStream()); |
| 221 | encodeULEB128(0, SubSection.GetStream()); // flags for future use |
| 222 | encodeULEB128(C.Entries.size(), SubSection.GetStream()); |
| 223 | for (const WasmYAML::ComdatEntry &Entry : C.Entries) { |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 224 | writeUint8(SubSection.GetStream(), Entry.Kind); |
Sam Clegg | ea7cace | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 225 | encodeULEB128(Entry.Index, SubSection.GetStream()); |
| 226 | } |
| 227 | } |
| 228 | SubSection.Done(); |
| 229 | } |
| 230 | |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 231 | return 0; |
| 232 | } |
| 233 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 234 | int WasmWriter::writeSectionContent(raw_ostream &OS, |
| 235 | WasmYAML::NameSection &Section) { |
Sam Clegg | 03cdd12 | 2017-05-05 18:12:34 +0000 | [diff] [blame] | 236 | writeStringRef(Section.Name, OS); |
| 237 | if (Section.FunctionNames.size()) { |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 238 | writeUint8(OS, wasm::WASM_NAMES_FUNCTION); |
Sam Clegg | 03cdd12 | 2017-05-05 18:12:34 +0000 | [diff] [blame] | 239 | |
Sam Clegg | 9e1ade9 | 2017-06-27 20:27:59 +0000 | [diff] [blame] | 240 | SubSectionWriter SubSection(OS); |
| 241 | |
| 242 | encodeULEB128(Section.FunctionNames.size(), SubSection.GetStream()); |
Sam Clegg | 03cdd12 | 2017-05-05 18:12:34 +0000 | [diff] [blame] | 243 | for (const WasmYAML::NameEntry &NameEntry : Section.FunctionNames) { |
Sam Clegg | 9e1ade9 | 2017-06-27 20:27:59 +0000 | [diff] [blame] | 244 | encodeULEB128(NameEntry.Index, SubSection.GetStream()); |
| 245 | writeStringRef(NameEntry.Name, SubSection.GetStream()); |
Sam Clegg | 03cdd12 | 2017-05-05 18:12:34 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Sam Clegg | 9e1ade9 | 2017-06-27 20:27:59 +0000 | [diff] [blame] | 248 | SubSection.Done(); |
Sam Clegg | 03cdd12 | 2017-05-05 18:12:34 +0000 | [diff] [blame] | 249 | } |
| 250 | return 0; |
| 251 | } |
| 252 | |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 253 | int WasmWriter::writeSectionContent(raw_ostream &OS, |
| 254 | WasmYAML::CustomSection &Section) { |
Sam Clegg | e4afbc6 | 2018-11-14 18:36:24 +0000 | [diff] [blame^] | 255 | if (auto S = dyn_cast<WasmYAML::DylinkSection>(&Section)) { |
| 256 | if (auto Err = writeSectionContent(OS, *S)) |
| 257 | return Err; |
| 258 | } else if (auto S = dyn_cast<WasmYAML::NameSection>(&Section)) { |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 259 | if (auto Err = writeSectionContent(OS, *S)) |
| 260 | return Err; |
| 261 | } else if (auto S = dyn_cast<WasmYAML::LinkingSection>(&Section)) { |
| 262 | if (auto Err = writeSectionContent(OS, *S)) |
| 263 | return Err; |
Sam Clegg | 03cdd12 | 2017-05-05 18:12:34 +0000 | [diff] [blame] | 264 | } else { |
Sam Clegg | 9745afa | 2018-04-12 20:31:12 +0000 | [diff] [blame] | 265 | writeStringRef(Section.Name, OS); |
Sam Clegg | 03cdd12 | 2017-05-05 18:12:34 +0000 | [diff] [blame] | 266 | Section.Payload.writeAsBinary(OS); |
| 267 | } |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | int WasmWriter::writeSectionContent(raw_ostream &OS, |
| 272 | WasmYAML::TypeSection &Section) { |
| 273 | encodeULEB128(Section.Signatures.size(), OS); |
Sam Clegg | e53af7f | 2018-01-09 21:38:53 +0000 | [diff] [blame] | 274 | uint32_t ExpectedIndex = 0; |
Sam Clegg | 03cdd12 | 2017-05-05 18:12:34 +0000 | [diff] [blame] | 275 | for (const WasmYAML::Signature &Sig : Section.Signatures) { |
Sam Clegg | e53af7f | 2018-01-09 21:38:53 +0000 | [diff] [blame] | 276 | if (Sig.Index != ExpectedIndex) { |
| 277 | errs() << "Unexpected type index: " << Sig.Index << "\n"; |
| 278 | return 1; |
| 279 | } |
| 280 | ++ExpectedIndex; |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 281 | writeUint8(OS, Sig.Form); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 282 | encodeULEB128(Sig.ParamTypes.size(), OS); |
| 283 | for (auto ParamType : Sig.ParamTypes) |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 284 | writeUint8(OS, ParamType); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 285 | if (Sig.ReturnType == wasm::WASM_TYPE_NORESULT) { |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 286 | encodeULEB128(0, OS); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 287 | } else { |
| 288 | encodeULEB128(1, OS); |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 289 | writeUint8(OS, Sig.ReturnType); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | int WasmWriter::writeSectionContent(raw_ostream &OS, |
| 296 | WasmYAML::ImportSection &Section) { |
| 297 | encodeULEB128(Section.Imports.size(), OS); |
Sam Clegg | 03cdd12 | 2017-05-05 18:12:34 +0000 | [diff] [blame] | 298 | for (const WasmYAML::Import &Import : Section.Imports) { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 299 | writeStringRef(Import.Module, OS); |
| 300 | writeStringRef(Import.Field, OS); |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 301 | writeUint8(OS, Import.Kind); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 302 | switch (Import.Kind) { |
| 303 | case wasm::WASM_EXTERNAL_FUNCTION: |
| 304 | encodeULEB128(Import.SigIndex, OS); |
Sam Clegg | e53af7f | 2018-01-09 21:38:53 +0000 | [diff] [blame] | 305 | NumImportedFunctions++; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 306 | break; |
| 307 | case wasm::WASM_EXTERNAL_GLOBAL: |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 308 | writeUint8(OS, Import.GlobalImport.Type); |
Sam Clegg | 41db519 | 2017-05-10 00:14:04 +0000 | [diff] [blame] | 309 | writeUint8(OS, Import.GlobalImport.Mutable); |
Sam Clegg | e53af7f | 2018-01-09 21:38:53 +0000 | [diff] [blame] | 310 | NumImportedGlobals++; |
Sam Clegg | 2ffff5a | 2017-05-09 23:48:41 +0000 | [diff] [blame] | 311 | break; |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 312 | case wasm::WASM_EXTERNAL_EVENT: |
| 313 | writeUint32(OS, Import.EventImport.Attribute); |
| 314 | writeUint32(OS, Import.EventImport.SigIndex); |
| 315 | NumImportedGlobals++; |
| 316 | break; |
Sam Clegg | 2ffff5a | 2017-05-09 23:48:41 +0000 | [diff] [blame] | 317 | case wasm::WASM_EXTERNAL_MEMORY: |
| 318 | writeLimits(Import.Memory, OS); |
| 319 | break; |
| 320 | case wasm::WASM_EXTERNAL_TABLE: |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 321 | writeUint8(OS, Import.TableImport.ElemType); |
Sam Clegg | 41db519 | 2017-05-10 00:14:04 +0000 | [diff] [blame] | 322 | writeLimits(Import.TableImport.TableLimits, OS); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 323 | break; |
| 324 | default: |
Sam Clegg | e53af7f | 2018-01-09 21:38:53 +0000 | [diff] [blame] | 325 | errs() << "Unknown import type: " << Import.Kind << "\n"; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 326 | return 1; |
| 327 | } |
| 328 | } |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | int WasmWriter::writeSectionContent(raw_ostream &OS, |
| 333 | WasmYAML::FunctionSection &Section) { |
| 334 | encodeULEB128(Section.FunctionTypes.size(), OS); |
| 335 | for (uint32_t FuncType : Section.FunctionTypes) { |
| 336 | encodeULEB128(FuncType, OS); |
| 337 | } |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | int WasmWriter::writeSectionContent(raw_ostream &OS, |
| 342 | WasmYAML::ExportSection &Section) { |
| 343 | encodeULEB128(Section.Exports.size(), OS); |
Sam Clegg | 03cdd12 | 2017-05-05 18:12:34 +0000 | [diff] [blame] | 344 | for (const WasmYAML::Export &Export : Section.Exports) { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 345 | writeStringRef(Export.Name, OS); |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 346 | writeUint8(OS, Export.Kind); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 347 | encodeULEB128(Export.Index, OS); |
| 348 | } |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | int WasmWriter::writeSectionContent(raw_ostream &OS, |
| 353 | WasmYAML::StartSection &Section) { |
| 354 | encodeULEB128(Section.StartFunction, OS); |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | int WasmWriter::writeSectionContent(raw_ostream &OS, |
| 359 | WasmYAML::TableSection &Section) { |
| 360 | encodeULEB128(Section.Tables.size(), OS); |
| 361 | for (auto &Table : Section.Tables) { |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 362 | writeUint8(OS, Table.ElemType); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 363 | writeLimits(Table.TableLimits, OS); |
| 364 | } |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | int WasmWriter::writeSectionContent(raw_ostream &OS, |
| 369 | WasmYAML::MemorySection &Section) { |
| 370 | encodeULEB128(Section.Memories.size(), OS); |
Sam Clegg | 03cdd12 | 2017-05-05 18:12:34 +0000 | [diff] [blame] | 371 | for (const WasmYAML::Limits &Mem : Section.Memories) { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 372 | writeLimits(Mem, OS); |
| 373 | } |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | int WasmWriter::writeSectionContent(raw_ostream &OS, |
| 378 | WasmYAML::GlobalSection &Section) { |
| 379 | encodeULEB128(Section.Globals.size(), OS); |
Sam Clegg | e53af7f | 2018-01-09 21:38:53 +0000 | [diff] [blame] | 380 | uint32_t ExpectedIndex = NumImportedGlobals; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 381 | for (auto &Global : Section.Globals) { |
Sam Clegg | e53af7f | 2018-01-09 21:38:53 +0000 | [diff] [blame] | 382 | if (Global.Index != ExpectedIndex) { |
| 383 | errs() << "Unexpected global index: " << Global.Index << "\n"; |
| 384 | return 1; |
| 385 | } |
| 386 | ++ExpectedIndex; |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 387 | writeUint8(OS, Global.Type); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 388 | writeUint8(OS, Global.Mutable); |
| 389 | writeInitExpr(Global.InitExpr, OS); |
| 390 | } |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | int WasmWriter::writeSectionContent(raw_ostream &OS, |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 395 | WasmYAML::EventSection &Section) { |
| 396 | encodeULEB128(Section.Events.size(), OS); |
| 397 | uint32_t ExpectedIndex = NumImportedEvents; |
| 398 | for (auto &Event : Section.Events) { |
| 399 | if (Event.Index != ExpectedIndex) { |
| 400 | errs() << "Unexpected event index: " << Event.Index << "\n"; |
| 401 | return 1; |
| 402 | } |
| 403 | ++ExpectedIndex; |
| 404 | encodeULEB128(Event.Attribute, OS); |
| 405 | encodeULEB128(Event.SigIndex, OS); |
| 406 | } |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | int WasmWriter::writeSectionContent(raw_ostream &OS, |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 411 | WasmYAML::ElemSection &Section) { |
| 412 | encodeULEB128(Section.Segments.size(), OS); |
| 413 | for (auto &Segment : Section.Segments) { |
| 414 | encodeULEB128(Segment.TableIndex, OS); |
| 415 | writeInitExpr(Segment.Offset, OS); |
| 416 | |
| 417 | encodeULEB128(Segment.Functions.size(), OS); |
| 418 | for (auto &Function : Segment.Functions) { |
| 419 | encodeULEB128(Function, OS); |
| 420 | } |
| 421 | } |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | int WasmWriter::writeSectionContent(raw_ostream &OS, |
| 426 | WasmYAML::CodeSection &Section) { |
| 427 | encodeULEB128(Section.Functions.size(), OS); |
Sam Clegg | e53af7f | 2018-01-09 21:38:53 +0000 | [diff] [blame] | 428 | uint32_t ExpectedIndex = NumImportedFunctions; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 429 | for (auto &Func : Section.Functions) { |
| 430 | std::string OutString; |
| 431 | raw_string_ostream StringStream(OutString); |
Sam Clegg | e53af7f | 2018-01-09 21:38:53 +0000 | [diff] [blame] | 432 | if (Func.Index != ExpectedIndex) { |
| 433 | errs() << "Unexpected function index: " << Func.Index << "\n"; |
| 434 | return 1; |
| 435 | } |
| 436 | ++ExpectedIndex; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 437 | |
| 438 | encodeULEB128(Func.Locals.size(), StringStream); |
| 439 | for (auto &LocalDecl : Func.Locals) { |
| 440 | encodeULEB128(LocalDecl.Count, StringStream); |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 441 | writeUint8(StringStream, LocalDecl.Type); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | Func.Body.writeAsBinary(StringStream); |
| 445 | |
| 446 | // Write the section size followed by the content |
| 447 | StringStream.flush(); |
| 448 | encodeULEB128(OutString.size(), OS); |
| 449 | OS << OutString; |
| 450 | } |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | int WasmWriter::writeSectionContent(raw_ostream &OS, |
| 455 | WasmYAML::DataSection &Section) { |
| 456 | encodeULEB128(Section.Segments.size(), OS); |
| 457 | for (auto &Segment : Section.Segments) { |
Sam Clegg | 9c07f94 | 2017-07-12 00:24:54 +0000 | [diff] [blame] | 458 | encodeULEB128(Segment.MemoryIndex, OS); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 459 | writeInitExpr(Segment.Offset, OS); |
| 460 | encodeULEB128(Segment.Content.binary_size(), OS); |
| 461 | Segment.Content.writeAsBinary(OS); |
| 462 | } |
| 463 | return 0; |
| 464 | } |
| 465 | |
Sam Clegg | 6f08c84 | 2018-04-24 18:11:36 +0000 | [diff] [blame] | 466 | int WasmWriter::writeRelocSection(raw_ostream &OS, WasmYAML::Section &Sec, |
| 467 | uint32_t SectionIndex) { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 468 | switch (Sec.Type) { |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 469 | case wasm::WASM_SEC_CODE: |
| 470 | writeStringRef("reloc.CODE", OS); |
| 471 | break; |
| 472 | case wasm::WASM_SEC_DATA: |
| 473 | writeStringRef("reloc.DATA", OS); |
| 474 | break; |
| 475 | case wasm::WASM_SEC_CUSTOM: { |
| 476 | auto CustomSection = dyn_cast<WasmYAML::CustomSection>(&Sec); |
| 477 | if (!CustomSection->Name.startswith(".debug_")) { |
| 478 | llvm_unreachable("not yet implemented (only for debug sections)"); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 479 | return 1; |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | writeStringRef(("reloc." + CustomSection->Name).str(), OS); |
| 483 | break; |
| 484 | } |
| 485 | default: |
| 486 | llvm_unreachable("not yet implemented"); |
| 487 | return 1; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Sam Clegg | 6f08c84 | 2018-04-24 18:11:36 +0000 | [diff] [blame] | 490 | encodeULEB128(SectionIndex, OS); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 491 | encodeULEB128(Sec.Relocations.size(), OS); |
| 492 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 493 | for (auto Reloc : Sec.Relocations) { |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 494 | writeUint8(OS, Reloc.Type); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 495 | encodeULEB128(Reloc.Offset, OS); |
| 496 | encodeULEB128(Reloc.Index, OS); |
| 497 | switch (Reloc.Type) { |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 498 | case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB: |
| 499 | case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB: |
| 500 | case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32: |
| 501 | case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32: |
| 502 | case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32: |
| 503 | encodeULEB128(Reloc.Addend, OS); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | return 0; |
| 507 | } |
| 508 | |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 509 | int WasmWriter::writeWasm(raw_ostream &OS) { |
| 510 | // Write headers |
| 511 | OS.write(wasm::WasmMagic, sizeof(wasm::WasmMagic)); |
| 512 | writeUint32(OS, Obj.Header.Version); |
| 513 | |
| 514 | // Write each section |
| 515 | for (const std::unique_ptr<WasmYAML::Section> &Sec : Obj.Sections) { |
Sam Clegg | e53af7f | 2018-01-09 21:38:53 +0000 | [diff] [blame] | 516 | encodeULEB128(Sec->Type, OS); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 517 | std::string OutString; |
| 518 | raw_string_ostream StringStream(OutString); |
| 519 | if (auto S = dyn_cast<WasmYAML::CustomSection>(Sec.get())) { |
| 520 | if (auto Err = writeSectionContent(StringStream, *S)) |
| 521 | return Err; |
| 522 | } else if (auto S = dyn_cast<WasmYAML::TypeSection>(Sec.get())) { |
| 523 | if (auto Err = writeSectionContent(StringStream, *S)) |
| 524 | return Err; |
| 525 | } else if (auto S = dyn_cast<WasmYAML::ImportSection>(Sec.get())) { |
| 526 | if (auto Err = writeSectionContent(StringStream, *S)) |
| 527 | return Err; |
| 528 | } else if (auto S = dyn_cast<WasmYAML::FunctionSection>(Sec.get())) { |
| 529 | if (auto Err = writeSectionContent(StringStream, *S)) |
| 530 | return Err; |
| 531 | } else if (auto S = dyn_cast<WasmYAML::TableSection>(Sec.get())) { |
| 532 | if (auto Err = writeSectionContent(StringStream, *S)) |
| 533 | return Err; |
| 534 | } else if (auto S = dyn_cast<WasmYAML::MemorySection>(Sec.get())) { |
| 535 | if (auto Err = writeSectionContent(StringStream, *S)) |
| 536 | return Err; |
| 537 | } else if (auto S = dyn_cast<WasmYAML::GlobalSection>(Sec.get())) { |
| 538 | if (auto Err = writeSectionContent(StringStream, *S)) |
| 539 | return Err; |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 540 | } else if (auto S = dyn_cast<WasmYAML::EventSection>(Sec.get())) { |
| 541 | if (auto Err = writeSectionContent(StringStream, *S)) |
| 542 | return Err; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 543 | } else if (auto S = dyn_cast<WasmYAML::ExportSection>(Sec.get())) { |
| 544 | if (auto Err = writeSectionContent(StringStream, *S)) |
| 545 | return Err; |
| 546 | } else if (auto S = dyn_cast<WasmYAML::StartSection>(Sec.get())) { |
| 547 | if (auto Err = writeSectionContent(StringStream, *S)) |
| 548 | return Err; |
| 549 | } else if (auto S = dyn_cast<WasmYAML::ElemSection>(Sec.get())) { |
| 550 | if (auto Err = writeSectionContent(StringStream, *S)) |
| 551 | return Err; |
| 552 | } else if (auto S = dyn_cast<WasmYAML::CodeSection>(Sec.get())) { |
| 553 | if (auto Err = writeSectionContent(StringStream, *S)) |
| 554 | return Err; |
| 555 | } else if (auto S = dyn_cast<WasmYAML::DataSection>(Sec.get())) { |
| 556 | if (auto Err = writeSectionContent(StringStream, *S)) |
| 557 | return Err; |
| 558 | } else { |
| 559 | errs() << "Unknown section type: " << Sec->Type << "\n"; |
| 560 | return 1; |
| 561 | } |
| 562 | StringStream.flush(); |
| 563 | |
| 564 | // Write the section size followed by the content |
| 565 | encodeULEB128(OutString.size(), OS); |
| 566 | OS << OutString; |
| 567 | } |
| 568 | |
| 569 | // write reloc sections for any section that have relocations |
Sam Clegg | 6f08c84 | 2018-04-24 18:11:36 +0000 | [diff] [blame] | 570 | uint32_t SectionIndex = 0; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 571 | for (const std::unique_ptr<WasmYAML::Section> &Sec : Obj.Sections) { |
Sam Clegg | 6f08c84 | 2018-04-24 18:11:36 +0000 | [diff] [blame] | 572 | if (Sec->Relocations.empty()) { |
| 573 | SectionIndex++; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 574 | continue; |
Sam Clegg | 6f08c84 | 2018-04-24 18:11:36 +0000 | [diff] [blame] | 575 | } |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 576 | |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 577 | writeUint8(OS, wasm::WASM_SEC_CUSTOM); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 578 | std::string OutString; |
| 579 | raw_string_ostream StringStream(OutString); |
Sam Clegg | 6f08c84 | 2018-04-24 18:11:36 +0000 | [diff] [blame] | 580 | writeRelocSection(StringStream, *Sec, SectionIndex++); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 581 | StringStream.flush(); |
| 582 | |
| 583 | encodeULEB128(OutString.size(), OS); |
| 584 | OS << OutString; |
| 585 | } |
| 586 | |
| 587 | return 0; |
| 588 | } |
| 589 | |
| 590 | int yaml2wasm(llvm::WasmYAML::Object &Doc, raw_ostream &Out) { |
| 591 | WasmWriter Writer(Doc); |
| 592 | |
| 593 | return Writer.writeWasm(Out); |
| 594 | } |