Interpreter.cpp 3.0 KB

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