Interpreter.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/DerivedTypes.h"
  18. #include "llvm/Module.h"
  19. #include "llvm/ModuleProvider.h"
  20. #include <cstring>
  21. using namespace llvm;
  22. static struct RegisterInterp {
  23. RegisterInterp() { Interpreter::Register(); }
  24. } InterpRegistrator;
  25. namespace llvm {
  26. void LinkInInterpreter() {
  27. }
  28. }
  29. /// create - Create a new interpreter object. This can never fail.
  30. ///
  31. ExecutionEngine *Interpreter::create(ModuleProvider *MP, std::string* ErrStr) {
  32. // Tell this ModuleProvide to materialize and release the module
  33. if (!MP->materializeModule(ErrStr))
  34. // We got an error, just return 0
  35. return 0;
  36. return new Interpreter(MP);
  37. }
  38. //===----------------------------------------------------------------------===//
  39. // Interpreter ctor - Initialize stuff
  40. //
  41. Interpreter::Interpreter(ModuleProvider *M)
  42. : ExecutionEngine(M), TD(M->getModule()) {
  43. memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
  44. setTargetData(&TD);
  45. // Initialize the "backend"
  46. initializeExecutionEngine();
  47. initializeExternalFunctions();
  48. emitGlobals();
  49. IL = new IntrinsicLowering(TD);
  50. }
  51. Interpreter::~Interpreter() {
  52. delete IL;
  53. }
  54. void Interpreter::runAtExitHandlers () {
  55. while (!AtExitHandlers.empty()) {
  56. callFunction(AtExitHandlers.back(), std::vector<GenericValue>());
  57. AtExitHandlers.pop_back();
  58. run();
  59. }
  60. }
  61. /// run - Start execution with the specified function and arguments.
  62. ///
  63. GenericValue
  64. Interpreter::runFunction(Function *F,
  65. const std::vector<GenericValue> &ArgValues) {
  66. assert (F && "Function *F was null at entry to run()");
  67. // Try extra hard not to pass extra args to a function that isn't
  68. // expecting them. C programmers frequently bend the rules and
  69. // declare main() with fewer parameters than it actually gets
  70. // passed, and the interpreter barfs if you pass a function more
  71. // parameters than it is declared to take. This does not attempt to
  72. // take into account gratuitous differences in declared types,
  73. // though.
  74. std::vector<GenericValue> ActualArgs;
  75. const unsigned ArgCount = F->getFunctionType()->getNumParams();
  76. for (unsigned i = 0; i < ArgCount; ++i)
  77. ActualArgs.push_back(ArgValues[i]);
  78. // Set up the function call.
  79. callFunction(F, ActualArgs);
  80. // Start executing the function.
  81. run();
  82. return ExitValue;
  83. }