1 //===- Interpreter.cpp - Top-Level LLVM Interpreter Implementation --------===//
3 // This file implements the top-level functionality for the LLVM interpreter.
4 // This interpreter is designed to be a very simple, portable, inefficient
7 //===----------------------------------------------------------------------===//
9 #include "Interpreter.h"
10 #include "llvm/Module.h"
12 /// create - Create a new interpreter object. This can never fail.
14 ExecutionEngine *Interpreter::create(Module *M, bool DebugMode, bool TraceMode){
16 switch (M->getEndianness()) {
17 case Module::LittleEndian: isLittleEndian = true; break;
18 case Module::BigEndian: isLittleEndian = false; break;
19 case Module::AnyPointerSize:
21 *(char*)&Test = 1; // Return true if the host is little endian
22 isLittleEndian = (Test == 1);
27 switch (M->getPointerSize()) {
28 case Module::Pointer32: isLongPointer = false; break;
29 case Module::Pointer64: isLongPointer = true; break;
30 case Module::AnyPointerSize:
31 isLongPointer = (sizeof(void*) == 8); // Follow host
35 return new Interpreter(M, isLittleEndian, isLongPointer, DebugMode,TraceMode);
38 //===----------------------------------------------------------------------===//
39 // Interpreter ctor - Initialize stuff
41 Interpreter::Interpreter(Module *M, bool isLittleEndian, bool isLongPointer,
42 bool DebugMode, bool TraceMode)
43 : ExecutionEngine(M), ExitCode(0), Debug(DebugMode), Trace(TraceMode),
44 CurFrame(-1), TD("lli", isLittleEndian, isLongPointer ? 8 : 4,
45 isLongPointer ? 8 : 4, isLongPointer ? 8 : 4) {
48 // Initialize the "backend"
49 initializeExecutionEngine();
50 initializeExternalFunctions();
51 CW.setModule(M); // Update Writer
55 /// run - Start execution with the specified function and arguments.
57 int Interpreter::run(const std::string &MainFunction,
58 const std::vector<std::string> &Args,
60 // Start interpreter into the main function...
62 if (!callMainFunction(MainFunction, Args) && !Debug) {
63 // If not in debug mode and if the call succeeded, run the code now...
68 // If debug mode, allow the user to interact... also, if the user pressed
69 // ctrl-c or execution hit an error, enter the event loop...
70 if (Debug || isStopped())
73 // If the program has exited, run atexit handlers...
74 if (ECStack.empty() && !AtExitHandlers.empty()) {
75 callFunction(AtExitHandlers.back(), std::vector<GenericValue>());
76 AtExitHandlers.pop_back();
79 } while (!ECStack.empty());