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"
11 #include "llvm/DerivedTypes.h"
12 #include "llvm/Function.h"
14 /// create - Create a new interpreter object. This can never fail.
16 ExecutionEngine *Interpreter::create(Module *M, bool TraceMode){
18 switch (M->getEndianness()) {
19 case Module::LittleEndian: isLittleEndian = true; break;
20 case Module::BigEndian: isLittleEndian = false; break;
21 case Module::AnyPointerSize:
23 *(char*)&Test = 1; // Return true if the host is little endian
24 isLittleEndian = (Test == 1);
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
37 return new Interpreter(M, isLittleEndian, isLongPointer, TraceMode);
40 //===----------------------------------------------------------------------===//
41 // Interpreter ctor - Initialize stuff
43 Interpreter::Interpreter(Module *M, bool isLittleEndian, bool isLongPointer,
45 : ExecutionEngine(M), ExitCode(0), Trace(TraceMode),
46 CurFrame(-1), TD("lli", isLittleEndian, isLongPointer ? 8 : 4,
47 isLongPointer ? 8 : 4, isLongPointer ? 8 : 4) {
50 // Initialize the "backend"
51 initializeExecutionEngine();
52 initializeExternalFunctions();
53 CW.setModule(M); // Update Writer
57 void Interpreter::runAtExitHandlers () {
58 while (!AtExitHandlers.empty()) {
59 callFunction(AtExitHandlers.back(), std::vector<GenericValue>());
60 AtExitHandlers.pop_back();
65 /// run - Start execution with the specified function and arguments.
67 GenericValue Interpreter::run(Function *F,
68 const std::vector<GenericValue> &ArgValues) {
69 assert (F && "Function *F was null at entry to run()");
71 // Try extra hard not to pass extra args to a function that isn't
72 // expecting them. C programmers frequently bend the rules and
73 // declare main() with fewer parameters than it actually gets
74 // passed, and the interpreter barfs if you pass a function more
75 // parameters than it is declared to take. This does not attempt to
76 // take into account gratuitous differences in declared types,
78 std::vector<GenericValue> ActualArgs;
79 const unsigned ArgCount = F->getFunctionType()->getParamTypes().size();
80 for (unsigned i = 0; i < ArgCount; ++i) {
81 ActualArgs.push_back (ArgValues[i]);
84 // Set up the function call.
85 callFunction(F, ActualArgs);
87 // Reset the current frame location to the top of stack
88 CurFrame = ECStack.size()-1;
90 // Start executing the function.
93 // Run any atexit handlers now!