srctree

Robin Linden parent 384e3556 201bc74f
wasm: Keep non-section types in a file of their own

inlinesplit
wasm/byte_code_parser.cpp added: 92, removed: 70, total 22
@@ -5,6 +5,7 @@
#include "wasm/byte_code_parser.h"
 
#include "wasm/leb128.h"
#include "wasm/types.h"
#include "wasm/wasm.h"
 
#include <tl/expected.hpp>
 
wasm/byte_code_parser.h added: 92, removed: 70, total 22
@@ -2,6 +2,7 @@
//
// SPDX-License-Identifier: BSD-2-Clause
 
#include "wasm/types.h"
#include "wasm/wasm.h"
 
#include <tl/expected.hpp>
 
wasm/byte_code_parser_test.cpp added: 92, removed: 70, total 22
@@ -4,6 +4,7 @@
 
#include "wasm/byte_code_parser.h"
 
#include "wasm/types.h"
#include "wasm/wasm.h"
 
#include "etest/etest.h"
 
wasm/instructions.h added: 92, removed: 70, total 22
@@ -6,7 +6,7 @@
#ifndef WASM_INSTRUCTIONS_H_
#define WASM_INSTRUCTIONS_H_
 
#include "wasm/wasm.h"
#include "wasm/types.h"
 
#include <cstdint>
#include <iosfwd>
 
wasm/instructions_test.cpp added: 92, removed: 70, total 22
@@ -5,7 +5,7 @@
 
#include "wasm/instructions.h"
 
#include "wasm/wasm.h"
#include "wasm/types.h"
 
#include "etest/etest2.h"
 
 
wasm/serialize.h added: 92, removed: 70, total 22
@@ -6,7 +6,7 @@
#define WASM_SERIALIZE_H
 
#include "wasm/instructions.h"
#include "wasm/wasm.h"
#include "wasm/types.h"
 
#include <cassert>
#include <cstddef>
 
wasm/serialize_test.cpp added: 92, removed: 70, total 22
@@ -4,7 +4,7 @@
 
#include "wasm/instructions.h"
#include "wasm/serialize.h"
#include "wasm/wasm.h"
#include "wasm/types.h"
 
#include "etest/etest2.h"
 
 
filename was Deleted added: 92, removed: 70, total 22
@@ -0,0 +1,72 @@
// SPDX-FileCopyrightText: 2023-2024 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
#ifndef WASM_TYPES_H_
#define WASM_TYPES_H_
 
#include <cstdint>
#include <optional>
#include <vector>
 
namespace wasm {
 
// https://webassembly.github.io/spec/core/binary/modules.html#indices
using TypeIdx = std::uint32_t;
using FuncIdx = std::uint32_t;
 
// https://webassembly.github.io/spec/core/syntax/types.html
struct ValueType {
enum Kind : std::uint8_t {
// Number types.
Int32,
Int64,
Float32,
Float64,
 
// Vector types.
Vector128,
 
// Reference types.
FunctionReference,
ExternReference,
};
 
Kind kind{};
 
[[nodiscard]] bool operator==(ValueType const &) const = default;
};
 
// https://webassembly.github.io/spec/core/binary/types.html#result-types
using ResultType = std::vector<ValueType>;
 
// https://webassembly.github.io/spec/core/binary/types.html#function-types
struct FunctionType {
ResultType parameters;
ResultType results;
 
[[nodiscard]] bool operator==(FunctionType const &) const = default;
};
 
// https://webassembly.github.io/spec/core/binary/types.html#limits
struct Limits {
std::uint32_t min{};
std::optional<std::uint32_t> max{};
 
[[nodiscard]] bool operator==(Limits const &) const = default;
};
 
// https://webassembly.github.io/spec/core/binary/types.html#table-types
struct TableType {
ValueType element_type{};
Limits limits{};
 
[[nodiscard]] bool operator==(TableType const &) const = default;
};
 
// https://webassembly.github.io/spec/core/binary/types.html#memory-types
using MemType = Limits;
 
} // namespace wasm
 
#endif
 
wasm/wasm.h added: 92, removed: 70, total 22
@@ -5,6 +5,8 @@
#ifndef WASM_WASM_H_
#define WASM_WASM_H_
 
#include "wasm/types.h"
 
#include <cstdint>
#include <optional>
#include <string>
@@ -12,43 +14,6 @@
 
namespace wasm {
 
// https://webassembly.github.io/spec/core/binary/modules.html#indices
using TypeIdx = std::uint32_t;
using FuncIdx = std::uint32_t;
 
// https://webassembly.github.io/spec/core/syntax/types.html
struct ValueType {
enum Kind : std::uint8_t {
// Number types.
Int32,
Int64,
Float32,
Float64,
 
// Vector types.
Vector128,
 
// Reference types.
FunctionReference,
ExternReference,
};
 
Kind kind{};
 
[[nodiscard]] bool operator==(ValueType const &) const = default;
};
 
// https://webassembly.github.io/spec/core/binary/types.html#result-types
using ResultType = std::vector<ValueType>;
 
// https://webassembly.github.io/spec/core/binary/types.html#function-types
struct FunctionType {
ResultType parameters;
ResultType results;
 
[[nodiscard]] bool operator==(FunctionType const &) const = default;
};
 
// https://webassembly.github.io/spec/core/binary/modules.html#type-section
struct TypeSection {
std::vector<FunctionType> types;
@@ -63,22 +28,6 @@ struct FunctionSection {
[[nodiscard]] bool operator==(FunctionSection const &) const = default;
};
 
// https://webassembly.github.io/spec/core/binary/types.html#limits
struct Limits {
std::uint32_t min{};
std::optional<std::uint32_t> max{};
 
[[nodiscard]] bool operator==(Limits const &) const = default;
};
 
// https://webassembly.github.io/spec/core/binary/types.html#table-types
struct TableType {
ValueType element_type{};
Limits limits{};
 
[[nodiscard]] bool operator==(TableType const &) const = default;
};
 
// https://webassembly.github.io/spec/core/binary/modules.html#table-section
struct TableSection {
std::vector<TableType> tables{};
@@ -86,9 +35,6 @@ struct TableSection {
[[nodiscard]] bool operator==(TableSection const &) const = default;
};
 
// https://webassembly.github.io/spec/core/binary/types.html#memory-types
using MemType = Limits;
 
// https://webassembly.github.io/spec/core/binary/modules.html#memory-section
struct MemorySection {
std::vector<MemType> memories{};
 
wasm/wasm_example.cpp added: 92, removed: 70, total 22
@@ -4,6 +4,7 @@
 
#include "wasm/byte_code_parser.h"
#include "wasm/instructions.h"
#include "wasm/types.h"
#include "wasm/wasm.h"
 
#include <algorithm>