diff --git a/clang/lib/AST/Interp/ByteCodeEmitter.cpp b/clang/lib/AST/Interp/ByteCodeEmitter.cpp index fd2a92d9d3f91..409ce21506caa 100644 --- a/clang/lib/AST/Interp/ByteCodeEmitter.cpp +++ b/clang/lib/AST/Interp/ByteCodeEmitter.cpp @@ -10,6 +10,7 @@ #include "ByteCodeGenError.h" #include "Context.h" #include "Floating.h" +#include "IntegralAP.h" #include "Opcode.h" #include "Program.h" #include "clang/AST/ASTLambda.h" @@ -209,9 +210,11 @@ static void emit(Program &P, std::vector &Code, const T &Val, } } -template <> -void emit(Program &P, std::vector &Code, const Floating &Val, - bool &Success) { +/// Emits a serializable value. These usually (potentially) contain +/// heap-allocated memory and aren't trivially copyable. +template +static void emitSerialized(std::vector &Code, const T &Val, + bool &Success) { size_t Size = Val.bytesToSerialize(); if (Code.size() + Size > std::numeric_limits::max()) { @@ -228,6 +231,24 @@ void emit(Program &P, std::vector &Code, const Floating &Val, Val.serialize(Code.data() + ValPos); } +template <> +void emit(Program &P, std::vector &Code, const Floating &Val, + bool &Success) { + emitSerialized(Code, Val, Success); +} + +template <> +void emit(Program &P, std::vector &Code, + const IntegralAP &Val, bool &Success) { + emitSerialized(Code, Val, Success); +} + +template <> +void emit(Program &P, std::vector &Code, const IntegralAP &Val, + bool &Success) { + emitSerialized(Code, Val, Success); +} + template bool ByteCodeEmitter::emitOp(Opcode Op, const Tys &... Args, const SourceInfo &SI) { bool Success = true; diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp index 8188d6f7f5c24..386abef598594 100644 --- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp +++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp @@ -2204,15 +2204,13 @@ bool ByteCodeExprGen::emitConst(T Value, PrimType Ty, const Expr *E) { return this->emitConstSint64(Value, E); case PT_Uint64: return this->emitConstUint64(Value, E); - case PT_IntAP: - case PT_IntAPS: - assert(false); - return false; case PT_Bool: return this->emitConstBool(Value, E); case PT_Ptr: case PT_FnPtr: case PT_Float: + case PT_IntAP: + case PT_IntAPS: llvm_unreachable("Invalid integral type"); break; } @@ -2228,6 +2226,11 @@ bool ByteCodeExprGen::emitConst(T Value, const Expr *E) { template bool ByteCodeExprGen::emitConst(const APSInt &Value, PrimType Ty, const Expr *E) { + if (Ty == PT_IntAPS) + return this->emitConstIntAPS(Value, E); + if (Ty == PT_IntAP) + return this->emitConstIntAP(Value, E); + if (Value.isSigned()) return this->emitConst(Value.getSExtValue(), Ty, E); return this->emitConst(Value.getZExtValue(), Ty, E); diff --git a/clang/lib/AST/Interp/Disasm.cpp b/clang/lib/AST/Interp/Disasm.cpp index d276df8f29262..eba437e05f59d 100644 --- a/clang/lib/AST/Interp/Disasm.cpp +++ b/clang/lib/AST/Interp/Disasm.cpp @@ -12,6 +12,7 @@ #include "Floating.h" #include "Function.h" +#include "IntegralAP.h" #include "Opcode.h" #include "PrimType.h" #include "Program.h" @@ -37,6 +38,20 @@ template <> inline Floating ReadArg(Program &P, CodePtr &OpPC) { return F; } +template <> +inline IntegralAP ReadArg>(Program &P, CodePtr &OpPC) { + IntegralAP I = IntegralAP::deserialize(*OpPC); + OpPC += align(I.bytesToSerialize()); + return I; +} + +template <> +inline IntegralAP ReadArg>(Program &P, CodePtr &OpPC) { + IntegralAP I = IntegralAP::deserialize(*OpPC); + OpPC += align(I.bytesToSerialize()); + return I; +} + LLVM_DUMP_METHOD void Function::dump() const { dump(llvm::errs()); } LLVM_DUMP_METHOD void Function::dump(llvm::raw_ostream &OS) const { diff --git a/clang/lib/AST/Interp/IntegralAP.h b/clang/lib/AST/Interp/IntegralAP.h index 55e29caa1cd74..bab9774288bfa 100644 --- a/clang/lib/AST/Interp/IntegralAP.h +++ b/clang/lib/AST/Interp/IntegralAP.h @@ -263,6 +263,31 @@ template class IntegralAP final { *R = IntegralAP(A.V.lshr(ShiftAmount)); } + // === Serialization support === + size_t bytesToSerialize() const { + // 4 bytes for the BitWidth followed by N bytes for the actual APInt. + return sizeof(uint32_t) + (V.getBitWidth() / CHAR_BIT); + } + + void serialize(std::byte *Buff) const { + assert(V.getBitWidth() < std::numeric_limits::max()); + uint32_t BitWidth = V.getBitWidth(); + + std::memcpy(Buff, &BitWidth, sizeof(uint32_t)); + llvm::StoreIntToMemory(V, (uint8_t *)(Buff + sizeof(uint32_t)), + BitWidth / CHAR_BIT); + } + + static IntegralAP deserialize(const std::byte *Buff) { + uint32_t BitWidth; + std::memcpy(&BitWidth, Buff, sizeof(uint32_t)); + IntegralAP Val(APInt(BitWidth, 0ull, !Signed)); + + llvm::LoadIntFromMemory(Val.V, (const uint8_t *)Buff + sizeof(uint32_t), + BitWidth / CHAR_BIT); + return Val; + } + private: template