Interpreter.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 (std::error_code EC = M->materializeAllPermanently()) {
  33. if (ErrStr)
  34. *ErrStr = EC.message();
  35. // We got an error, just return 0
  36. return nullptr;
  37. }
  38. return new Interpreter(std::move(M));
  39. }
  40. //===----------------------------------------------------------------------===//
  41. // Interpreter ctor - Initialize stuff
  42. //
  43. Interpreter::Interpreter(std::unique_ptr<Module> M)
  44. : ExecutionEngine(std::move(M)), TD(Modules.back().get()) {
  45. memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
  46. setDataLayout(&TD);
  47. // Initialize the "backend"
  48. initializeExecutionEngine();
  49. initializeExternalFunctions();
  50. emitGlobals();
  51. IL = new IntrinsicLowering(TD);
  52. }
  53. Interpreter::~Interpreter() {
  54. delete IL;
  55. }
  56. void Interpreter::runAtExitHandlers () {
  57. while (!AtExitHandlers.empty()) {
  58. callFunction(AtExitHandlers.back(), None);
  59. AtExitHandlers.pop_back();
  60. run();
  61. }
  62. }
  63. /// run - Start execution with the specified function and arguments.
  64. ///
  65. GenericValue Interpreter::runFunction(Function *F,
  66. ArrayRef<GenericValue> ArgValues) {
  67. assert (F && "Function *F was null at entry to run()");
  68. // Try extra hard not to pass extra args to a function that isn't
  69. // expecting them. C programmers frequently bend the rules and
  70. // declare main() with fewer parameters than it actually gets
  71. // passed, and the interpreter barfs if you pass a function more
  72. // parameters than it is declared to take. This does not attempt to
  73. // take into account gratuitous differences in declared types,
  74. // though.
  75. const size_t ArgCount = F->getFunctionType()->getNumParams();
  76. ArrayRef<GenericValue> ActualArgs =
  77. ArgValues.slice(0, std::min(ArgValues.size(), ArgCount));
  78. // Set up the function call.
  79. callFunction(F, ActualArgs);
  80. // Start executing the function.
  81. run();
  82. return ExitValue;
  83. }