13#ifndef LLVM_CLANG_AST_INTERP_BYTECODEEXPRGEN_H
14#define LLVM_CLANG_AST_INTERP_BYTECODEEXPRGEN_H
31template <
class Emitter>
class LocalScope;
32template <
class Emitter>
class DestructorScope;
40template <
class Emitter>
45 using LabelTy =
typename Emitter::LabelTy;
46 using AddrTy =
typename Emitter::AddrTy;
55 template <
typename... Tys>
129 llvm_unreachable(
"not a primitive type");
150 if (!this->emitGetPtrLocal(I, Init))
156 return this->emitPopPtr(Init);
161 if (!this->emitGetPtrGlobal(I, Init))
167 if (Init->getType()->isRecordType() && !this->emitCheckGlobalCtor(Init))
170 return this->emitPopPtr(Init);
175 if (!this->emitThis(I))
181 return this->emitPopPtr(I);
185 llvm::function_ref<
bool(
const Expr *)>
V);
189 bool IsExtended =
false);
206 enum class DerefKind {
219 llvm::function_ref<
bool(
PrimType)> Direct,
220 llvm::function_ref<
bool(
PrimType)> Indirect);
223 llvm::function_ref<
bool(
PrimType)> Direct,
224 llvm::function_ref<
bool(
PrimType)> Indirect);
226 DerefKind AK, llvm::function_ref<
bool(
PrimType)> Direct,
227 llvm::function_ref<
bool(
PrimType)> Indirect);
230 bool emitConst(
const llvm::APSInt &
Value,
const Expr *E);
231 bool emitConst(
const llvm::APInt &
Value,
const Expr *E) {
232 return emitConst(
static_cast<llvm::APSInt
>(
Value), E);
236 template <
typename T>
bool emitConst(T
Value,
const Expr *E);
249 bool shouldBeGloballyIndexed(
const ValueDecl *VD)
const {
250 if (
const auto *
V = dyn_cast<VarDecl>(VD))
251 return V->hasGlobalStorage() ||
V->isConstexpr();
256 llvm::RoundingMode getRoundingMode(
const Expr *E)
const {
259 if (FPO.getRoundingMode() == llvm::RoundingMode::Dynamic)
260 return llvm::RoundingMode::NearestTiesToEven;
262 return FPO.getRoundingMode();
265 bool emitRecordDestruction(
const Descriptor *Desc);
266 bool emitDerivedToBaseCasts(
const RecordType *DerivedType,
267 const RecordType *BaseType,
const Expr *E);
271 llvm::DenseMap<const ValueDecl *, Scope::Local>
Locals;
293 : Ctx(Ctx),
Parent(Ctx->VarScope) {
301 this->addExtended(Local);
303 this->addLocal(Local);
308 this->
Parent->addLocal(Local);
313 this->
Parent->addExtended(Local);
343 this->emitDestructors();
345 this->Idx = std::nullopt;
350 Idx = this->Ctx->Descriptors.size();
351 this->Ctx->Descriptors.emplace_back();
354 this->Ctx->Descriptors[*Idx].emplace_back(Local);
362 for (
Scope::Local &Local : this->Ctx->Descriptors[*Idx]) {
363 if (!Local.Desc->isPrimitive() && !Local.Desc->isPrimitiveArray()) {
364 this->Ctx->emitGetPtrLocal(Local.Offset,
SourceInfo{});
365 this->Ctx->emitRecordDestruction(Local.Desc);
371 std::optional<unsigned>
Idx;
407 this->addLocal(Local);
419 this->
Parent->addLocal(Local);
434 std::optional<uint64_t> OldArrayIndex;
static std::optional< DereferenceInfo > dereference(ProgramStateRef State, const FieldRegion *FR)
Dereferences FR and returns with the pointee's region, and whether it needs to be casted back to it's...
AbstractConditionalOperator - An abstract base class for ConditionalOperator and BinaryConditionalOpe...
Represents the index of the current element of an array being initialized by an ArrayInitLoopExpr.
ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting.
A builtin binary operation expression such as "x + y" or "x <= y".
A boolean literal, per ([C++ lex.bool] Boolean literals).
A default argument (C++ [dcl.fct.default]).
A use of a default initializer in a constructor or in aggregate initialization.
Represents a call to a member function that may be written either with member call syntax (e....
The null pointer literal (C++11 [lex.nullptr])
Represents a C++ struct/union/class.
Represents the this expression in C++.
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
CastExpr - Base class for type casts, including both implicit casts (ImplicitCastExpr) and explicit c...
CompoundAssignOperator - For compound assignments (e.g.
CompoundLiteralExpr - [C99 6.5.2.5].
ConstStmtVisitor - This class implements a simple visitor for Stmt subclasses.
ConstantExpr - An expression that occurs in a constant context and optionally the result of evaluatin...
A reference to a declared variable, function, enum, etc.
Decl - This represents one declaration (or definition), e.g.
Represents an expression – generally a full-expression – that introduces cleanups to be run at the en...
This represents one expression.
Represents a function declaration or definition.
Represents an implicitly-generated value initialization of an object of a given type.
Describes an C or C++ initializer list.
Represents a prvalue temporary that is written into memory so that a reference can bind to it.
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
OpaqueValueExpr - An expression referring to an opaque object of a fixed type and value class.
ParenExpr - This represents a parethesized expression, e.g.
Represents a parameter to a function.
A (possibly-)qualified type.
Represents a struct/union/class.
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
StringLiteral - This represents a string literal expression, e.g.
Represents a reference to a non-type template parameter that has been substituted with a template arg...
A type trait used in the implementation of various C++11 and Library TR1 trait templates.
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
const CXXRecordDecl * getPointeeCXXRecordDecl() const
If this is a pointer or reference to a RecordType, return the CXXRecordDecl that the type refers to.
UnaryExprOrTypeTraitExpr - expression with either a type or (unevaluated) expression operand.
UnaryOperator - This represents the unary-expression's (except sizeof and alignof),...
Represents a variable declaration or definition.
ArrayIndexScope(ByteCodeExprGen< Emitter > *Ctx, uint64_t Index)
Like a regular LocalScope, except that the destructors of all local variables are automatically emitt...
AutoScope(ByteCodeExprGen< Emitter > *Ctx)
Scope for storage declared in a compound statement.
BlockScope(ByteCodeExprGen< Emitter > *Ctx)
void addExtended(const Scope::Local &Local) override
Compilation context for expressions.
bool visitExpr(const Expr *E) override
std::optional< unsigned > allocateLocal(DeclTy &&Decl, bool IsExtended=false)
Allocates a space storing a local given its type.
unsigned allocateLocalPrimitive(DeclTy &&Decl, PrimType Ty, bool IsConst, bool IsExtended=false)
Creates a local primitive value.
bool VisitIntegerLiteral(const IntegerLiteral *E)
bool VisitCharacterLiteral(const CharacterLiteral *E)
bool VisitDeclRefExpr(const DeclRefExpr *E)
bool VisitExprWithCleanups(const ExprWithCleanups *E)
bool VisitMemberExpr(const MemberExpr *E)
bool discard(const Expr *E)
Evaluates an expression for side effects and discards the result.
bool visitThisInitializer(const Expr *I)
Visits a delegated initializer.
bool visitDecl(const VarDecl *VD) override
Toplevel visitDecl().
bool visitInitializer(const Expr *E)
Compiles an initializer.
bool visitRecordInitializer(const Expr *Initializer)
Compiles a record initializer.
bool VisitParenExpr(const ParenExpr *E)
bool VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E)
bool VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E)
bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E)
bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E)
Program & P
Program to link to.
bool VisitBuiltinCallExpr(const CallExpr *E)
PrimType classifyPrim(QualType Ty) const
Classifies a known primitive type.
bool VisitCXXMemberCallExpr(const CXXMemberCallExpr *E)
llvm::DenseMap< const ValueDecl *, Scope::Local > Locals
Variable to storage mapping.
void emitCleanup()
Emits scope cleanup instructions.
bool VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E)
bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
Context & Ctx
Current compilation context.
bool visitGlobalInitializer(const Expr *Init, unsigned I)
Visits an initializer for a global.
bool visitBool(const Expr *E)
Visits an expression and converts it to a boolean.
VariableScope< Emitter > * VarScope
Current scope.
std::optional< PrimType > classify(QualType Ty) const
bool VisitBinaryOperator(const BinaryOperator *E)
bool VisitTypeTraitExpr(const TypeTraitExpr *E)
bool visit(const Expr *E)
Evaluates an expression and places result on stack.
bool VisitInitListExpr(const InitListExpr *E)
bool VisitFloatingLiteral(const FloatingLiteral *E)
bool VisitAbstractConditionalOperator(const AbstractConditionalOperator *E)
bool VisitUnaryOperator(const UnaryOperator *E)
bool VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E)
std::optional< uint64_t > ArrayIndex
Current argument index. Needed to emit ArrayInitIndexExpr.
bool VisitPointerCompoundAssignOperator(const CompoundAssignOperator *E)
bool VisitFloatCompoundAssignOperator(const CompoundAssignOperator *E)
std::optional< PrimType > classify(const Expr *E) const
Classifies a type.
bool DiscardResult
Flag indicating if return value is to be discarded.
bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E)
bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E)
const Function * getFunction(const FunctionDecl *FD)
bool visitLocalInitializer(const Expr *Init, unsigned I)
Visits an initializer for a local.
bool VisitLogicalBinOp(const BinaryOperator *E)
bool visitVarDecl(const VarDecl *VD)
Creates and initializes a variable from the given decl.
bool VisitStringLiteral(const StringLiteral *E)
ByteCodeExprGen(Context &Ctx, Program &P, Tys &&... Args)
Initializes the compiler and the backend emitter.
Record * getRecord(QualType Ty)
Returns a record from a record or pointer type.
llvm::DenseMap< const OpaqueValueExpr *, unsigned > OpaqueExprs
OpaqueValueExpr to location mapping.
bool VisitOpaqueValueExpr(const OpaqueValueExpr *E)
bool VisitCXXThisExpr(const CXXThisExpr *E)
typename Emitter::LabelTy LabelTy
bool VisitPointerArithBinOp(const BinaryOperator *E)
Perform addition/subtraction of a pointer and an integer or subtraction of two pointers.
bool VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E)
bool visitConditional(const AbstractConditionalOperator *E, llvm::function_ref< bool(const Expr *)> V)
Visit a conditional operator, i.e.
bool VisitCompoundAssignOperator(const CompoundAssignOperator *E)
bool VisitCallExpr(const CallExpr *E)
const RecordType * getRecordTy(QualType Ty)
Returns a record type from a record or pointer type.
bool VisitConstantExpr(const ConstantExpr *E)
bool VisitCastExpr(const CastExpr *E)
bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E)
bool visitArrayInitializer(const Expr *Initializer)
Compiles an array initializer.
typename Emitter::AddrTy AddrTy
Holds all information required to evaluate constexpr code in a module.
const LangOptions & getLangOpts() const
Returns the language options.
std::optional< PrimType > classify(QualType T) const
Classifies an expression.
Scope used to handle temporaries in toplevel variable declarations.
Emits the destructors of the variables of.
DestructorScope(LocalScope< Emitter > &OtherScope)
Expression scope which tracks potentially lifetime extended temporaries which are hoisted to the pare...
void addExtended(const Scope::Local &Local) override
ExprScope(ByteCodeExprGen< Emitter > *Ctx)
Generic scope for local variables.
~LocalScope() override
Emit a Destroy op for this scope.
void emitDestruction() override
Overriden to support explicit destruction.
void emitDestructors() override
void addLocal(const Scope::Local &Local) override
std::optional< unsigned > Idx
Index of the scope in the chain.
LocalScope(ByteCodeExprGen< Emitter > *Ctx)
Scope used to handle initialization methods.
The program contains and links the bytecode for all functions.
Structure/Class descriptor.
Describes the statement/declaration an opcode was generated from.
Scope chain managing the variable lifetimes.
virtual void addExtended(const Scope::Local &Local)
virtual void emitDestructors()
void add(const Scope::Local &Local, bool IsExtended)
VariableScope * Parent
Link to the parent scope.
virtual void addLocal(const Scope::Local &Local)
VariableScope(ByteCodeExprGen< Emitter > *Ctx)
VariableScope * getParent() const
ByteCodeExprGen< Emitter > * Ctx
ByteCodeExprGen instance.
virtual void emitDestruction()
Defines the clang::TargetInfo interface.
PrimType
Enumeration of the primitive types of the VM.
llvm::PointerUnion< const Decl *, const Expr * > DeclTy
Information about a local's storage.