Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 1 | //===- Interpreter.cpp - Top-Level LLVM Interpreter Implementation --------===// |
| 2 | // |
| 3 | // This file implements the top-level functionality for the LLVM interpreter. |
| 4 | // This interpreter is designed to be a very simple, portable, inefficient |
| 5 | // interpreter. |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "Interpreter.h" |
Chris Lattner | 39c0726 | 2003-08-24 19:50:53 +0000 | [diff] [blame] | 10 | #include "llvm/Module.h" |
Brian Gaeke | 413ab66 | 2003-09-05 04:46:26 +0000 | [diff] [blame^] | 11 | #include "llvm/DerivedTypes.h" |
| 12 | #include "llvm/Function.h" |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 13 | |
Brian Gaeke | 82d8277 | 2003-09-03 20:34:19 +0000 | [diff] [blame] | 14 | /// create - Create a new interpreter object. This can never fail. |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 15 | /// |
Brian Gaeke | f58815e | 2003-09-04 22:21:24 +0000 | [diff] [blame] | 16 | ExecutionEngine *Interpreter::create(Module *M, bool TraceMode){ |
Chris Lattner | 39c0726 | 2003-08-24 19:50:53 +0000 | [diff] [blame] | 17 | bool isLittleEndian; |
| 18 | switch (M->getEndianness()) { |
| 19 | case Module::LittleEndian: isLittleEndian = true; break; |
| 20 | case Module::BigEndian: isLittleEndian = false; break; |
| 21 | case Module::AnyPointerSize: |
| 22 | int Test = 0; |
| 23 | *(char*)&Test = 1; // Return true if the host is little endian |
| 24 | isLittleEndian = (Test == 1); |
| 25 | break; |
| 26 | } |
| 27 | |
| 28 | bool isLongPointer; |
| 29 | switch (M->getPointerSize()) { |
| 30 | case Module::Pointer32: isLongPointer = false; break; |
| 31 | case Module::Pointer64: isLongPointer = true; break; |
| 32 | case Module::AnyPointerSize: |
| 33 | isLongPointer = (sizeof(void*) == 8); // Follow host |
| 34 | break; |
| 35 | } |
| 36 | |
Brian Gaeke | f58815e | 2003-09-04 22:21:24 +0000 | [diff] [blame] | 37 | return new Interpreter(M, isLittleEndian, isLongPointer, TraceMode); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | // Interpreter ctor - Initialize stuff |
| 42 | // |
Chris Lattner | 39c0726 | 2003-08-24 19:50:53 +0000 | [diff] [blame] | 43 | Interpreter::Interpreter(Module *M, bool isLittleEndian, bool isLongPointer, |
Brian Gaeke | f58815e | 2003-09-04 22:21:24 +0000 | [diff] [blame] | 44 | bool TraceMode) |
| 45 | : ExecutionEngine(M), ExitCode(0), Trace(TraceMode), |
Chris Lattner | 39c0726 | 2003-08-24 19:50:53 +0000 | [diff] [blame] | 46 | CurFrame(-1), TD("lli", isLittleEndian, isLongPointer ? 8 : 4, |
| 47 | isLongPointer ? 8 : 4, isLongPointer ? 8 : 4) { |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 48 | |
| 49 | setTargetData(TD); |
| 50 | // Initialize the "backend" |
| 51 | initializeExecutionEngine(); |
Chris Lattner | da82ed5 | 2003-05-08 16:18:31 +0000 | [diff] [blame] | 52 | initializeExternalFunctions(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 53 | CW.setModule(M); // Update Writer |
Chris Lattner | 56adf15 | 2003-05-12 02:14:34 +0000 | [diff] [blame] | 54 | emitGlobals(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /// run - Start execution with the specified function and arguments. |
| 58 | /// |
| 59 | int Interpreter::run(const std::string &MainFunction, |
John Criswell | 69582b3 | 2003-08-21 21:12:30 +0000 | [diff] [blame] | 60 | const std::vector<std::string> &Args, |
| 61 | const char ** envp) { |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 62 | // Start interpreter into the main function... |
| 63 | // |
Brian Gaeke | f58815e | 2003-09-04 22:21:24 +0000 | [diff] [blame] | 64 | if (!callMainFunction(MainFunction, Args)) { |
| 65 | // If the call succeeded, run the code now... |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 66 | run(); |
| 67 | } |
| 68 | |
Chris Lattner | 44edb6b | 2003-05-14 14:21:30 +0000 | [diff] [blame] | 69 | do { |
Chris Lattner | 44edb6b | 2003-05-14 14:21:30 +0000 | [diff] [blame] | 70 | // If the program has exited, run atexit handlers... |
| 71 | if (ECStack.empty() && !AtExitHandlers.empty()) { |
| 72 | callFunction(AtExitHandlers.back(), std::vector<GenericValue>()); |
| 73 | AtExitHandlers.pop_back(); |
| 74 | run(); |
| 75 | } |
| 76 | } while (!ECStack.empty()); |
| 77 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 78 | return ExitCode; |
| 79 | } |
| 80 | |
Brian Gaeke | 413ab66 | 2003-09-05 04:46:26 +0000 | [diff] [blame^] | 81 | |
| 82 | // callMainFunction - Construct call to typical C main() function and |
| 83 | // call it using callFunction(). |
| 84 | // |
| 85 | bool Interpreter::callMainFunction(const std::string &Name, |
| 86 | const std::vector<std::string> &InputArgv) { |
| 87 | Function *M = getModule().getNamedFunction(Name); |
| 88 | if (M == 0) { |
| 89 | std::cerr << "Could not find function '" << Name << "' in module!\n"; |
| 90 | return 1; |
| 91 | } |
| 92 | const FunctionType *MT = M->getFunctionType(); |
| 93 | |
| 94 | std::vector<GenericValue> Args; |
| 95 | if (MT->getParamTypes().size() >= 2) { |
| 96 | PointerType *SPP = PointerType::get(PointerType::get(Type::SByteTy)); |
| 97 | if (MT->getParamTypes()[1] != SPP) { |
| 98 | CW << "Second argument of '" << Name << "' should have type: '" |
| 99 | << SPP << "'!\n"; |
| 100 | return true; |
| 101 | } |
| 102 | Args.push_back(PTOGV(CreateArgv(InputArgv))); |
| 103 | } |
| 104 | |
| 105 | if (MT->getParamTypes().size() >= 1) { |
| 106 | if (!MT->getParamTypes()[0]->isInteger()) { |
| 107 | std::cout << "First argument of '" << Name |
| 108 | << "' should be an integer!\n"; |
| 109 | return true; |
| 110 | } else { |
| 111 | GenericValue GV; GV.UIntVal = InputArgv.size(); |
| 112 | Args.insert(Args.begin(), GV); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | callFunction(M, Args); // Start executing it... |
| 117 | |
| 118 | // Reset the current frame location to the top of stack |
| 119 | CurFrame = ECStack.size()-1; |
| 120 | |
| 121 | return false; |
| 122 | } |