| 1 |
//===--- PrimType.h - Types for the constexpr VM ----------------*- C++ -*-===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
// |
| 9 |
// Defines the VM types and helpers operating on types. |
| 10 |
// |
| 11 |
//===----------------------------------------------------------------------===// |
| 12 |
|
| 13 |
#ifndef LLVM_CLANG_AST_INTERP_TYPE_H |
| 14 |
#define LLVM_CLANG_AST_INTERP_TYPE_H |
| 15 |
|
| 16 |
#include "llvm/Support/raw_ostream.h" |
| 17 |
#include <climits> |
| 18 |
#include <cstddef> |
| 19 |
#include <cstdint> |
| 20 |
|
| 21 |
namespace clang { |
| 22 |
namespace interp { |
| 23 |
|
| 24 |
class Pointer; |
| 25 |
class Boolean; |
| 26 |
class Floating; |
| 27 |
class FunctionPointer; |
| 28 |
class MemberPointer; |
| 29 |
class FixedPoint; |
| 30 |
template <bool Signed> class IntegralAP; |
| 31 |
template <unsigned Bits, bool Signed> class Integral; |
| 32 |
|
| 33 |
/// Enumeration of the primitive types of the VM. |
| 34 |
enum PrimType : uint8_t { |
| 35 |
PT_Sint8 = 0, |
| 36 |
PT_Uint8 = 1, |
| 37 |
PT_Sint16 = 2, |
| 38 |
PT_Uint16 = 3, |
| 39 |
PT_Sint32 = 4, |
| 40 |
PT_Uint32 = 5, |
| 41 |
PT_Sint64 = 6, |
| 42 |
PT_Uint64 = 7, |
| 43 |
PT_IntAP = 8, |
| 44 |
PT_IntAPS = 9, |
| 45 |
PT_Bool = 10, |
| 46 |
PT_FixedPoint = 11, |
| 47 |
PT_Float = 12, |
| 48 |
PT_Ptr = 13, |
| 49 |
PT_MemberPtr = 14, |
| 50 |
}; |
| 51 |
|
| 52 |
// Like std::optional<PrimType>, but only sizeof(PrimType). |
| 53 |
class OptPrimType final { |
| 54 |
static constexpr uint8_t None = 0xFF; |
| 55 |
uint8_t V = None; |
| 56 |
|
| 57 |
public: |
| 58 |
OptPrimType() = default; |
| 59 |
OptPrimType(std::nullopt_t) {} |
| 60 |
OptPrimType(PrimType T) : V(static_cast<unsigned>(T)) {} |
| 61 |
|
| 62 |
explicit constexpr operator bool() const { return V != None; } |
| 63 |
PrimType operator*() const { |
| 64 |
assert(operator bool()); |
| 65 |
return static_cast<PrimType>(V); |
| 66 |
} |
| 67 |
|
| 68 |
PrimType value_or(PrimType PT) const { |
| 69 |
if (operator bool()) |
| 70 |
return static_cast<PrimType>(V); |
| 71 |
return PT; |
| 72 |
} |
| 73 |
|
| 74 |
bool operator==(PrimType PT) const { |
| 75 |
if (!operator bool()) |
| 76 |
return false; |
| 77 |
return V == static_cast<unsigned>(PT); |
| 78 |
} |
| 79 |
bool operator==(OptPrimType OPT) const { return V == OPT.V; } |
| 80 |
bool operator!=(PrimType PT) const { return !(*this == PT); } |
| 81 |
bool operator!=(OptPrimType OPT) const { return V != OPT.V; } |
| 82 |
}; |
| 83 |
static_assert(sizeof(OptPrimType) == sizeof(PrimType)); |
| 84 |
|
| 85 |
inline constexpr bool isPtrType(PrimType T) { |
| 86 |
return T == PT_Ptr || T == PT_MemberPtr; |
| 87 |
} |
| 88 |
|
| 89 |
inline constexpr bool isSignedType(PrimType T) { |
| 90 |
switch (T) { |
| 91 |
case PT_Sint8: |
| 92 |
case PT_Sint16: |
| 93 |
case PT_Sint32: |
| 94 |
case PT_Sint64: |
| 95 |
return true; |
| 96 |
default: |
| 97 |
return false; |
| 98 |
} |
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
enum class CastKind : uint8_t { |
| 103 |
Reinterpret, |
| 104 |
ReinterpretLike, |
| 105 |
Volatile, |
| 106 |
Dynamic, |
| 107 |
}; |
| 108 |
|
| 109 |
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, |
| 110 |
interp::CastKind CK) { |
| 111 |
switch (CK) { |
| 112 |
case interp::CastKind::Reinterpret: |
| 113 |
OS << "reinterpret_cast"; |
| 114 |
break; |
| 115 |
case interp::CastKind::ReinterpretLike: |
| 116 |
OS << "reinterpret_like"; |
| 117 |
break; |
| 118 |
case interp::CastKind::Volatile: |
| 119 |
OS << "volatile"; |
| 120 |
break; |
| 121 |
case interp::CastKind::Dynamic: |
| 122 |
OS << "dynamic"; |
| 123 |
break; |
| 124 |
} |
| 125 |
return OS; |
| 126 |
} |
| 127 |
|
| 128 |
constexpr bool isIntegralType(PrimType T) { return T <= PT_FixedPoint; } |
| 129 |
template <typename T> constexpr bool needsAlloc() { |
| 130 |
return std::is_same_v<T, IntegralAP<false>> || |
| 131 |
std::is_same_v<T, IntegralAP<true>> || std::is_same_v<T, Floating>; |
| 132 |
} |
| 133 |
constexpr bool needsAlloc(PrimType T) { |
| 134 |
return T == PT_IntAP || T == PT_IntAPS || T == PT_Float; |
| 135 |
} |
| 136 |
|
| 137 |
/// Mapping from primitive types to their representation. |
| 138 |
template <PrimType T> struct PrimConv; |
| 139 |
template <> struct PrimConv<PT_Sint8> { |
| 140 |
using T = Integral<8, true>; |
| 141 |
}; |
| 142 |
template <> struct PrimConv<PT_Uint8> { |
| 143 |
using T = Integral<8, false>; |
| 144 |
}; |
| 145 |
template <> struct PrimConv<PT_Sint16> { |
| 146 |
using T = Integral<16, true>; |
| 147 |
}; |
| 148 |
template <> struct PrimConv<PT_Uint16> { |
| 149 |
using T = Integral<16, false>; |
| 150 |
}; |
| 151 |
template <> struct PrimConv<PT_Sint32> { |
| 152 |
using T = Integral<32, true>; |
| 153 |
}; |
| 154 |
template <> struct PrimConv<PT_Uint32> { |
| 155 |
using T = Integral<32, false>; |
| 156 |
}; |
| 157 |
template <> struct PrimConv<PT_Sint64> { |
| 158 |
using T = Integral<64, true>; |
| 159 |
}; |
| 160 |
template <> struct PrimConv<PT_Uint64> { |
| 161 |
using T = Integral<64, false>; |
| 162 |
}; |
| 163 |
template <> struct PrimConv<PT_IntAP> { |
| 164 |
using T = IntegralAP<false>; |
| 165 |
}; |
| 166 |
template <> struct PrimConv<PT_IntAPS> { |
| 167 |
using T = IntegralAP<true>; |
| 168 |
}; |
| 169 |
template <> struct PrimConv<PT_Float> { |
| 170 |
using T = Floating; |
| 171 |
}; |
| 172 |
template <> struct PrimConv<PT_Bool> { |
| 173 |
using T = Boolean; |
| 174 |
}; |
| 175 |
template <> struct PrimConv<PT_Ptr> { |
| 176 |
using T = Pointer; |
| 177 |
}; |
| 178 |
template <> struct PrimConv<PT_MemberPtr> { |
| 179 |
using T = MemberPointer; |
| 180 |
}; |
| 181 |
template <> struct PrimConv<PT_FixedPoint> { |
| 182 |
using T = FixedPoint; |
| 183 |
}; |
| 184 |
|
| 185 |
/// Returns the size of a primitive type in bytes. |
| 186 |
size_t primSize(PrimType Type); |
| 187 |
|
| 188 |
/// Aligns a size to the pointer alignment. |
| 189 |
constexpr size_t align(size_t Size) { |
| 190 |
return ((Size + alignof(void *) - 1) / alignof(void *)) * alignof(void *); |
| 191 |
} |
| 192 |
|
| 193 |
constexpr bool aligned(uintptr_t Value) { return Value == align(Value); } |
| 194 |
static_assert(aligned(sizeof(void *))); |
| 195 |
|
| 196 |
static inline bool aligned(const void *P) { |
| 197 |
return aligned(reinterpret_cast<uintptr_t>(P)); |
| 198 |
} |
| 199 |
|
| 200 |
} // namespace interp |
| 201 |
} // namespace clang |
| 202 |
|
| 203 |
/// Helper macro to simplify type switches. |
| 204 |
/// The macro implicitly exposes a type T in the scope of the inner block. |
| 205 |
#define TYPE_SWITCH_CASE(Name, B) \ |
| 206 |
case Name: { \ |
| 207 |
using T = PrimConv<Name>::T; \ |
| 208 |
B; \ |
| 209 |
break; \ |
| 210 |
} |
| 211 |
#define TYPE_SWITCH(Expr, B) \ |
| 212 |
do { \ |
| 213 |
switch (Expr) { \ |
| 214 |
TYPE_SWITCH_CASE(PT_Sint8, B) \ |
| 215 |
TYPE_SWITCH_CASE(PT_Uint8, B) \ |
| 216 |
TYPE_SWITCH_CASE(PT_Sint16, B) \ |
| 217 |
TYPE_SWITCH_CASE(PT_Uint16, B) \ |
| 218 |
TYPE_SWITCH_CASE(PT_Sint32, B) \ |
| 219 |
TYPE_SWITCH_CASE(PT_Uint32, B) \ |
| 220 |
TYPE_SWITCH_CASE(PT_Sint64, B) \ |
| 221 |
TYPE_SWITCH_CASE(PT_Uint64, B) \ |
| 222 |
TYPE_SWITCH_CASE(PT_IntAP, B) \ |
| 223 |
TYPE_SWITCH_CASE(PT_IntAPS, B) \ |
| 224 |
TYPE_SWITCH_CASE(PT_Float, B) \ |
| 225 |
TYPE_SWITCH_CASE(PT_Bool, B) \ |
| 226 |
TYPE_SWITCH_CASE(PT_Ptr, B) \ |
| 227 |
TYPE_SWITCH_CASE(PT_MemberPtr, B) \ |
| 228 |
TYPE_SWITCH_CASE(PT_FixedPoint, B) \ |
| 229 |
} \ |
| 230 |
} while (0) |
| 231 |
|
| 232 |
#define INT_TYPE_SWITCH(Expr, B) \ |
| 233 |
do { \ |
| 234 |
switch (Expr) { \ |
| 235 |
TYPE_SWITCH_CASE(PT_Sint8, B) \ |
| 236 |
TYPE_SWITCH_CASE(PT_Uint8, B) \ |
| 237 |
TYPE_SWITCH_CASE(PT_Sint16, B) \ |
| 238 |
TYPE_SWITCH_CASE(PT_Uint16, B) \ |
| 239 |
TYPE_SWITCH_CASE(PT_Sint32, B) \ |
| 240 |
TYPE_SWITCH_CASE(PT_Uint32, B) \ |
| 241 |
TYPE_SWITCH_CASE(PT_Sint64, B) \ |
| 242 |
TYPE_SWITCH_CASE(PT_Uint64, B) \ |
| 243 |
TYPE_SWITCH_CASE(PT_IntAP, B) \ |
| 244 |
TYPE_SWITCH_CASE(PT_IntAPS, B) \ |
| 245 |
TYPE_SWITCH_CASE(PT_Bool, B) \ |
| 246 |
default: \ |
| 247 |
llvm_unreachable("Not an integer value"); \ |
| 248 |
} \ |
| 249 |
} while (0) |
| 250 |
|
| 251 |
#define INT_TYPE_SWITCH_NO_BOOL(Expr, B) \ |
| 252 |
do { \ |
| 253 |
switch (Expr) { \ |
| 254 |
TYPE_SWITCH_CASE(PT_Sint8, B) \ |
| 255 |
TYPE_SWITCH_CASE(PT_Uint8, B) \ |
| 256 |
TYPE_SWITCH_CASE(PT_Sint16, B) \ |
| 257 |
TYPE_SWITCH_CASE(PT_Uint16, B) \ |
| 258 |
TYPE_SWITCH_CASE(PT_Sint32, B) \ |
| 259 |
TYPE_SWITCH_CASE(PT_Uint32, B) \ |
| 260 |
TYPE_SWITCH_CASE(PT_Sint64, B) \ |
| 261 |
TYPE_SWITCH_CASE(PT_Uint64, B) \ |
| 262 |
TYPE_SWITCH_CASE(PT_IntAP, B) \ |
| 263 |
TYPE_SWITCH_CASE(PT_IntAPS, B) \ |
| 264 |
default: \ |
| 265 |
llvm_unreachable("Not an integer value"); \ |
| 266 |
} \ |
| 267 |
} while (0) |
| 268 |
|
| 269 |
#define TYPE_SWITCH_ALLOC(Expr, B) \ |
| 270 |
do { \ |
| 271 |
switch (Expr) { \ |
| 272 |
TYPE_SWITCH_CASE(PT_Float, B) \ |
| 273 |
TYPE_SWITCH_CASE(PT_IntAP, B) \ |
| 274 |
TYPE_SWITCH_CASE(PT_IntAPS, B) \ |
| 275 |
default:; \ |
| 276 |
} \ |
| 277 |
} while (0) |
| 278 |
|
| 279 |
#endif |