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" |
| 10 | #include "llvm/Target/TargetMachineImpls.h" |
| 11 | |
| 12 | /// createInterpreter - Create a new interpreter object. This can never fail. |
| 13 | /// |
| 14 | ExecutionEngine *ExecutionEngine::createInterpreter(Module *M, |
| 15 | unsigned Config, |
| 16 | bool DebugMode, |
| 17 | bool TraceMode) { |
| 18 | return new Interpreter(M, Config, DebugMode, TraceMode); |
| 19 | } |
| 20 | |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | // Interpreter ctor - Initialize stuff |
| 23 | // |
| 24 | Interpreter::Interpreter(Module *M, unsigned Config, |
| 25 | bool DebugMode, bool TraceMode) |
| 26 | : ExecutionEngine(M), ExitCode(0), Debug(DebugMode), Trace(TraceMode), |
| 27 | CurFrame(-1), TD("lli", (Config & TM::EndianMask) == TM::LittleEndian, |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 28 | (Config & TM::PtrSizeMask) == TM::PtrSize64 ? 8 : 4, |
Chris Lattner | 6b73fec | 2003-04-25 06:15:05 +0000 | [diff] [blame] | 29 | (Config & TM::PtrSizeMask) == TM::PtrSize64 ? 8 : 4, |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 30 | (Config & TM::PtrSizeMask) == TM::PtrSize64 ? 8 : 4) { |
| 31 | |
| 32 | setTargetData(TD); |
| 33 | // Initialize the "backend" |
| 34 | initializeExecutionEngine(); |
Chris Lattner | da82ed5 | 2003-05-08 16:18:31 +0000 | [diff] [blame] | 35 | initializeExternalFunctions(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 36 | CW.setModule(M); // Update Writer |
Chris Lattner | 56adf15 | 2003-05-12 02:14:34 +0000 | [diff] [blame] | 37 | emitGlobals(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | /// run - Start execution with the specified function and arguments. |
| 41 | /// |
| 42 | int Interpreter::run(const std::string &MainFunction, |
John Criswell | 69582b3 | 2003-08-21 21:12:30 +0000 | [diff] [blame^] | 43 | const std::vector<std::string> &Args, |
| 44 | const char ** envp) { |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 45 | // Start interpreter into the main function... |
| 46 | // |
Chris Lattner | da82ed5 | 2003-05-08 16:18:31 +0000 | [diff] [blame] | 47 | if (!callMainFunction(MainFunction, Args) && !Debug) { |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 48 | // If not in debug mode and if the call succeeded, run the code now... |
| 49 | run(); |
| 50 | } |
| 51 | |
Chris Lattner | 44edb6b | 2003-05-14 14:21:30 +0000 | [diff] [blame] | 52 | do { |
| 53 | // If debug mode, allow the user to interact... also, if the user pressed |
| 54 | // ctrl-c or execution hit an error, enter the event loop... |
| 55 | if (Debug || isStopped()) |
| 56 | handleUserInput(); |
| 57 | |
| 58 | // If the program has exited, run atexit handlers... |
| 59 | if (ECStack.empty() && !AtExitHandlers.empty()) { |
| 60 | callFunction(AtExitHandlers.back(), std::vector<GenericValue>()); |
| 61 | AtExitHandlers.pop_back(); |
| 62 | run(); |
| 63 | } |
| 64 | } while (!ECStack.empty()); |
| 65 | |
| 66 | PerformExitStuff(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 67 | return ExitCode; |
| 68 | } |
| 69 | |