Interpreter.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //===- Interpreter.cpp - Top-Level LLVM Interpreter Implementation --------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file was developed by the LLVM research group and is distributed under
  6. // the University of Illinois Open Source 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. using namespace llvm;
  20. /// create - Create a new interpreter object. This can never fail.
  21. ///
  22. ExecutionEngine *Interpreter::create(Module *M, IntrinsicLowering *IL) {
  23. bool isLittleEndian = false;
  24. switch (M->getEndianness()) {
  25. case Module::LittleEndian: isLittleEndian = true; break;
  26. case Module::BigEndian: isLittleEndian = false; break;
  27. case Module::AnyPointerSize:
  28. int Test = 0;
  29. *(char*)&Test = 1; // Return true if the host is little endian
  30. isLittleEndian = (Test == 1);
  31. break;
  32. }
  33. bool isLongPointer = false;
  34. switch (M->getPointerSize()) {
  35. case Module::Pointer32: isLongPointer = false; break;
  36. case Module::Pointer64: isLongPointer = true; break;
  37. case Module::AnyPointerSize:
  38. isLongPointer = (sizeof(void*) == 8); // Follow host
  39. break;
  40. }
  41. return new Interpreter(M, isLittleEndian, isLongPointer, IL);
  42. }
  43. //===----------------------------------------------------------------------===//
  44. // Interpreter ctor - Initialize stuff
  45. //
  46. Interpreter::Interpreter(Module *M, bool isLittleEndian, bool isLongPointer,
  47. IntrinsicLowering *il)
  48. : ExecutionEngine(M), ExitCode(0),
  49. TD("lli", isLittleEndian, isLongPointer ? 8 : 4, isLongPointer ? 8 : 4,
  50. isLongPointer ? 8 : 4), IL(il) {
  51. setTargetData(TD);
  52. // Initialize the "backend"
  53. initializeExecutionEngine();
  54. initializeExternalFunctions();
  55. emitGlobals();
  56. if (IL == 0) IL = new DefaultIntrinsicLowering();
  57. }
  58. Interpreter::~Interpreter() {
  59. delete IL;
  60. }
  61. void Interpreter::runAtExitHandlers () {
  62. while (!AtExitHandlers.empty()) {
  63. callFunction(AtExitHandlers.back(), std::vector<GenericValue>());
  64. AtExitHandlers.pop_back();
  65. run();
  66. }
  67. }
  68. /// run - Start execution with the specified function and arguments.
  69. ///
  70. GenericValue
  71. Interpreter::runFunction(Function *F,
  72. const std::vector<GenericValue> &ArgValues) {
  73. assert (F && "Function *F was null at entry to run()");
  74. // Try extra hard not to pass extra args to a function that isn't
  75. // expecting them. C programmers frequently bend the rules and
  76. // declare main() with fewer parameters than it actually gets
  77. // passed, and the interpreter barfs if you pass a function more
  78. // parameters than it is declared to take. This does not attempt to
  79. // take into account gratuitous differences in declared types,
  80. // though.
  81. std::vector<GenericValue> ActualArgs;
  82. const unsigned ArgCount = F->getFunctionType()->getNumParams();
  83. for (unsigned i = 0; i < ArgCount; ++i)
  84. ActualArgs.push_back(ArgValues[i]);
  85. // Set up the function call.
  86. callFunction(F, ActualArgs);
  87. // Start executing the function.
  88. run();
  89. GenericValue rv;
  90. rv.IntVal = ExitCode;
  91. return rv;
  92. }