EvalEmitter.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //===--- EvalEmitter.cpp - Instruction emitter for the VM -------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "EvalEmitter.h"
  9. #include "Context.h"
  10. #include "Interp.h"
  11. #include "Opcode.h"
  12. #include "Program.h"
  13. #include "clang/AST/DeclCXX.h"
  14. using namespace clang;
  15. using namespace clang::interp;
  16. using APSInt = llvm::APSInt;
  17. template <typename T> using Expected = llvm::Expected<T>;
  18. EvalEmitter::EvalEmitter(Context &Ctx, Program &P, State &Parent,
  19. InterpStack &Stk, APValue &Result)
  20. : Ctx(Ctx), P(P), S(Parent, P, Stk, Ctx, this), Result(Result) {
  21. // Create a dummy frame for the interpreter which does not have locals.
  22. S.Current = new InterpFrame(S, nullptr, nullptr, CodePtr(), Pointer());
  23. }
  24. llvm::Expected<bool> EvalEmitter::interpretExpr(const Expr *E) {
  25. if (this->visitExpr(E))
  26. return true;
  27. if (BailLocation)
  28. return llvm::make_error<ByteCodeGenError>(*BailLocation);
  29. return false;
  30. }
  31. llvm::Expected<bool> EvalEmitter::interpretDecl(const VarDecl *VD) {
  32. if (this->visitDecl(VD))
  33. return true;
  34. if (BailLocation)
  35. return llvm::make_error<ByteCodeGenError>(*BailLocation);
  36. return false;
  37. }
  38. void EvalEmitter::emitLabel(LabelTy Label) {
  39. CurrentLabel = Label;
  40. }
  41. EvalEmitter::LabelTy EvalEmitter::getLabel() { return NextLabel++; }
  42. Scope::Local EvalEmitter::createLocal(Descriptor *D) {
  43. // Allocate memory for a local.
  44. auto Memory = std::make_unique<char[]>(sizeof(Block) + D->getAllocSize());
  45. auto *B = new (Memory.get()) Block(D, /*isStatic=*/false);
  46. B->invokeCtor();
  47. // Register the local.
  48. unsigned Off = Locals.size();
  49. Locals.insert({Off, std::move(Memory)});
  50. return {Off, D};
  51. }
  52. bool EvalEmitter::bail(const SourceLocation &Loc) {
  53. if (!BailLocation)
  54. BailLocation = Loc;
  55. return false;
  56. }
  57. bool EvalEmitter::jumpTrue(const LabelTy &Label) {
  58. if (isActive()) {
  59. if (S.Stk.pop<bool>())
  60. ActiveLabel = Label;
  61. }
  62. return true;
  63. }
  64. bool EvalEmitter::jumpFalse(const LabelTy &Label) {
  65. if (isActive()) {
  66. if (!S.Stk.pop<bool>())
  67. ActiveLabel = Label;
  68. }
  69. return true;
  70. }
  71. bool EvalEmitter::jump(const LabelTy &Label) {
  72. if (isActive())
  73. CurrentLabel = ActiveLabel = Label;
  74. return true;
  75. }
  76. bool EvalEmitter::fallthrough(const LabelTy &Label) {
  77. if (isActive())
  78. ActiveLabel = Label;
  79. CurrentLabel = Label;
  80. return true;
  81. }
  82. template <PrimType OpType> bool EvalEmitter::emitRet(const SourceInfo &Info) {
  83. if (!isActive())
  84. return true;
  85. using T = typename PrimConv<OpType>::T;
  86. return ReturnValue<T>(S.Stk.pop<T>(), Result);
  87. }
  88. bool EvalEmitter::emitRetVoid(const SourceInfo &Info) { return true; }
  89. bool EvalEmitter::emitRetValue(const SourceInfo &Info) {
  90. // Method to recursively traverse composites.
  91. std::function<bool(QualType, const Pointer &, APValue &)> Composite;
  92. Composite = [this, &Composite](QualType Ty, const Pointer &Ptr, APValue &R) {
  93. if (auto *AT = Ty->getAs<AtomicType>())
  94. Ty = AT->getValueType();
  95. if (auto *RT = Ty->getAs<RecordType>()) {
  96. auto *Record = Ptr.getRecord();
  97. assert(Record && "Missing record descriptor");
  98. bool Ok = true;
  99. if (RT->getDecl()->isUnion()) {
  100. const FieldDecl *ActiveField = nullptr;
  101. APValue Value;
  102. for (auto &F : Record->fields()) {
  103. const Pointer &FP = Ptr.atField(F.Offset);
  104. QualType FieldTy = F.Decl->getType();
  105. if (FP.isActive()) {
  106. if (llvm::Optional<PrimType> T = Ctx.classify(FieldTy)) {
  107. TYPE_SWITCH(*T, Ok &= ReturnValue<T>(FP.deref<T>(), Value));
  108. } else {
  109. Ok &= Composite(FieldTy, FP, Value);
  110. }
  111. break;
  112. }
  113. }
  114. R = APValue(ActiveField, Value);
  115. } else {
  116. unsigned NF = Record->getNumFields();
  117. unsigned NB = Record->getNumBases();
  118. unsigned NV = Ptr.isBaseClass() ? 0 : Record->getNumVirtualBases();
  119. R = APValue(APValue::UninitStruct(), NB, NF);
  120. for (unsigned I = 0; I < NF; ++I) {
  121. const Record::Field *FD = Record->getField(I);
  122. QualType FieldTy = FD->Decl->getType();
  123. const Pointer &FP = Ptr.atField(FD->Offset);
  124. APValue &Value = R.getStructField(I);
  125. if (llvm::Optional<PrimType> T = Ctx.classify(FieldTy)) {
  126. TYPE_SWITCH(*T, Ok &= ReturnValue<T>(FP.deref<T>(), Value));
  127. } else {
  128. Ok &= Composite(FieldTy, FP, Value);
  129. }
  130. }
  131. for (unsigned I = 0; I < NB; ++I) {
  132. const Record::Base *BD = Record->getBase(I);
  133. QualType BaseTy = Ctx.getASTContext().getRecordType(BD->Decl);
  134. const Pointer &BP = Ptr.atField(BD->Offset);
  135. Ok &= Composite(BaseTy, BP, R.getStructBase(I));
  136. }
  137. for (unsigned I = 0; I < NV; ++I) {
  138. const Record::Base *VD = Record->getVirtualBase(I);
  139. QualType VirtBaseTy = Ctx.getASTContext().getRecordType(VD->Decl);
  140. const Pointer &VP = Ptr.atField(VD->Offset);
  141. Ok &= Composite(VirtBaseTy, VP, R.getStructBase(NB + I));
  142. }
  143. }
  144. return Ok;
  145. }
  146. if (auto *AT = Ty->getAsArrayTypeUnsafe()) {
  147. const size_t NumElems = Ptr.getNumElems();
  148. QualType ElemTy = AT->getElementType();
  149. R = APValue(APValue::UninitArray{}, NumElems, NumElems);
  150. bool Ok = true;
  151. for (unsigned I = 0; I < NumElems; ++I) {
  152. APValue &Slot = R.getArrayInitializedElt(I);
  153. const Pointer &EP = Ptr.atIndex(I);
  154. if (llvm::Optional<PrimType> T = Ctx.classify(ElemTy)) {
  155. TYPE_SWITCH(*T, Ok &= ReturnValue<T>(EP.deref<T>(), Slot));
  156. } else {
  157. Ok &= Composite(ElemTy, EP.narrow(), Slot);
  158. }
  159. }
  160. return Ok;
  161. }
  162. llvm_unreachable("invalid value to return");
  163. };
  164. // Return the composite type.
  165. const auto &Ptr = S.Stk.pop<Pointer>();
  166. return Composite(Ptr.getType(), Ptr, Result);
  167. }
  168. bool EvalEmitter::emitGetPtrLocal(uint32_t I, const SourceInfo &Info) {
  169. if (!isActive())
  170. return true;
  171. auto It = Locals.find(I);
  172. assert(It != Locals.end() && "Missing local variable");
  173. S.Stk.push<Pointer>(reinterpret_cast<Block *>(It->second.get()));
  174. return true;
  175. }
  176. template <PrimType OpType>
  177. bool EvalEmitter::emitGetLocal(uint32_t I, const SourceInfo &Info) {
  178. if (!isActive())
  179. return true;
  180. using T = typename PrimConv<OpType>::T;
  181. auto It = Locals.find(I);
  182. assert(It != Locals.end() && "Missing local variable");
  183. auto *B = reinterpret_cast<Block *>(It->second.get());
  184. S.Stk.push<T>(*reinterpret_cast<T *>(B + 1));
  185. return true;
  186. }
  187. template <PrimType OpType>
  188. bool EvalEmitter::emitSetLocal(uint32_t I, const SourceInfo &Info) {
  189. if (!isActive())
  190. return true;
  191. using T = typename PrimConv<OpType>::T;
  192. auto It = Locals.find(I);
  193. assert(It != Locals.end() && "Missing local variable");
  194. auto *B = reinterpret_cast<Block *>(It->second.get());
  195. *reinterpret_cast<T *>(B + 1) = S.Stk.pop<T>();
  196. return true;
  197. }
  198. bool EvalEmitter::emitDestroy(uint32_t I, const SourceInfo &Info) {
  199. if (!isActive())
  200. return true;
  201. for (auto &Local : Descriptors[I]) {
  202. auto It = Locals.find(Local.Offset);
  203. assert(It != Locals.end() && "Missing local variable");
  204. S.deallocate(reinterpret_cast<Block *>(It->second.get()));
  205. }
  206. return true;
  207. }
  208. //===----------------------------------------------------------------------===//
  209. // Opcode evaluators
  210. //===----------------------------------------------------------------------===//
  211. #define GET_EVAL_IMPL
  212. #include "Opcodes.inc"
  213. #undef GET_EVAL_IMPL