13 #ifndef LLVM_CLANG_AST_INTERP_BYTECODEEXPRGEN_H 14 #define LLVM_CLANG_AST_INTERP_BYTECODEEXPRGEN_H 24 #include "llvm/ADT/Optional.h" 40 template <
class Emitter>
51 using LabelTy =
typename Emitter::LabelTy;
52 using AddrTy =
typename Emitter::AddrTy;
64 template <
typename... Tys>
66 :
Emitter(Ctx, P, Args...), Ctx(Ctx), P(P) {}
119 llvm_unreachable(
"not a primitive type");
135 return this->emitGetPtrLocal(I, Init);
142 return this->emitGetPtrGlobal(I, Init);
153 bool IsExtended =
false);
157 bool IsExtended =
false);
167 bool visitZeroInitializer(PrimType T,
const Expr *E);
169 enum class DerefKind {
181 bool dereference(
const Expr *LV, DerefKind AK,
182 llvm::function_ref<
bool(PrimType)> Direct,
183 llvm::function_ref<
bool(PrimType)> Indirect);
184 bool dereferenceParam(
const Expr *LV, PrimType T,
const ParmVarDecl *PD,
186 llvm::function_ref<
bool(PrimType)> Direct,
187 llvm::function_ref<
bool(PrimType)> Indirect);
188 bool dereferenceVar(
const Expr *LV, PrimType T,
const VarDecl *PD,
189 DerefKind AK, llvm::function_ref<
bool(PrimType)> Direct,
190 llvm::function_ref<
bool(PrimType)> Indirect);
197 template <
typename T>
bool emitConst(
const Expr *E, T
Value) {
200 APInt WrappedValue(NumBits, Value, std::is_signed<T>::value);
201 return emitConst(*Ctx.
classify(Ty), NumBits, WrappedValue, E);
205 bool getPtrVarDecl(
const VarDecl *VD,
const Expr *E);
212 assert(
InitFn &&
"missing initializer");
218 llvm::DenseMap<const ValueDecl *, Scope::Local>
Locals;
246 this->addExtended(Local);
248 this->addLocal(Local);
253 this->
Parent->addLocal(Local);
258 this->
Parent->addExtended(Local);
288 if (!Idx.hasValue()) {
289 Idx = this->Ctx->Descriptors.size();
290 this->Ctx->Descriptors.emplace_back();
293 this->Ctx->Descriptors[*Idx].emplace_back(Local);
313 llvm_unreachable(
"Cannot create temporaries in full scopes");
324 this->
Parent->addLocal(Local);
VariableScope * Parent
Link to the parent scope.
unsigned llvm::PointerUnion< const Decl *, const Expr * > DeclTy
Scope chain managing the variable lifetimes.
A (possibly-)qualified type.
ConstStmtVisitor - This class implements a simple visitor for Stmt subclasses.
Decl - This represents one declaration (or definition), e.g.
llvm::DenseMap< const ValueDecl *, Scope::Local > Locals
Variable to storage mapping.
ParenExpr - This represents a parethesized expression, e.g.
bool VisitCastExpr(const CastExpr *E)
const TargetInfo & getTargetInfo() const
constexpr XRayInstrMask Function
unsigned getCharWidth() const
Represents a variable declaration or definition.
bool visitInitializer(const Expr *E, InitFnRef GenPtr)
Compiles an initializer for a local.
Describes the statement/declaration an opcode was generated from.
Holds all information required to evaluate constexpr code in a module.
Represents a parameter to a function.
bool VisitIntegerLiteral(const IntegerLiteral *E)
bool(ByteCodeExprGen::*)(PrimType, const SourceInfo &) UnaryFn
llvm::Optional< PrimType > classify(QualType T)
Classifies an expression.
Represents a struct/union/class.
bool visitDecl(const VarDecl *VD) override
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
bool(ByteCodeExprGen::*)(const SourceInfo &) NullaryFn
bool needsAdjust(QualType Ty) const
Checks if a pointer needs adjustment.
bool visit(const Expr *E)
Evaluates an expression and places result on stack.
llvm::Optional< InitFnRef > InitFn
Expression being initialized.
LocalScope(ByteCodeExprGen< Emitter > *Ctx)
void emitDestruction() override
Structure/Class descriptor.
void addLocal(const Scope::Local &Local) override
bool visitBool(const Expr *E)
Visits an expression and converts it to a boolean.
PrimType
Enumeration of the primitive types of the VM.
A builtin binary operation expression such as "x + y" or "x <= y".
void add(const Scope::Local &Local, bool IsExtended)
VariableScope * getParent()
CastExpr - Base class for type casts, including both implicit casts (ImplicitCastExpr) and explicit c...
Optional< unsigned > Idx
Index of the scope in the chain.
llvm::DenseMap< const OpaqueValueExpr *, unsigned > OpaqueExprs
OpaqueValueExpr to location mapping.
Expression scope which tracks potentially lifetime extended temporaries which are hoisted to the pare...
Scope for storage declared in a compound statement.
bool VisitBinaryOperator(const BinaryOperator *E)
ExprScope(ByteCodeExprGen< Emitter > *Ctx)
bool discard(const Expr *E)
Evaluates an expression for side effects and discards the result.
This represents one expression.
Information about a local's storage.
std::function< bool()> InitFnRef
ByteCodeExprGen< Emitter > * Ctx
ByteCodeExprGen instance.
ASTContext & getASTContext() const
Returns the AST context.
void addExtended(const Scope::Local &Local) override
typename Emitter::AddrTy AddrTy
void emitCleanup()
Emits scope cleanup instructions.
The program contains and links the bytecode for all functions.
virtual void addLocal(const Scope::Local &Local)
VariableScope(ByteCodeExprGen< Emitter > *Ctx)
llvm::Optional< PrimType > classify(const Expr *E) const
Classifies a type.
Record * getRecord(QualType Ty)
Returns a record from a record or pointer type.
Scope used to handle temporaries in toplevel variable declarations.
bool visitThisInitializer(const Expr *I)
Visits a delegated initializer.
Scope used to handle initialization methods.
virtual void emitDestruction()
Dataflow Directional Tag Classes.
bool DiscardResult
Flag indicating if return value is to be discarded.
unsigned getIntWidth(QualType Ty)
Returns the size int bits of an integer.
void addExtended(const Scope::Local &Local) override
bool(ByteCodeExprGen::*)(PrimType, PrimType, const SourceInfo &) BinaryFn
PrimType classifyPrim(QualType Ty) const
Classifies a known primitive type.
bool visitLocalInitializer(const Expr *Init, unsigned I)
Visits an initializer for a local.
const RecordType * getRecordTy(QualType Ty)
Returns a record type from a record or pointer type.
bool visitExpr(const Expr *E) override
unsigned getIntWidth(QualType T) const
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Context & Ctx
Current compilation context.
ByteCodeExprGen(Context &Ctx, Program &P, Tys &&... Args)
Initializes the compiler and the backend emitter.
Program & P
Program to link to.
Scope for local variables.
bool VisitParenExpr(const ParenExpr *E)
Compilation context for expressions.
typename Emitter::LabelTy LabelTy
BlockScope(ByteCodeExprGen< Emitter > *Ctx)
VariableScope< Emitter > * VarScope
Current scope.
llvm::Optional< PrimType > classify(QualType Ty) const
bool visitGlobalInitializer(const Expr *Init, unsigned I)
Visits an initializer for a global.
virtual void addExtended(const Scope::Local &Local)
llvm::Optional< uint64_t > ArrayIndex
Current argument index.
unsigned getCharBit() const
Returns the value of CHAR_BIT.
llvm::Optional< unsigned > allocateLocal(DeclTy &&Decl, bool IsExtended=false)
Allocates a space storing a local given its type.
unsigned allocateLocalPrimitive(DeclTy &&Decl, PrimType Ty, bool IsMutable, bool IsExtended=false)
Creates a local primitive value.