From a09ea699d69ed54741d95b9b5fa2247bdcf28152 Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Wed, 25 Oct 2023 20:55:27 +0200 Subject: [analysis] Implement an Int lattice (#6037) Implement a generic lattice template for integral types ordered by `<`. --- src/analysis/lattices/int.h | 63 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/analysis/lattices/int.h (limited to 'src') diff --git a/src/analysis/lattices/int.h b/src/analysis/lattices/int.h new file mode 100644 index 000000000..a2cd37c1e --- /dev/null +++ b/src/analysis/lattices/int.h @@ -0,0 +1,63 @@ +/* + * 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_int_h +#define wasm_analysis_lattices_int_h + +#include +#include + +#include "../lattice.h" + +namespace wasm::analysis { + +// The lattice of integers of the given type `T`, ordered by <. The min integer +// is the bottom element and the max integer is the top element. +#if __cplusplus >= 202002L +template +#else +template +#endif +struct Integer { + using Element = T; + Element getBottom() const noexcept { return std::numeric_limits::min(); } + 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; + } +}; + +using Int32 = Integer; +using UInt32 = Integer; +using Int64 = Integer; +using UInt64 = Integer; + +#if __cplusplus >= 202002L +static_assert(Lattice); +static_assert(Lattice); +static_assert(Lattice); +static_assert(Lattice); +#endif // __cplusplus >= 202002L + +} // namespace wasm::analysis + +#endif // wasm_analysis_lattices_int_h -- cgit v1.2.3