Skip to content

Commit

Permalink
[analysis] Implement a Bool lattice
Browse files Browse the repository at this point in the history
This is a lattice with two elements: `false` is bottom and `true` is top.

Add a new gtest file for testing lattices.
  • Loading branch information
tlively committed Oct 21, 2023
1 parent 8c624ec commit 9003cc3
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/analysis/lattices/bool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2023 WebAssembly Community Group participants
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef wasm_analysis_lattices_bool_h
#define wasm_analysis_lattices_bool_h

#include "../lattice.h"

namespace wasm::analysis {

struct Bool {
using Element = bool;
Element getBottom() const noexcept { return false; }
LatticeComparison compare(Element a, Element b) const noexcept {
return a > b ? GREATER : a == b ? EQUAL : LESS;
}
bool join(Element& self, Element other) const noexcept {
if (!self && other) {
self = other;
return true;
}
return false;
}
};

#if __cplusplus >= 202002L
static_assert(Lattice<Bool>);
#endif // __cplusplus >= 202002L

} // namespace wasm::analysis

#endif // wasm_analysis_lattices_bool_h
1 change: 1 addition & 0 deletions test/gtest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ include_directories(../../src/wasm)
set(unittest_SOURCES
cfg.cpp
dfa_minimization.cpp
lattices.cpp
possible-contents.cpp
printing.cpp
stringify.cpp
Expand Down
50 changes: 50 additions & 0 deletions test/gtest/lattices.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2023 WebAssembly Community Group participants
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "analysis/lattices/bool.h"
#include "gtest/gtest.h"

using namespace wasm;

TEST(BoolLattice, GetBottom) {
analysis::Bool lattice;
EXPECT_FALSE(lattice.getBottom());
}

TEST(BoolLattice, Compare) {
analysis::Bool lattice;
EXPECT_EQ(lattice.compare(false, false), analysis::EQUAL);
EXPECT_EQ(lattice.compare(false, true), analysis::LESS);
EXPECT_EQ(lattice.compare(true, false), analysis::GREATER);
EXPECT_EQ(lattice.compare(true, true), analysis::EQUAL);
}

TEST(BoolLattice, Join) {
analysis::Bool lattice;
bool elem = false;

EXPECT_FALSE(lattice.join(elem, false));
ASSERT_FALSE(elem);

EXPECT_TRUE(lattice.join(elem, true));
ASSERT_TRUE(elem);

EXPECT_FALSE(lattice.join(elem, false));
ASSERT_TRUE(elem);

EXPECT_FALSE(lattice.join(elem, true));
ASSERT_TRUE(elem);
}

0 comments on commit 9003cc3

Please sign in to comment.