Skip to content

Commit b4e101a

Browse files
committed
[analysis] Implement a Bool lattice
This is a lattice with two elements: `false` is bottom and `true` is top. Add a new gtest file for testing lattices.
1 parent 2337d40 commit b4e101a

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

src/analysis/lattices/bool.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2023 WebAssembly Community Group participants
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef wasm_analysis_lattices_bool_h
18+
#define wasm_analysis_lattices_bool_h
19+
20+
#include "../lattice.h"
21+
22+
namespace wasm::analysis {
23+
24+
struct Bool {
25+
using Element = bool;
26+
Element getBottom() const noexcept { return false; }
27+
LatticeComparison compare(Element a, Element b) const noexcept {
28+
return a > b ? GREATER : a == b ? EQUAL : LESS;
29+
}
30+
bool join(Element& self, Element other) const noexcept {
31+
if (!self && other) {
32+
self = other;
33+
return true;
34+
}
35+
return false;
36+
}
37+
};
38+
39+
#if __cplusplus >= 202002L
40+
static_assert(Lattice<Bool>);
41+
#endif // __cplusplus >= 202002L
42+
43+
} // namespace wasm::analysis
44+
45+
#endif // wasm_analysis_lattices_bool_h

test/gtest/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ include_directories(../../src/wasm)
44
set(unittest_SOURCES
55
cfg.cpp
66
dfa_minimization.cpp
7+
lattices.cpp
78
possible-contents.cpp
89
printing.cpp
910
stringify.cpp

test/gtest/lattices.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2023 WebAssembly Community Group participants
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "analysis/lattices/bool.h"
18+
#include "gtest/gtest.h"
19+
20+
using namespace wasm;
21+
22+
TEST(BoolLattice, GetBottom) {
23+
analysis::Bool lattice;
24+
EXPECT_FALSE(lattice.getBottom());
25+
}
26+
27+
TEST(BoolLattice, Compare) {
28+
analysis::Bool lattice;
29+
EXPECT_EQ(lattice.compare(false, false), analysis::EQUAL);
30+
EXPECT_EQ(lattice.compare(false, true), analysis::LESS);
31+
EXPECT_EQ(lattice.compare(true, false), analysis::GREATER);
32+
EXPECT_EQ(lattice.compare(true, true), analysis::EQUAL);
33+
}
34+
35+
TEST(BoolLattice, Join) {
36+
analysis::Bool lattice;
37+
bool elem = false;
38+
39+
EXPECT_FALSE(lattice.join(elem, false));
40+
ASSERT_FALSE(elem);
41+
42+
EXPECT_TRUE(lattice.join(elem, true));
43+
ASSERT_TRUE(elem);
44+
45+
EXPECT_FALSE(lattice.join(elem, false));
46+
ASSERT_TRUE(elem);
47+
48+
EXPECT_FALSE(lattice.join(elem, true));
49+
ASSERT_TRUE(elem);
50+
}

0 commit comments

Comments
 (0)