Go to the documentation of this file.
13 #ifndef LLVM_CLANG_AST_INTERP_BYTECODEEXPRGEN_H
14 #define LLVM_CLANG_AST_INTERP_BYTECODEEXPRGEN_H
25 #include "llvm/ADT/Optional.h"
39 template <
class Emitter>
50 using LabelTy =
typename Emitter::LabelTy;
51 using AddrTy =
typename Emitter::AddrTy;
63 template <
typename... Tys>
118 llvm_unreachable(
"not a primitive type");
134 return this->emitGetPtrLocal(I, Init);
141 return this->emitGetPtrGlobal(I, Init);
152 bool IsExtended =
false);
156 bool IsExtended =
false);
168 enum class DerefKind {
180 bool dereference(
const Expr *LV, DerefKind AK,
181 llvm::function_ref<
bool(
PrimType)> Direct,
182 llvm::function_ref<
bool(
PrimType)> Indirect);
185 llvm::function_ref<
bool(
PrimType)> Direct,
186 llvm::function_ref<
bool(
PrimType)> Indirect);
188 DerefKind AK, llvm::function_ref<
bool(
PrimType)> Direct,
189 llvm::function_ref<
bool(
PrimType)> Indirect);
196 template <
typename T>
bool emitConst(
const Expr *E, T
Value) {
199 APInt WrappedValue(NumBits,
Value, std::is_signed<T>::value);
200 return emitConst(*
Ctx.
classify(Ty), NumBits, WrappedValue, E);
204 bool getPtrVarDecl(
const VarDecl *VD,
const Expr *E);
211 assert(
InitFn &&
"missing initializer");
217 llvm::DenseMap<const ValueDecl *, Scope::Local>
Locals;
235 extern template class ByteCodeExprGen<ByteCodeEmitter>;
236 extern template class ByteCodeExprGen<EvalEmitter>;
239 template <
class Emitter>
class VariableScope {
245 this->addExtended(Local);
247 this->addLocal(Local);
252 this->
Parent->addLocal(Local);
257 this->
Parent->addExtended(Local);
266 : Ctx(Ctx),
Parent(Ctx->VarScope) {
287 if (!Idx.hasValue()) {
288 Idx = this->Ctx->Descriptors.size();
289 this->Ctx->Descriptors.emplace_back();
292 this->Ctx->Descriptors[*Idx].emplace_back(Local);
312 llvm_unreachable(
"Cannot create temporaries in full scopes");
323 this->
Parent->addLocal(Local);
bool visitBool(const Expr *E)
Visits an expression and converts it to a boolean.
bool discard(const Expr *E)
Evaluates an expression for side effects and discards the result.
Scope used to handle initialization methods.
unsigned getIntWidth(QualType Ty)
Returns the size int bits of an integer.
llvm::DenseMap< const OpaqueValueExpr *, unsigned > OpaqueExprs
OpaqueValueExpr to location mapping.
bool(ByteCodeExprGen::*)(PrimType, PrimType, const SourceInfo &) BinaryFn
unsigned getIntWidth(QualType T) const
Expression scope which tracks potentially lifetime extended temporaries which are hoisted to the pare...
bool VisitBinaryOperator(const BinaryOperator *E)
bool visitLocalInitializer(const Expr *Init, unsigned I)
Visits an initializer for a local.
A (possibly-)qualified type.
virtual void addExtended(const Scope::Local &Local)
void addLocal(const Scope::Local &Local) override
void emitCleanup()
Emits scope cleanup instructions.
Represents a parameter to a function.
Compilation context for expressions.
Context & Ctx
Current compilation context.
llvm::Optional< PrimType > classify(QualType Ty) const
unsigned getCharWidth() const
Program & P
Program to link to.
Holds all information required to evaluate constexpr code in a module.
std::function< bool()> InitFnRef
VariableScope * Parent
Link to the parent scope.
typename Emitter::AddrTy AddrTy
llvm::Optional< PrimType > classify(const Expr *E) const
Classifies a type.
Scope chain managing the variable lifetimes.
Information about a local's storage.
void addExtended(const Scope::Local &Local) override
Optional< unsigned > Idx
Index of the scope in the chain.
llvm::Optional< uint64_t > ArrayIndex
Current argument index.
ByteCodeExprGen< Emitter > * Ctx
ByteCodeExprGen instance.
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.
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Scope for local variables.
LocalScope(ByteCodeExprGen< Emitter > *Ctx)
virtual void addLocal(const Scope::Local &Local)
A builtin binary operation expression such as "x + y" or "x <= y".
void add(const Scope::Local &Local, bool IsExtended)
void addExtended(const Scope::Local &Local) override
void emitDestruction() override
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Record * getRecord(QualType Ty)
Returns a record from a record or pointer type.
Scope used to handle temporaries in toplevel variable declarations.
Scope for storage declared in a compound statement.
bool visitThisInitializer(const Expr *I)
Visits a delegated initializer.
PrimType
Enumeration of the primitive types of the VM.
bool visitExpr(const Expr *E) override
VariableScope * getParent()
llvm::DenseMap< const ValueDecl *, Scope::Local > Locals
Variable to storage mapping.
bool(ByteCodeExprGen::*)(PrimType, const SourceInfo &) UnaryFn
bool DiscardResult
Flag indicating if return value is to be discarded.
Represents a variable declaration or definition.
Describes the statement/declaration an opcode was generated from.
bool VisitParenExpr(const ParenExpr *E)
bool visitInitializer(const Expr *E, InitFnRef GenPtr)
Compiles an initializer for a local.
PrimType classifyPrim(QualType Ty) const
Classifies a known primitive type.
bool needsAdjust(QualType Ty) const
Checks if a pointer needs adjustment.
const RecordType * getRecordTy(QualType Ty)
Returns a record type from a record or pointer type.
bool VisitIntegerLiteral(const IntegerLiteral *E)
ByteCodeExprGen(Context &Ctx, Program &P, Tys &&... Args)
Initializes the compiler and the backend emitter.
VariableScope(ByteCodeExprGen< Emitter > *Ctx)
bool visitGlobalInitializer(const Expr *Init, unsigned I)
Visits an initializer for a global.
unsigned getCharBit() const
Returns the value of CHAR_BIT.
llvm::Optional< InitFnRef > InitFn
Expression being initialized.
typename Emitter::LabelTy LabelTy
ParenExpr - This represents a parethesized expression, e.g.
Decl - This represents one declaration (or definition), e.g.
VariableScope< Emitter > * VarScope
Current scope.
ConstStmtVisitor - This class implements a simple visitor for Stmt subclasses.
const TargetInfo & getTargetInfo() const
bool VisitCastExpr(const CastExpr *E)
ASTContext & getASTContext() const
Returns the AST context.
ExprScope(ByteCodeExprGen< Emitter > *Ctx)
bool visitDecl(const VarDecl *VD) override
unsigned llvm::PointerUnion< const Decl *, const Expr * > DeclTy
bool(ByteCodeExprGen::*)(const SourceInfo &) NullaryFn
bool visit(const Expr *E)
Evaluates an expression and places result on stack.
The program contains and links the bytecode for all functions.
This represents one expression.
CastExpr - Base class for type casts, including both implicit casts (ImplicitCastExpr) and explicit c...
Structure/Class descriptor.
Represents a struct/union/class.
llvm::Optional< PrimType > classify(QualType T)
Classifies an expression.
BlockScope(ByteCodeExprGen< Emitter > *Ctx)
virtual void emitDestruction()