srctree

Robin Linden parent 7ce7b0f8 99d40be8
wasm: Simplify ValueType

Now that ValueType::parse lives in ByteCodeParser, this can be turnedinto a enum class again.

inlinesplit
wasm/byte_code_parser.cpp added: 63, removed: 77, total 0
@@ -57,19 +57,19 @@ std::optional<ValueType> parse(std::istream &is) {
 
switch (byte) {
case 0x7f:
return ValueType{ValueType::Kind::Int32};
return ValueType::Int32;
case 0x7e:
return ValueType{ValueType::Kind::Int64};
return ValueType::Int64;
case 0x7d:
return ValueType{ValueType::Kind::Float32};
return ValueType::Float32;
case 0x7c:
return ValueType{ValueType::Kind::Float64};
return ValueType::Float64;
case 0x7b:
return ValueType{ValueType::Kind::Vector128};
return ValueType::Vector128;
case 0x70:
return ValueType{ValueType::Kind::FunctionReference};
return ValueType::FunctionReference;
case 0x6f:
return ValueType{ValueType::Kind::ExternReference};
return ValueType::ExternReference;
default:
return std::nullopt;
}
@@ -162,9 +162,7 @@ std::optional<FunctionType> parse(std::istream &is) {
template<>
std::optional<TableType> parse(std::istream &is) {
auto element_type = parse<ValueType>(is);
if (!element_type
|| (element_type->kind != ValueType::Kind::FunctionReference
&& element_type->kind != ValueType::Kind::ExternReference)) {
if (!element_type || (element_type != ValueType::FunctionReference && element_type != ValueType::ExternReference)) {
return std::nullopt;
}
 
 
wasm/byte_code_parser_test.cpp added: 63, removed: 77, total 0
@@ -200,7 +200,7 @@ void table_section_tests() {
expect_eq(module.table_section,
wasm::TableSection{.tables{
wasm::TableType{
wasm::ValueType{wasm::ValueType::FunctionReference},
wasm::ValueType::FunctionReference,
wasm::Limits{.min = 42},
},
}});
@@ -217,7 +217,7 @@ void table_section_tests() {
expect_eq(module.table_section,
wasm::TableSection{.tables{
wasm::TableType{
wasm::ValueType{wasm::ValueType::ExternReference},
wasm::ValueType::ExternReference,
wasm::Limits{.min = 42, .max = 42},
},
}});
@@ -323,7 +323,7 @@ void global_section_tests() {
.value();
expect_eq(module.global_section,
wasm::GlobalSection{.globals{{
.type{wasm::ValueType{wasm::ValueType::Int32}, wasm::GlobalType::Mutability::Const},
.type{wasm::ValueType::Int32, wasm::GlobalType::Mutability::Const},
.init{wasm::instructions::I32Const{42}},
}}});
});
@@ -333,7 +333,7 @@ void global_section_tests() {
.value();
expect_eq(module.global_section,
wasm::GlobalSection{.globals{{
.type{wasm::ValueType{wasm::ValueType::Int32}, wasm::GlobalType::Mutability::Var},
.type{wasm::ValueType::Int32, wasm::GlobalType::Mutability::Var},
.init{wasm::instructions::I32Const{42}},
}}});
});
@@ -345,11 +345,11 @@ void global_section_tests() {
expect_eq(module.global_section,
wasm::GlobalSection{.globals{
{
.type{wasm::ValueType{wasm::ValueType::Int32}, wasm::GlobalType::Mutability::Var},
.type{wasm::ValueType::Int32, wasm::GlobalType::Mutability::Var},
.init{wasm::instructions::I32Const{42}},
},
{
.type{wasm::ValueType{wasm::ValueType::Int32}, wasm::GlobalType::Mutability::Const},
.type{wasm::ValueType::Int32, wasm::GlobalType::Mutability::Const},
.init{wasm::instructions::I32Const{42}},
},
}});
@@ -403,11 +403,10 @@ void type_section_tests() {
expect_eq(module.type_section,
wasm::TypeSection{
.types{
wasm::FunctionType{.results{wasm::ValueType{wasm::ValueType::Int32}}},
wasm::FunctionType{.results{wasm::ValueType::Int32}},
wasm::FunctionType{
.parameters{wasm::ValueType{wasm::ValueType::Int32},
wasm::ValueType{wasm::ValueType::Int32}},
.results{wasm::ValueType{wasm::ValueType::Float64}},
.parameters{wasm::ValueType::Int32, wasm::ValueType::Int32},
.results{wasm::ValueType::Float64},
},
},
});
@@ -423,13 +422,13 @@ void type_section_tests() {
.types{
wasm::FunctionType{
.parameters{
wasm::ValueType{wasm::ValueType::Int32},
wasm::ValueType{wasm::ValueType::Int64},
wasm::ValueType{wasm::ValueType::Float32},
wasm::ValueType{wasm::ValueType::Float64},
wasm::ValueType{wasm::ValueType::Vector128},
wasm::ValueType{wasm::ValueType::FunctionReference},
wasm::ValueType{wasm::ValueType::ExternReference},
wasm::ValueType::Int32,
wasm::ValueType::Int64,
wasm::ValueType::Float32,
wasm::ValueType::Float64,
wasm::ValueType::Vector128,
wasm::ValueType::FunctionReference,
wasm::ValueType::ExternReference,
},
},
},
@@ -481,7 +480,7 @@ void code_section_tests() {
wasm::CodeSection expected{.entries{
wasm::CodeEntry{
.code{wasm::instructions::I32Const{0b11}, wasm::instructions::I32PopulationCount{}},
.locals{{1, wasm::ValueType{wasm::ValueType::Int32}}},
.locals{{1, wasm::ValueType::Int32}},
},
}};
expect_eq(module.code_section, expected);
@@ -495,12 +494,11 @@ void code_section_tests() {
wasm::CodeSection expected{.entries{
wasm::CodeEntry{
.code{wasm::instructions::I32Const{42}},
.locals{{1, wasm::ValueType{wasm::ValueType::Int32}}},
.locals{{1, wasm::ValueType::Int32}},
},
wasm::CodeEntry{
.code{},
.locals{{5, wasm::ValueType{wasm::ValueType::Int64}},
{6, wasm::ValueType{wasm::ValueType::Float32}}},
.locals{{5, wasm::ValueType::Int64}, {6, wasm::ValueType::Float32}},
},
}};
expect_eq(module.code_section, expected);
 
wasm/instructions_test.cpp added: 63, removed: 77, total 0
@@ -34,7 +34,7 @@ int main() {
// No instructions, empty function prototype.
a.expect_eq(parse("\x02\x40\x0b\x0b"), InsnVec{Block{.type{BlockType::Empty{}}}});
// No instructions, function returning an f32.
a.expect_eq(parse("\x02\x7d\x0b\x0b"), InsnVec{Block{.type{wasm::ValueType{wasm::ValueType::Kind::Float32}}}});
a.expect_eq(parse("\x02\x7d\x0b\x0b"), InsnVec{Block{.type{wasm::ValueType::Float32}}});
// Return, empty function prototype.
a.expect_eq(parse("\x02\x40\x0f\x0b\x0b"), InsnVec{Block{.type{BlockType::Empty{}}, .instructions{Return{}}}});
 
@@ -49,7 +49,7 @@ int main() {
// No instructions, empty function prototype.
a.expect_eq(parse("\x03\x40\x0b\x0b"), InsnVec{Loop{.type{BlockType::Empty{}}}});
// No instructions, function returning an f32.
a.expect_eq(parse("\x03\x7d\x0b\x0b"), InsnVec{Loop{.type{wasm::ValueType{wasm::ValueType::Kind::Float32}}}});
a.expect_eq(parse("\x03\x7d\x0b\x0b"), InsnVec{Loop{.type{wasm::ValueType::Float32}}});
// Return, empty function prototype.
a.expect_eq(parse("\x03\x40\x0f\x0b\x0b"), InsnVec{Loop{.type{BlockType::Empty{}}, .instructions{Return{}}}});
 
 
wasm/serialize.h added: 63, removed: 77, total 0
@@ -18,21 +18,21 @@
 
namespace wasm {
 
constexpr std::string_view to_string(ValueType const &vt) {
switch (vt.kind) {
case ValueType::Kind::Int32:
constexpr std::string_view to_string(ValueType vt) {
switch (vt) {
case ValueType::Int32:
return "i32";
case ValueType::Kind::Int64:
case ValueType::Int64:
return "i64";
case ValueType::Kind::Float32:
case ValueType::Float32:
return "f32";
case ValueType::Kind::Float64:
case ValueType::Float64:
return "f64";
case ValueType::Kind::Vector128:
case ValueType::Vector128:
return "v128";
case ValueType::Kind::FunctionReference:
case ValueType::FunctionReference:
return "funcref";
case ValueType::Kind::ExternReference:
case ValueType::ExternReference:
return "externref";
}
 
 
wasm/serialize_test.cpp added: 63, removed: 77, total 0
@@ -14,14 +14,12 @@ int main() {
using namespace wasm::instructions;
 
s.add_test("block", [](etest::IActions &a) {
a.expect_eq(to_string(Block{.type{wasm::ValueType{wasm::ValueType::Kind::Int32}},
.instructions{I32Const{2}, I32Const{2}, I32Add{}}}),
a.expect_eq(to_string(Block{.type{wasm::ValueType::Int32}, .instructions{I32Const{2}, I32Const{2}, I32Add{}}}),
"block (result i32) \n\ti32.const 2\n\ti32.const 2\n\ti32.add\nend");
a.expect_eq(to_string(Block{.type{wasm::TypeIdx{7}}, .instructions{I32Const{2}, I32Const{2}, I32Add{}}}),
"block (type 7) \n\ti32.const 2\n\ti32.const 2\n\ti32.add\nend");
a.expect_eq(to_string(Block{.type{wasm::ValueType{wasm::ValueType::Kind::Int32}},
.instructions{Block{.type{wasm::ValueType{wasm::ValueType::Kind::Int32}},
.instructions{I32Const{8}}},
a.expect_eq(to_string(Block{.type{wasm::ValueType::Int32},
.instructions{Block{.type{wasm::ValueType::Int32}, .instructions{I32Const{8}}},
I32Const{2},
I32Const{2},
I32Add{}}}),
@@ -30,14 +28,12 @@ int main() {
});
 
s.add_test("loop", [](etest::IActions &a) {
a.expect_eq(to_string(Loop{.type{wasm::ValueType{wasm::ValueType::Kind::Int32}},
.instructions{I32Const{2}, I32Const{2}, I32Add{}}}),
a.expect_eq(to_string(Loop{.type{wasm::ValueType::Int32}, .instructions{I32Const{2}, I32Const{2}, I32Add{}}}),
"loop (result i32) \n\ti32.const 2\n\ti32.const 2\n\ti32.add\nend");
a.expect_eq(to_string(Loop{.type{wasm::TypeIdx{7}}, .instructions{I32Const{2}, I32Const{2}, I32Add{}}}),
"loop (type 7) \n\ti32.const 2\n\ti32.const 2\n\ti32.add\nend");
a.expect_eq(to_string(Loop{.type{wasm::ValueType{wasm::ValueType::Kind::Int32}},
.instructions{Loop{.type{wasm::ValueType{wasm::ValueType::Kind::Int32}},
.instructions{I32Const{8}}},
a.expect_eq(to_string(Loop{.type{wasm::ValueType::Int32},
.instructions{Loop{.type{wasm::ValueType::Int32}, .instructions{I32Const{8}}},
I32Const{2},
I32Const{2},
I32Add{}}}),
 
wasm/types.h added: 63, removed: 77, total 0
@@ -16,25 +16,19 @@ 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,
enum class ValueType {
// Number types.
Int32,
Int64,
Float32,
Float64,
 
// Vector types.
Vector128,
// Vector types.
Vector128,
 
// Reference types.
FunctionReference,
ExternReference,
};
 
Kind kind{};
 
[[nodiscard]] bool operator==(ValueType const &) const = default;
// Reference types.
FunctionReference,
ExternReference,
};
 
// https://webassembly.github.io/spec/core/binary/types.html#result-types
 
wasm/wasm_example.cpp added: 63, removed: 77, total 0
@@ -16,7 +16,7 @@
namespace wasm {
std::ostream &operator<<(std::ostream &, wasm::ValueType);
std::ostream &operator<<(std::ostream &os, wasm::ValueType type) {
switch (type.kind) {
switch (type) {
case ValueType::Int32:
os << "i32";
break;