Interpreter.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //===- Interpreter.cpp - Top-Level LLVM Interpreter Implementation --------===//
  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. //
  9. // This file implements the top-level functionality for the LLVM interpreter.
  10. // This interpreter is designed to be a very simple, portable, inefficient
  11. // interpreter.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "Interpreter.h"
  15. #include "llvm/CodeGen/IntrinsicLowering.h"
  16. #include "llvm/IR/DerivedTypes.h"
  17. #include "llvm/IR/Module.h"
  18. #include <cstring>
  19. using namespace llvm;
  20. namespace {
  21. static struct RegisterInterp {
  22. RegisterInterp() { Interpreter::Register(); }
  23. } InterpRegistrator;
  24. }
  25. extern "C" void LLVMLinkInInterpreter() { }
  26. /// Create a new interpreter object.
  27. ///
  28. ExecutionEngine *Interpreter::create(std::unique_ptr<Module> M,
  29. std::string *ErrStr) {
  30. // Tell this Module to materialize everything and release the GVMaterializer.
  31. if (Error Err = M->materializeAll()) {
  32. std::string Msg;
  33. handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
  34. Msg = EIB.message();
  35. });
  36. if (ErrStr)
  37. *ErrStr = Msg;
  38. // We got an error, just return 0
  39. return nullptr;
  40. }
  41. return new Interpreter(std::move(M));
  42. }
  43. //===----------------------------------------------------------------------===//
  44. // Interpreter ctor - Initialize stuff
  45. //
  46. Interpreter::Interpreter(std::unique_ptr<Module> M)
  47. : ExecutionEngine(std::move(M)) {
  48. memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
  49. // Initialize the "backend"
  50. initializeExecutionEngine();
  51. initializeExternalFunctions();
  52. emitGlobals();
  53. IL = new IntrinsicLowering(getDataLayout());
  54. }
  55. Interpreter::~Interpreter() {
  56. delete IL;
  57. }
  58. void Interpreter::runAtExitHandlers () {
  59. while (!AtExitHandlers.empty()) {
  60. callFunction(AtExitHandlers.back(), None);
  61. AtExitHandlers.pop_back();
  62. run();
  63. }
  64. }
  65. /// run - Start execution with the specified function and arguments.
  66. ///
  67. GenericValue Interpreter::runFunction(Function *F,
  68. ArrayRef<GenericValue> ArgValues) {
  69. assert (F && "Function *F was null at entry to run()");
  70. // Try extra hard not to pass extra args to a function that isn't
  71. // expecting them. C programmers frequently bend the rules and
  72. // declare main() with fewer parameters than it actually gets
  73. // passed, and the interpreter barfs if you pass a function more
  74. // parameters than it is declared to take. This does not attempt to
  75. // take into account gratuitous differences in declared types,
  76. // though.
  77. const size_t ArgCount = F->getFunctionType()->getNumParams();
  78. ArrayRef<GenericValue> ActualArgs =
  79. ArgValues.slice(0, std::min(ArgValues.size(), ArgCount));
  80. // Set up the function call.
  81. callFunction(F, ActualArgs);
  82. // Start executing the function.
  83. run();
  84. return ExitValue;
  85. }