xref: /freebsd/contrib/llvm-project/llvm/lib/ObjCopy/wasm/WasmWriter.h (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
1*81ad6265SDimitry Andric //===- WasmWriter.h ---------------------------------------------*- C++ -*-===//
2*81ad6265SDimitry Andric //
3*81ad6265SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*81ad6265SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*81ad6265SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*81ad6265SDimitry Andric //
7*81ad6265SDimitry Andric //===----------------------------------------------------------------------===//
8*81ad6265SDimitry Andric 
9*81ad6265SDimitry Andric #ifndef LLVM_LIB_OBJCOPY_WASM_WASMWRITER_H
10*81ad6265SDimitry Andric #define LLVM_LIB_OBJCOPY_WASM_WASMWRITER_H
11*81ad6265SDimitry Andric 
12*81ad6265SDimitry Andric #include "WasmObject.h"
13*81ad6265SDimitry Andric #include <cstdint>
14*81ad6265SDimitry Andric #include <vector>
15*81ad6265SDimitry Andric 
16*81ad6265SDimitry Andric namespace llvm {
17*81ad6265SDimitry Andric namespace objcopy {
18*81ad6265SDimitry Andric namespace wasm {
19*81ad6265SDimitry Andric 
20*81ad6265SDimitry Andric class Writer {
21*81ad6265SDimitry Andric public:
Writer(Object & Obj,raw_ostream & Out)22*81ad6265SDimitry Andric   Writer(Object &Obj, raw_ostream &Out) : Obj(Obj), Out(Out) {}
23*81ad6265SDimitry Andric   Error write();
24*81ad6265SDimitry Andric 
25*81ad6265SDimitry Andric private:
26*81ad6265SDimitry Andric   using SectionHeader = SmallVector<char, 8>;
27*81ad6265SDimitry Andric   Object &Obj;
28*81ad6265SDimitry Andric   raw_ostream &Out;
29*81ad6265SDimitry Andric   std::vector<SectionHeader> SectionHeaders;
30*81ad6265SDimitry Andric 
31*81ad6265SDimitry Andric   /// Generate a wasm section section header for S.
32*81ad6265SDimitry Andric   /// The header consists of
33*81ad6265SDimitry Andric   /// * A one-byte section ID (aka the section type).
34*81ad6265SDimitry Andric   /// * The size of the section contents, encoded as ULEB128.
35*81ad6265SDimitry Andric   /// * If the section is a custom section (type 0) it also has a name, which is
36*81ad6265SDimitry Andric   ///   encoded as a length-prefixed string. The encoded section size *includes*
37*81ad6265SDimitry Andric   ///   this string.
38*81ad6265SDimitry Andric   /// See https://webassembly.github.io/spec/core/binary/modules.html#sections
39*81ad6265SDimitry Andric   /// Return the header and store the total size in SectionSize.
40*81ad6265SDimitry Andric   static SectionHeader createSectionHeader(const Section &S,
41*81ad6265SDimitry Andric                                            size_t &SectionSize);
42*81ad6265SDimitry Andric   size_t finalize();
43*81ad6265SDimitry Andric };
44*81ad6265SDimitry Andric 
45*81ad6265SDimitry Andric } // end namespace wasm
46*81ad6265SDimitry Andric } // end namespace objcopy
47*81ad6265SDimitry Andric } // end namespace llvm
48*81ad6265SDimitry Andric 
49*81ad6265SDimitry Andric #endif // LLVM_LIB_OBJCOPY_WASM_WASMWRITER_H
50