EvalEmitter.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 =
  23. new InterpFrame(S, /*Func=*/nullptr, /*Caller=*/nullptr, CodePtr());
  24. }
  25. llvm::Expected<bool> EvalEmitter::interpretExpr(const Expr *E) {
  26. if (this->visitExpr(E))
  27. return true;
  28. if (BailLocation)
  29. return llvm::make_error<ByteCodeGenError>(*BailLocation);
  30. return false;
  31. }
  32. llvm::Expected<bool> EvalEmitter::interpretDecl(const VarDecl *VD) {
  33. if (this->visitDecl(VD))
  34. return true;
  35. if (BailLocation)
  36. return llvm::make_error<ByteCodeGenError>(*BailLocation);
  37. return false;
  38. }
  39. void EvalEmitter::emitLabel(LabelTy Label) {
  40. CurrentLabel = Label;
  41. }
  42. EvalEmitter::LabelTy EvalEmitter::getLabel() { return NextLabel++; }
  43. Scope::Local EvalEmitter::createLocal(Descriptor *D) {
  44. // Allocate memory for a local.
  45. auto Memory = std::make_unique<char[]>(sizeof(Block) + D->getAllocSize());
  46. auto *B = new (Memory.get()) Block(D, /*isStatic=*/false);
  47. B->invokeCtor();
  48. // Initialize local variable inline descriptor.
  49. InlineDescriptor &Desc = *reinterpret_cast<InlineDescriptor *>(B->rawData());
  50. Desc.Desc = D;
  51. Desc.Offset = sizeof(InlineDescriptor);
  52. Desc.IsActive = true;
  53. Desc.IsBase = false;
  54. Desc.IsFieldMutable = false;
  55. Desc.IsConst = false;
  56. Desc.IsInitialized = false;
  57. // Register the local.
  58. unsigned Off = Locals.size();
  59. Locals.insert({Off, std::move(Memory)});
  60. return {Off, D};
  61. }
  62. bool EvalEmitter::bail(const SourceLocation &Loc) {
  63. if (!BailLocation)
  64. BailLocation = Loc;
  65. return false;
  66. }
  67. bool EvalEmitter::jumpTrue(const LabelTy &Label) {
  68. if (isActive()) {
  69. if (S.Stk.pop<bool>())
  70. ActiveLabel = Label;
  71. }
  72. return true;
  73. }
  74. bool EvalEmitter::jumpFalse(const LabelTy &Label) {
  75. if (isActive()) {
  76. if (!S.Stk.pop<bool>())
  77. ActiveLabel = Label;
  78. }
  79. return true;
  80. }
  81. bool EvalEmitter::jump(const LabelTy &Label) {
  82. if (isActive())
  83. CurrentLabel = ActiveLabel = Label;
  84. return true;
  85. }
  86. bool EvalEmitter::fallthrough(const LabelTy &Label) {
  87. if (isActive())
  88. ActiveLabel = Label;
  89. CurrentLabel = Label;
  90. return true;
  91. }
  92. template <PrimType OpType> bool EvalEmitter::emitRet(const SourceInfo &Info) {
  93. if (!isActive())
  94. return true;
  95. using T = typename PrimConv<OpType>::T;
  96. return ReturnValue<T>(S.Stk.pop<T>(), Result);
  97. }
  98. bool EvalEmitter::emitRetVoid(const SourceInfo &Info) { return true; }
  99. bool EvalEmitter::emitRetValue(const SourceInfo &Info) {
  100. // Method to recursively traverse composites.
  101. std::function<bool(QualType, const Pointer &, APValue &)> Composite;
  102. Composite = [this, &Composite](QualType Ty, const Pointer &Ptr, APValue &R) {
  103. if (auto *AT = Ty->getAs<AtomicType>())
  104. Ty = AT->getValueType();
  105. if (auto *RT = Ty->getAs<RecordType>()) {
  106. auto *Record = Ptr.getRecord();
  107. assert(Record && "Missing record descriptor");
  108. bool Ok = true;
  109. if (RT->getDecl()->isUnion()) {
  110. const FieldDecl *ActiveField = nullptr;
  111. APValue Value;
  112. for (auto &F : Record->fields()) {
  113. const Pointer &FP = Ptr.atField(F.Offset);
  114. QualType FieldTy = F.Decl->getType();
  115. if (FP.isActive()) {
  116. if (std::optional<PrimType> T = Ctx.classify(FieldTy)) {
  117. TYPE_SWITCH(*T, Ok &= ReturnValue<T>(FP.deref<T>(), Value));
  118. } else {
  119. Ok &= Composite(FieldTy, FP, Value);
  120. }
  121. break;
  122. }
  123. }
  124. R = APValue(ActiveField, Value);
  125. } else {
  126. unsigned NF = Record->getNumFields();
  127. unsigned NB = Record->getNumBases();
  128. unsigned NV = Ptr.isBaseClass() ? 0 : Record->getNumVirtualBases();
  129. R = APValue(APValue::UninitStruct(), NB, NF);
  130. for (unsigned I = 0; I < NF; ++I) {
  131. const Record::Field *FD = Record->getField(I);
  132. QualType FieldTy = FD->Decl->getType();
  133. const Pointer &FP = Ptr.atField(FD->Offset);
  134. APValue &Value = R.getStructField(I);
  135. if (std::optional<PrimType> T = Ctx.classify(FieldTy)) {
  136. TYPE_SWITCH(*T, Ok &= ReturnValue<T>(FP.deref<T>(), Value));
  137. } else {
  138. Ok &= Composite(FieldTy, FP, Value);
  139. }
  140. }
  141. for (unsigned I = 0; I < NB; ++I) {
  142. const Record::Base *BD = Record->getBase(I);
  143. QualType BaseTy = Ctx.getASTContext().getRecordType(BD->Decl);
  144. const Pointer &BP = Ptr.atField(BD->Offset);
  145. Ok &= Composite(BaseTy, BP, R.getStructBase(I));
  146. }
  147. for (unsigned I = 0; I < NV; ++I) {
  148. const Record::Base *VD = Record->getVirtualBase(I);
  149. QualType VirtBaseTy = Ctx.getASTContext().getRecordType(VD->Decl);
  150. const Pointer &VP = Ptr.atField(VD->Offset);
  151. Ok &= Composite(VirtBaseTy, VP, R.getStructBase(NB + I));
  152. }
  153. }
  154. return Ok;
  155. }
  156. if (auto *AT = Ty->getAsArrayTypeUnsafe()) {
  157. const size_t NumElems = Ptr.getNumElems();
  158. QualType ElemTy = AT->getElementType();
  159. R = APValue(APValue::UninitArray{}, NumElems, NumElems);
  160. bool Ok = true;
  161. for (unsigned I = 0; I < NumElems; ++I) {
  162. APValue &Slot = R.getArrayInitializedElt(I);
  163. const Pointer &EP = Ptr.atIndex(I);
  164. if (std::optional<PrimType> T = Ctx.classify(ElemTy)) {
  165. TYPE_SWITCH(*T, Ok &= ReturnValue<T>(EP.deref<T>(), Slot));
  166. } else {
  167. Ok &= Composite(ElemTy, EP.narrow(), Slot);
  168. }
  169. }
  170. return Ok;
  171. }
  172. llvm_unreachable("invalid value to return");
  173. };
  174. // Return the composite type.
  175. const auto &Ptr = S.Stk.pop<Pointer>();
  176. return Composite(Ptr.getType(), Ptr, Result);
  177. }
  178. bool EvalEmitter::emitGetPtrLocal(uint32_t I, const SourceInfo &Info) {
  179. if (!isActive())
  180. return true;
  181. auto It = Locals.find(I);
  182. assert(It != Locals.end() && "Missing local variable");
  183. Block *B = reinterpret_cast<Block *>(It->second.get());
  184. S.Stk.push<Pointer>(B, sizeof(InlineDescriptor));
  185. return true;
  186. }
  187. template <PrimType OpType>
  188. bool EvalEmitter::emitGetLocal(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. S.Stk.push<T>(*reinterpret_cast<T *>(B->data()));
  196. return true;
  197. }
  198. template <PrimType OpType>
  199. bool EvalEmitter::emitSetLocal(uint32_t I, const SourceInfo &Info) {
  200. if (!isActive())
  201. return true;
  202. using T = typename PrimConv<OpType>::T;
  203. auto It = Locals.find(I);
  204. assert(It != Locals.end() && "Missing local variable");
  205. auto *B = reinterpret_cast<Block *>(It->second.get());
  206. *reinterpret_cast<T *>(B->data()) = S.Stk.pop<T>();
  207. InlineDescriptor &Desc = *reinterpret_cast<InlineDescriptor *>(B->rawData());
  208. Desc.IsInitialized = true;
  209. return true;
  210. }
  211. bool EvalEmitter::emitDestroy(uint32_t I, const SourceInfo &Info) {
  212. if (!isActive())
  213. return true;
  214. for (auto &Local : Descriptors[I]) {
  215. auto It = Locals.find(Local.Offset);
  216. assert(It != Locals.end() && "Missing local variable");
  217. S.deallocate(reinterpret_cast<Block *>(It->second.get()));
  218. }
  219. return true;
  220. }
  221. //===----------------------------------------------------------------------===//
  222. // Opcode evaluators
  223. //===----------------------------------------------------------------------===//
  224. #define GET_EVAL_IMPL
  225. #include "Opcodes.inc"
  226. #undef GET_EVAL_IMPL