17330f729Sjoerg //===--- Context.cpp - Context for the constexpr VM -------------*- C++ -*-===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg
97330f729Sjoerg #include "Context.h"
107330f729Sjoerg #include "ByteCodeEmitter.h"
117330f729Sjoerg #include "ByteCodeExprGen.h"
127330f729Sjoerg #include "ByteCodeStmtGen.h"
137330f729Sjoerg #include "EvalEmitter.h"
147330f729Sjoerg #include "Interp.h"
157330f729Sjoerg #include "InterpFrame.h"
167330f729Sjoerg #include "InterpStack.h"
177330f729Sjoerg #include "PrimType.h"
187330f729Sjoerg #include "Program.h"
197330f729Sjoerg #include "clang/AST/Expr.h"
20*e038c9c4Sjoerg #include "clang/Basic/TargetInfo.h"
217330f729Sjoerg
227330f729Sjoerg using namespace clang;
237330f729Sjoerg using namespace clang::interp;
247330f729Sjoerg
Context(ASTContext & Ctx)25*e038c9c4Sjoerg Context::Context(ASTContext &Ctx) : Ctx(Ctx), P(new Program(*this)) {}
267330f729Sjoerg
~Context()277330f729Sjoerg Context::~Context() {}
287330f729Sjoerg
isPotentialConstantExpr(State & Parent,const FunctionDecl * FD)29*e038c9c4Sjoerg bool Context::isPotentialConstantExpr(State &Parent, const FunctionDecl *FD) {
307330f729Sjoerg Function *Func = P->getFunction(FD);
317330f729Sjoerg if (!Func) {
327330f729Sjoerg if (auto R = ByteCodeStmtGen<ByteCodeEmitter>(*this, *P).compileFunc(FD)) {
337330f729Sjoerg Func = *R;
34*e038c9c4Sjoerg } else {
357330f729Sjoerg handleAllErrors(R.takeError(), [&Parent](ByteCodeGenError &Err) {
367330f729Sjoerg Parent.FFDiag(Err.getLoc(), diag::err_experimental_clang_interp_failed);
377330f729Sjoerg });
38*e038c9c4Sjoerg return false;
397330f729Sjoerg }
407330f729Sjoerg }
417330f729Sjoerg
427330f729Sjoerg if (!Func->isConstexpr())
43*e038c9c4Sjoerg return false;
447330f729Sjoerg
457330f729Sjoerg APValue Dummy;
467330f729Sjoerg return Run(Parent, Func, Dummy);
477330f729Sjoerg }
487330f729Sjoerg
evaluateAsRValue(State & Parent,const Expr * E,APValue & Result)49*e038c9c4Sjoerg bool Context::evaluateAsRValue(State &Parent, const Expr *E, APValue &Result) {
507330f729Sjoerg ByteCodeExprGen<EvalEmitter> C(*this, *P, Parent, Stk, Result);
517330f729Sjoerg return Check(Parent, C.interpretExpr(E));
527330f729Sjoerg }
537330f729Sjoerg
evaluateAsInitializer(State & Parent,const VarDecl * VD,APValue & Result)54*e038c9c4Sjoerg bool Context::evaluateAsInitializer(State &Parent, const VarDecl *VD,
557330f729Sjoerg APValue &Result) {
567330f729Sjoerg ByteCodeExprGen<EvalEmitter> C(*this, *P, Parent, Stk, Result);
577330f729Sjoerg return Check(Parent, C.interpretDecl(VD));
587330f729Sjoerg }
597330f729Sjoerg
getLangOpts() const607330f729Sjoerg const LangOptions &Context::getLangOpts() const { return Ctx.getLangOpts(); }
617330f729Sjoerg
classify(QualType T)627330f729Sjoerg llvm::Optional<PrimType> Context::classify(QualType T) {
637330f729Sjoerg if (T->isReferenceType() || T->isPointerType()) {
647330f729Sjoerg return PT_Ptr;
657330f729Sjoerg }
667330f729Sjoerg
677330f729Sjoerg if (T->isBooleanType())
687330f729Sjoerg return PT_Bool;
697330f729Sjoerg
707330f729Sjoerg if (T->isSignedIntegerOrEnumerationType()) {
717330f729Sjoerg switch (Ctx.getIntWidth(T)) {
727330f729Sjoerg case 64:
737330f729Sjoerg return PT_Sint64;
747330f729Sjoerg case 32:
757330f729Sjoerg return PT_Sint32;
767330f729Sjoerg case 16:
777330f729Sjoerg return PT_Sint16;
787330f729Sjoerg case 8:
797330f729Sjoerg return PT_Sint8;
807330f729Sjoerg default:
817330f729Sjoerg return {};
827330f729Sjoerg }
837330f729Sjoerg }
847330f729Sjoerg
857330f729Sjoerg if (T->isUnsignedIntegerOrEnumerationType()) {
867330f729Sjoerg switch (Ctx.getIntWidth(T)) {
877330f729Sjoerg case 64:
887330f729Sjoerg return PT_Uint64;
897330f729Sjoerg case 32:
907330f729Sjoerg return PT_Uint32;
917330f729Sjoerg case 16:
927330f729Sjoerg return PT_Uint16;
937330f729Sjoerg case 8:
947330f729Sjoerg return PT_Uint8;
957330f729Sjoerg default:
967330f729Sjoerg return {};
977330f729Sjoerg }
987330f729Sjoerg }
997330f729Sjoerg
1007330f729Sjoerg if (T->isNullPtrType())
1017330f729Sjoerg return PT_Ptr;
1027330f729Sjoerg
1037330f729Sjoerg if (auto *AT = dyn_cast<AtomicType>(T))
1047330f729Sjoerg return classify(AT->getValueType());
1057330f729Sjoerg
1067330f729Sjoerg return {};
1077330f729Sjoerg }
1087330f729Sjoerg
getCharBit() const1097330f729Sjoerg unsigned Context::getCharBit() const {
1107330f729Sjoerg return Ctx.getTargetInfo().getCharWidth();
1117330f729Sjoerg }
1127330f729Sjoerg
Run(State & Parent,Function * Func,APValue & Result)113*e038c9c4Sjoerg bool Context::Run(State &Parent, Function *Func, APValue &Result) {
1147330f729Sjoerg InterpState State(Parent, *P, Stk, *this);
1157330f729Sjoerg State.Current = new InterpFrame(State, Func, nullptr, {}, {});
116*e038c9c4Sjoerg if (Interpret(State, Result))
117*e038c9c4Sjoerg return true;
1187330f729Sjoerg Stk.clear();
119*e038c9c4Sjoerg return false;
1207330f729Sjoerg }
1217330f729Sjoerg
Check(State & Parent,llvm::Expected<bool> && Flag)122*e038c9c4Sjoerg bool Context::Check(State &Parent, llvm::Expected<bool> &&Flag) {
123*e038c9c4Sjoerg if (Flag)
124*e038c9c4Sjoerg return *Flag;
125*e038c9c4Sjoerg handleAllErrors(Flag.takeError(), [&Parent](ByteCodeGenError &Err) {
1267330f729Sjoerg Parent.FFDiag(Err.getLoc(), diag::err_experimental_clang_interp_failed);
1277330f729Sjoerg });
128*e038c9c4Sjoerg return false;
1297330f729Sjoerg }
130