summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWasm/Printer/Printer.h
blob: a3aa735a628e20e4d7ad98e04cbe5740c0e1f1cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
 * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <LibCore/Stream.h>
#include <LibWasm/Types.h>

namespace Wasm {

class Reference;
class Value;

DeprecatedString instruction_name(OpCode const& opcode);
Optional<OpCode> instruction_from_name(StringView name);

struct Printer {
    explicit Printer(Core::Stream::Stream& stream, size_t initial_indent = 0)
        : m_stream(stream)
        , m_indent(initial_indent)
    {
    }

    void print(Wasm::BlockType const&);
    void print(Wasm::CodeSection const&);
    void print(Wasm::CodeSection::Code const&);
    void print(Wasm::CodeSection::Func const&);
    void print(Wasm::CustomSection const&);
    void print(Wasm::DataCountSection const&);
    void print(Wasm::DataSection const&);
    void print(Wasm::DataSection::Data const&);
    void print(Wasm::ElementSection const&);
    void print(Wasm::ElementSection::Element const&);
    void print(Wasm::ExportSection const&);
    void print(Wasm::ExportSection::Export const&);
    void print(Wasm::Expression const&);
    void print(Wasm::FunctionSection const&);
    void print(Wasm::FunctionType const&);
    void print(Wasm::GlobalSection const&);
    void print(Wasm::GlobalSection::Global const&);
    void print(Wasm::GlobalType const&);
    void print(Wasm::ImportSection const&);
    void print(Wasm::ImportSection::Import const&);
    void print(Wasm::Instruction const&);
    void print(Wasm::Limits const&);
    void print(Wasm::Locals const&);
    void print(Wasm::MemorySection const&);
    void print(Wasm::MemorySection::Memory const&);
    void print(Wasm::MemoryType const&);
    void print(Wasm::Module const&);
    void print(Wasm::Module::Function const&);
    void print(Wasm::Reference const&);
    void print(Wasm::StartSection const&);
    void print(Wasm::StartSection::StartFunction const&);
    void print(Wasm::TableSection const&);
    void print(Wasm::TableSection::Table const&);
    void print(Wasm::TableType const&);
    void print(Wasm::TypeSection const&);
    void print(Wasm::ValueType const&);
    void print(Wasm::Value const&);

private:
    void print_indent();
    template<typename... Args>
    void print(CheckedFormatString<Args...> fmt, Args&&... args)
    {
        StringBuilder builder;
        builder.appendff(fmt.view(), forward<Args>(args)...);
        m_stream.write_entire_buffer(builder.string_view().bytes()).release_value_but_fixme_should_propagate_errors();
    }

    Core::Stream::Stream& m_stream;
    size_t m_indent { 0 };
};

}