1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
//===--- EvalEmitter.cpp - Instruction emitter for the VM -------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "EvalEmitter.h"
#include "Context.h"
#include "IntegralAP.h"
#include "Interp.h"
#include "clang/AST/DeclCXX.h"
using namespace clang;
using namespace clang::interp;
EvalEmitter::EvalEmitter(Context &Ctx, Program &P, State &Parent,
InterpStack &Stk)
: Ctx(Ctx), P(P), S(Parent, P, Stk, Ctx, this), EvalResult(&Ctx) {
// Create a dummy frame for the interpreter which does not have locals.
S.Current =
new InterpFrame(S, /*Func=*/nullptr, /*Caller=*/nullptr, CodePtr(), 0);
}
EvalEmitter::~EvalEmitter() {
for (auto &[K, V] : Locals) {
Block *B = reinterpret_cast<Block *>(V.get());
if (B->isInitialized())
B->invokeDtor();
}
}
/// Clean up all our resources. This needs to done in failed evaluations before
/// we call InterpStack::clear(), because there might be a Pointer on the stack
/// pointing into a Block in the EvalEmitter.
void EvalEmitter::cleanup() { S.cleanup(); }
EvaluationResult EvalEmitter::interpretExpr(const Expr *E,
bool ConvertResultToRValue,
bool DestroyToplevelScope) {
S.setEvalLocation(E->getExprLoc());
this->ConvertResultToRValue = ConvertResultToRValue && !isa<ConstantExpr>(E);
this->CheckFullyInitialized = isa<ConstantExpr>(E);
EvalResult.setSource(E);
if (!this->visitExpr(E, DestroyToplevelScope)) {
// EvalResult may already have a result set, but something failed
// after that (e.g. evaluating destructors).
EvalResult.setInvalid();
}
return std::move(this->EvalResult);
}
EvaluationResult EvalEmitter::interpretDecl(const VarDecl *VD,
bool CheckFullyInitialized) {
this->CheckFullyInitialized = CheckFullyInitialized;
S.EvaluatingDecl = VD;
EvalResult.setSource(VD);
if (const Expr *Init = VD->getAnyInitializer()) {
QualType T = VD->getType();
this->ConvertResultToRValue = !Init->isGLValue() && !T->isPointerType() &&
!T->isObjCObjectPointerType();
} else
this->ConvertResultToRValue = false;
EvalResult.setSource(VD);
if (!this->visitDeclAndReturn(VD, S.inConstantContext()))
EvalResult.setInvalid();
S.EvaluatingDecl = nullptr;
updateGlobalTemporaries();
return std::move(this->EvalResult);
}
void EvalEmitter::emitLabel(LabelTy Label) { CurrentLabel = Label; }
EvalEmitter::LabelTy EvalEmitter::getLabel() { return NextLabel++; }
Scope::Local EvalEmitter::createLocal(Descriptor *D) {
// Allocate memory for a local.
auto Memory = std::make_unique<char[]>(sizeof(Block) + D->getAllocSize());
auto *B = new (Memory.get()) Block(Ctx.getEvalID(), D, /*isStatic=*/false);
B->invokeCtor();
// Initialize local variable inline descriptor.
InlineDescriptor &Desc = *reinterpret_cast<InlineDescriptor *>(B->rawData());
Desc.Desc = D;
Desc.Offset = sizeof(InlineDescriptor);
Desc.IsActive = true;
Desc.IsBase = false;
Desc.IsFieldMutable = false;
Desc.IsConst = false;
Desc.IsInitialized = false;
// Register the local.
unsigned Off = Locals.size();
Locals.insert({Off, std::move(Memory)});
return {Off, D};
}
bool EvalEmitter::jumpTrue(const LabelTy &Label) {
if (isActive()) {
if (S.Stk.pop<bool>())
ActiveLabel = Label;
}
return true;
}
bool EvalEmitter::jumpFalse(const LabelTy &Label) {
if (isActive()) {
if (!S.Stk.pop<bool>())
ActiveLabel = Label;
}
return true;
}
bool EvalEmitter::jump(const LabelTy &Label) {
if (isActive())
CurrentLabel = ActiveLabel = Label;
return true;
}
bool EvalEmitter::fallthrough(const LabelTy &Label) {
if (isActive())
ActiveLabel = Label;
CurrentLabel = Label;
return true;
}
template <PrimType OpType> bool EvalEmitter::emitRet(const SourceInfo &Info) {
if (!isActive())
return true;
using T = typename PrimConv<OpType>::T;
EvalResult.setValue(S.Stk.pop<T>().toAPValue(Ctx.getASTContext()));
return true;
}
template <> bool EvalEmitter::emitRet<PT_Ptr>(const SourceInfo &Info) {
if (!isActive())
return true;
const Pointer &Ptr = S.Stk.pop<Pointer>();
if (!EvalResult.checkReturnValue(S, Ctx, Ptr, Info))
return false;
if (CheckFullyInitialized && !EvalResult.checkFullyInitialized(S, Ptr))
return false;
// Implicitly convert lvalue to rvalue, if requested.
if (ConvertResultToRValue) {
if (!Ptr.isZero() && !Ptr.isDereferencable())
return false;
if (S.getLangOpts().CPlusPlus11 && Ptr.isBlockPointer() &&
!CheckFinalLoad(S, OpPC, Ptr)) {
return false;
}
// Never allow reading from a non-const pointer, unless the memory
// has been created in this evaluation.
if (!Ptr.isZero() && !Ptr.isConst() && Ptr.isBlockPointer() &&
Ptr.block()->getEvalID() != Ctx.getEvalID())
return false;
if (std::optional<APValue> V =
Ptr.toRValue(Ctx, EvalResult.getSourceType())) {
EvalResult.setValue(*V);
} else {
return false;
}
} else {
EvalResult.setValue(Ptr.toAPValue(Ctx.getASTContext()));
}
return true;
}
template <> bool EvalEmitter::emitRet<PT_FnPtr>(const SourceInfo &Info) {
if (!isActive())
return true;
// Function pointers cannot be converted to rvalues.
EvalResult.setFunctionPointer(S.Stk.pop<FunctionPointer>());
return true;
}
bool EvalEmitter::emitRetVoid(const SourceInfo &Info) {
EvalResult.setValid();
return true;
}
bool EvalEmitter::emitRetValue(const SourceInfo &Info) {
const auto &Ptr = S.Stk.pop<Pointer>();
if (!EvalResult.checkReturnValue(S, Ctx, Ptr, Info))
return false;
if (CheckFullyInitialized && !EvalResult.checkFullyInitialized(S, Ptr))
return false;
if (std::optional<APValue> APV =
Ptr.toRValue(S.getASTContext(), EvalResult.getSourceType())) {
EvalResult.setValue(*APV);
return true;
}
EvalResult.setInvalid();
return false;
}
bool EvalEmitter::emitGetPtrLocal(uint32_t I, const SourceInfo &Info) {
if (!isActive())
return true;
Block *B = getLocal(I);
S.Stk.push<Pointer>(B, sizeof(InlineDescriptor));
return true;
}
template <PrimType OpType>
bool EvalEmitter::emitGetLocal(uint32_t I, const SourceInfo &Info) {
if (!isActive())
return true;
using T = typename PrimConv<OpType>::T;
Block *B = getLocal(I);
S.Stk.push<T>(*reinterpret_cast<T *>(B->data()));
return true;
}
template <PrimType OpType>
bool EvalEmitter::emitSetLocal(uint32_t I, const SourceInfo &Info) {
if (!isActive())
return true;
using T = typename PrimConv<OpType>::T;
Block *B = getLocal(I);
*reinterpret_cast<T *>(B->data()) = S.Stk.pop<T>();
InlineDescriptor &Desc = *reinterpret_cast<InlineDescriptor *>(B->rawData());
Desc.IsInitialized = true;
return true;
}
bool EvalEmitter::emitDestroy(uint32_t I, const SourceInfo &Info) {
if (!isActive())
return true;
for (auto &Local : Descriptors[I]) {
Block *B = getLocal(Local.Offset);
S.deallocate(B);
}
return true;
}
/// Global temporaries (LifetimeExtendedTemporary) carry their value
/// around as an APValue, which codegen accesses.
/// We set their value once when creating them, but we don't update it
/// afterwards when code changes it later.
/// This is what we do here.
void EvalEmitter::updateGlobalTemporaries() {
for (const auto &[E, Temp] : S.SeenGlobalTemporaries) {
if (std::optional<unsigned> GlobalIndex = P.getGlobal(E)) {
const Pointer &Ptr = P.getPtrGlobal(*GlobalIndex);
APValue *Cached = Temp->getOrCreateValue(true);
if (std::optional<PrimType> T = Ctx.classify(E->getType())) {
TYPE_SWITCH(
*T, { *Cached = Ptr.deref<T>().toAPValue(Ctx.getASTContext()); });
} else {
if (std::optional<APValue> APV =
Ptr.toRValue(Ctx, Temp->getTemporaryExpr()->getType()))
*Cached = *APV;
}
}
}
S.SeenGlobalTemporaries.clear();
}
//===----------------------------------------------------------------------===//
// Opcode evaluators
//===----------------------------------------------------------------------===//
#define GET_EVAL_IMPL
#include "Opcodes.inc"
#undef GET_EVAL_IMPL
|