Skip to content

Commit 28d77bf

Browse files
committed
[analysis] Implement an Int lattice
Implement a generic lattice template for integral types ordered by `<`.
1 parent 9003cc3 commit 28d77bf

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

src/analysis/lattices/int.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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_int_h
18+
#define wasm_analysis_lattices_int_h
19+
20+
#include <cstdint>
21+
#include <limits>
22+
23+
#include "../lattice.h"
24+
25+
namespace wasm::analysis {
26+
27+
#if __cplusplus >= 202002L
28+
template<std::integral T>
29+
#else
30+
template<typename T>
31+
#endif
32+
struct Integer {
33+
using Element = T;
34+
Element getBottom() const noexcept { return std::numeric_limits<T>::min(); }
35+
LatticeComparison compare(Element a, Element b) const noexcept {
36+
return a > b ? GREATER : a == b ? EQUAL : LESS;
37+
}
38+
bool join(Element& self, Element other) const noexcept {
39+
if (self < other) {
40+
self = other;
41+
return true;
42+
}
43+
return false;
44+
}
45+
};
46+
47+
using Int32 = Integer<int32_t>;
48+
using UInt32 = Integer<uint32_t>;
49+
using Int64 = Integer<int64_t>;
50+
using UInt64 = Integer<uint64_t>;
51+
52+
#if __cplusplus >= 202002L
53+
static_assert(Lattice<Int32>);
54+
static_assert(Lattice<Int64>);
55+
static_assert(Lattice<UInt32>);
56+
static_assert(Lattice<UInt64>);
57+
#endif // __cplusplus >= 202002L
58+
59+
} // namespace wasm::analysis
60+
61+
#endif // wasm_analysis_lattices_int_h

test/gtest/lattices.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
#include "analysis/lattices/bool.h"
18+
#include "analysis/lattices/int.h"
1819
#include "gtest/gtest.h"
1920

2021
using namespace wasm;
@@ -48,3 +49,38 @@ TEST(BoolLattice, Join) {
4849
EXPECT_FALSE(lattice.join(elem, true));
4950
ASSERT_TRUE(elem);
5051
}
52+
53+
TEST(IntLattice, GetBottom) {
54+
analysis::Int32 int32;
55+
EXPECT_EQ(int32.getBottom(), (int32_t)(1ll << 31));
56+
57+
analysis::Int64 int64;
58+
EXPECT_EQ(int64.getBottom(), (int64_t)(1ll << 63));
59+
60+
analysis::UInt32 uint32;
61+
EXPECT_EQ(uint32.getBottom(), (uint32_t)0);
62+
63+
analysis::UInt64 uint64;
64+
EXPECT_EQ(uint64.getBottom(), (uint32_t)0);
65+
}
66+
67+
TEST(IntLattice, Compare) {
68+
analysis::Int32 int32;
69+
EXPECT_EQ(int32.compare(-5, 42), analysis::LESS);
70+
EXPECT_EQ(int32.compare(42, -5), analysis::GREATER);
71+
EXPECT_EQ(int32.compare(42, 42), analysis::EQUAL);
72+
}
73+
74+
TEST(IntLattice, Join) {
75+
analysis::Int32 int32;
76+
int elem = 0;
77+
78+
EXPECT_FALSE(int32.join(elem, -10));
79+
ASSERT_EQ(elem, 0);
80+
81+
EXPECT_FALSE(int32.join(elem, 0));
82+
ASSERT_EQ(elem, 0);
83+
84+
EXPECT_TRUE(int32.join(elem, 100));
85+
ASSERT_EQ(elem, 100);
86+
}

0 commit comments

Comments
 (0)