Interpreter.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 - Create a new interpreter object. This can never fail.
  28. ///
  29. ExecutionEngine *Interpreter::create(Module *M, std::string* ErrStr) {
  30. // Tell this Module to materialize everything and release the GVMaterializer.
  31. if (error_code EC = M->materializeAllPermanently()) {
  32. if (ErrStr)
  33. *ErrStr = EC.message();
  34. // We got an error, just return 0
  35. return 0;
  36. }
  37. return new Interpreter(M);
  38. }
  39. //===----------------------------------------------------------------------===//
  40. // Interpreter ctor - Initialize stuff
  41. //
  42. Interpreter::Interpreter(Module *M)
  43. : ExecutionEngine(M), TD(M) {
  44. memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
  45. setDataLayout(&TD);
  46. // Initialize the "backend"
  47. initializeExecutionEngine();
  48. initializeExternalFunctions();
  49. emitGlobals();
  50. IL = new IntrinsicLowering(TD);
  51. }
  52. Interpreter::~Interpreter() {
  53. delete IL;
  54. }
  55. void Interpreter::runAtExitHandlers () {
  56. while (!AtExitHandlers.empty()) {
  57. callFunction(AtExitHandlers.back(), std::vector<GenericValue>());
  58. AtExitHandlers.pop_back();
  59. run();
  60. }
  61. }
  62. /// run - Start execution with the specified function and arguments.
  63. ///
  64. GenericValue
  65. Interpreter::runFunction(Function *F,
  66. const std::vector<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. std::vector<GenericValue> ActualArgs;
  76. const unsigned ArgCount = F->getFunctionType()->getNumParams();
  77. for (unsigned i = 0; i < ArgCount; ++i)
  78. ActualArgs.push_back(ArgValues[i]);
  79. // Set up the function call.
  80. callFunction(F, ActualArgs);
  81. // Start executing the function.
  82. run();
  83. return ExitValue;
  84. }