[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
//===--- Boolean.h - Wrapper for boolean types for the VM -------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_AST_INTERP_BOOLEAN_H
|
|
|
|
#define LLVM_CLANG_AST_INTERP_BOOLEAN_H
|
|
|
|
|
|
|
|
#include "Integral.h"
|
|
|
|
#include "clang/AST/APValue.h"
|
|
|
|
#include "clang/AST/ComparisonCategories.h"
|
|
|
|
#include "llvm/ADT/APSInt.h"
|
|
|
|
#include "llvm/Support/MathExtras.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2024-08-16 17:13:12 +02:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace interp {
|
|
|
|
|
|
|
|
/// Wrapper around boolean types.
|
2022-09-17 15:14:32 +02:00
|
|
|
class Boolean final {
|
2024-08-16 17:13:12 +02:00
|
|
|
private:
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
/// Underlying boolean.
|
|
|
|
bool V;
|
|
|
|
|
2024-08-16 17:13:12 +02:00
|
|
|
public:
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
/// Zero-initializes a boolean.
|
|
|
|
Boolean() : V(false) {}
|
2024-09-29 13:31:44 +02:00
|
|
|
Boolean(const llvm::APSInt &I) : V(!I.isZero()) {}
|
2023-01-25 14:51:16 +01:00
|
|
|
explicit Boolean(bool V) : V(V) {}
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
|
|
|
|
bool operator<(Boolean RHS) const { return V < RHS.V; }
|
|
|
|
bool operator>(Boolean RHS) const { return V > RHS.V; }
|
|
|
|
bool operator<=(Boolean RHS) const { return V <= RHS.V; }
|
|
|
|
bool operator>=(Boolean RHS) const { return V >= RHS.V; }
|
|
|
|
bool operator==(Boolean RHS) const { return V == RHS.V; }
|
|
|
|
bool operator!=(Boolean RHS) const { return V != RHS.V; }
|
|
|
|
|
|
|
|
bool operator>(unsigned RHS) const { return static_cast<unsigned>(V) > RHS; }
|
|
|
|
|
|
|
|
Boolean operator-() const { return Boolean(V); }
|
2023-10-13 14:47:46 +02:00
|
|
|
Boolean operator-(const Boolean &Other) const { return Boolean(V - Other.V); }
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
Boolean operator~() const { return Boolean(true); }
|
2024-08-18 15:59:59 +02:00
|
|
|
Boolean operator!() const { return Boolean(!V); }
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
|
2024-07-15 15:12:11 +02:00
|
|
|
template <typename Ty, typename = std::enable_if_t<std::is_integral_v<Ty>>>
|
|
|
|
explicit operator Ty() const {
|
|
|
|
return V;
|
|
|
|
}
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
|
|
|
|
APSInt toAPSInt() const {
|
|
|
|
return APSInt(APInt(1, static_cast<uint64_t>(V), false), true);
|
|
|
|
}
|
|
|
|
APSInt toAPSInt(unsigned NumBits) const {
|
|
|
|
return APSInt(toAPSInt().zextOrTrunc(NumBits), true);
|
|
|
|
}
|
2024-07-20 15:25:00 +02:00
|
|
|
APValue toAPValue(const ASTContext &) const { return APValue(toAPSInt()); }
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
|
|
|
|
Boolean toUnsigned() const { return *this; }
|
|
|
|
|
2023-06-30 08:26:36 +02:00
|
|
|
constexpr static unsigned bitWidth() { return 1; }
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
bool isZero() const { return !V; }
|
|
|
|
bool isMin() const { return isZero(); }
|
|
|
|
|
|
|
|
constexpr static bool isMinusOne() { return false; }
|
|
|
|
|
|
|
|
constexpr static bool isSigned() { return false; }
|
|
|
|
|
|
|
|
constexpr static bool isNegative() { return false; }
|
|
|
|
constexpr static bool isPositive() { return !isNegative(); }
|
|
|
|
|
|
|
|
ComparisonCategoryResult compare(const Boolean &RHS) const {
|
|
|
|
return Compare(V, RHS.V);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned countLeadingZeros() const { return V ? 0 : 1; }
|
|
|
|
|
|
|
|
Boolean truncate(unsigned TruncBits) const { return *this; }
|
|
|
|
|
2024-10-31 18:09:40 +01:00
|
|
|
static Boolean bitcastFromMemory(const std::byte *Buff, unsigned BitWidth) {
|
2024-12-04 11:44:46 +01:00
|
|
|
// Just load the first byte.
|
2024-10-31 18:09:40 +01:00
|
|
|
bool Val = static_cast<bool>(*Buff);
|
|
|
|
return Boolean(Val);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bitcastToMemory(std::byte *Buff) { std::memcpy(Buff, &V, sizeof(V)); }
|
|
|
|
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
void print(llvm::raw_ostream &OS) const { OS << (V ? "true" : "false"); }
|
2023-09-29 09:41:58 +02:00
|
|
|
std::string toDiagnosticString(const ASTContext &Ctx) const {
|
|
|
|
std::string NameStr;
|
|
|
|
llvm::raw_string_ostream OS(NameStr);
|
|
|
|
print(OS);
|
|
|
|
return NameStr;
|
|
|
|
}
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
|
|
|
|
static Boolean min(unsigned NumBits) { return Boolean(false); }
|
|
|
|
static Boolean max(unsigned NumBits) { return Boolean(true); }
|
|
|
|
|
2022-08-26 15:39:17 +02:00
|
|
|
template <typename T> static Boolean from(T Value) {
|
|
|
|
if constexpr (std::is_integral<T>::value)
|
|
|
|
return Boolean(Value != 0);
|
|
|
|
return Boolean(static_cast<decltype(Boolean::V)>(Value) != 0);
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <unsigned SrcBits, bool SrcSign>
|
2020-02-10 23:23:44 -08:00
|
|
|
static std::enable_if_t<SrcBits != 0, Boolean>
|
|
|
|
from(Integral<SrcBits, SrcSign> Value) {
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
return Boolean(!Value.isZero());
|
|
|
|
}
|
|
|
|
|
|
|
|
static Boolean zero() { return from(false); }
|
|
|
|
|
2024-08-16 17:13:12 +02:00
|
|
|
template <typename T> static Boolean from(T Value, unsigned NumBits) {
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
return Boolean(Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool inRange(int64_t Value, unsigned NumBits) {
|
|
|
|
return Value == 0 || Value == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool increment(Boolean A, Boolean *R) {
|
|
|
|
*R = Boolean(true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool decrement(Boolean A, Boolean *R) {
|
|
|
|
llvm_unreachable("Cannot decrement booleans");
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool add(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
|
|
|
|
*R = Boolean(A.V || B.V);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool sub(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
|
|
|
|
*R = Boolean(A.V ^ B.V);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool mul(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
|
|
|
|
*R = Boolean(A.V && B.V);
|
|
|
|
return false;
|
|
|
|
}
|
2022-08-09 08:08:48 +02:00
|
|
|
|
|
|
|
static bool inv(Boolean A, Boolean *R) {
|
|
|
|
*R = Boolean(!A.V);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool neg(Boolean A, Boolean *R) {
|
|
|
|
*R = Boolean(A.V);
|
|
|
|
return false;
|
|
|
|
}
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Boolean &B) {
|
|
|
|
B.print(OS);
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
2024-08-16 17:13:12 +02:00
|
|
|
} // namespace interp
|
|
|
|
} // namespace clang
|
[Clang Interpreter] Initial patch for the constexpr interpreter
Summary:
This patch introduces the skeleton of the constexpr interpreter,
capable of evaluating a simple constexpr functions consisting of
if statements. The interpreter is described in more detail in the
RFC. Further patches will add more features.
Reviewers: Bigcheese, jfb, rsmith
Subscribers: bruno, uenoku, ldionne, Tyker, thegameg, tschuett, dexonsmith, mgorny, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64146
llvm-svn: 371834
2019-09-13 09:46:16 +00:00
|
|
|
|
|
|
|
#endif
|