Interpreter.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #ifdef __APPLE__
  21. #include <TargetConditionals.h>
  22. #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
  23. #include "ios_error.h"
  24. #include "llvm/Support/DynamicLibrary.h"
  25. #endif
  26. #endif
  27. using namespace llvm;
  28. namespace {
  29. static struct RegisterInterp {
  30. RegisterInterp() { Interpreter::Register(); }
  31. } InterpRegistrator;
  32. }
  33. extern "C" void LLVMLinkInInterpreter() { }
  34. /// Create a new interpreter object.
  35. ///
  36. ExecutionEngine *Interpreter::create(std::unique_ptr<Module> M,
  37. std::string *ErrStr) {
  38. // Tell this Module to materialize everything and release the GVMaterializer.
  39. if (Error Err = M->materializeAll()) {
  40. std::string Msg;
  41. handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
  42. Msg = EIB.message();
  43. });
  44. if (ErrStr)
  45. *ErrStr = Msg;
  46. // We got an error, just return 0
  47. return nullptr;
  48. }
  49. return new Interpreter(std::move(M));
  50. }
  51. //===----------------------------------------------------------------------===//
  52. // Interpreter ctor - Initialize stuff
  53. //
  54. Interpreter::Interpreter(std::unique_ptr<Module> M)
  55. : ExecutionEngine(std::move(M)) {
  56. memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
  57. // Initialize the "backend"
  58. initializeExecutionEngine();
  59. initializeExternalFunctions();
  60. #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
  61. // Works without it if you don't need the ios_system modifications
  62. // (writing to thread_stdout, calling system()...)
  63. llvm::sys::DynamicLibrary::LoadLibraryPermanently("libc++.1.dylib");
  64. llvm::sys::DynamicLibrary::LoadLibraryPermanently("libc++abi.1.dylib");
  65. #endif
  66. emitGlobals();
  67. IL = new IntrinsicLowering(getDataLayout());
  68. }
  69. Interpreter::~Interpreter() {
  70. delete IL;
  71. }
  72. void Interpreter::runAtExitHandlers () {
  73. while (!AtExitHandlers.empty()) {
  74. callFunction(AtExitHandlers.back(), None);
  75. AtExitHandlers.pop_back();
  76. run();
  77. }
  78. }
  79. /// run - Start execution with the specified function and arguments.
  80. ///
  81. GenericValue Interpreter::runFunction(Function *F,
  82. ArrayRef<GenericValue> ArgValues) {
  83. assert (F && "Function *F was null at entry to run()");
  84. // Try extra hard not to pass extra args to a function that isn't
  85. // expecting them. C programmers frequently bend the rules and
  86. // declare main() with fewer parameters than it actually gets
  87. // passed, and the interpreter barfs if you pass a function more
  88. // parameters than it is declared to take. This does not attempt to
  89. // take into account gratuitous differences in declared types,
  90. // though.
  91. const size_t ArgCount = F->getFunctionType()->getNumParams();
  92. ArrayRef<GenericValue> ActualArgs =
  93. ArgValues.slice(0, std::min(ArgValues.size(), ArgCount));
  94. // Set up the function call.
  95. callFunction(F, ActualArgs);
  96. // Start executing the function.
  97. run();
  98. return ExitValue;
  99. }