1 //===- Interpreter.cpp - Top-Level LLVM Interpreter Implementation --------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the top-level functionality for the LLVM interpreter.
11 // This interpreter is designed to be a very simple, portable, inefficient
14 //===----------------------------------------------------------------------===//
16 #include "Interpreter.h"
17 #include "llvm/Module.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/Function.h"
21 /// create - Create a new interpreter object. This can never fail.
23 ExecutionEngine *Interpreter::create(Module *M, bool TraceMode){
24 bool isLittleEndian = false;
25 switch (M->getEndianness()) {
26 case Module::LittleEndian: isLittleEndian = true; break;
27 case Module::BigEndian: isLittleEndian = false; break;
28 case Module::AnyPointerSize:
30 *(char*)&Test = 1; // Return true if the host is little endian
31 isLittleEndian = (Test == 1);
35 bool isLongPointer = false;
36 switch (M->getPointerSize()) {
37 case Module::Pointer32: isLongPointer = false; break;
38 case Module::Pointer64: isLongPointer = true; break;
39 case Module::AnyPointerSize:
40 isLongPointer = (sizeof(void*) == 8); // Follow host
44 return new Interpreter(M, isLittleEndian, isLongPointer, TraceMode);
47 //===----------------------------------------------------------------------===//
48 // Interpreter ctor - Initialize stuff
50 Interpreter::Interpreter(Module *M, bool isLittleEndian, bool isLongPointer,
52 : ExecutionEngine(M), ExitCode(0), Trace(TraceMode),
53 CurFrame(-1), TD("lli", isLittleEndian, isLongPointer ? 8 : 4,
54 isLongPointer ? 8 : 4, isLongPointer ? 8 : 4) {
57 // Initialize the "backend"
58 initializeExecutionEngine();
59 initializeExternalFunctions();
60 CW.setModule(M); // Update Writer
64 void Interpreter::runAtExitHandlers () {
65 while (!AtExitHandlers.empty()) {
66 callFunction(AtExitHandlers.back(), std::vector<GenericValue>());
67 AtExitHandlers.pop_back();
72 /// run - Start execution with the specified function and arguments.
74 GenericValue Interpreter::run(Function *F,
75 const std::vector<GenericValue> &ArgValues) {
76 assert (F && "Function *F was null at entry to run()");
78 // Try extra hard not to pass extra args to a function that isn't
79 // expecting them. C programmers frequently bend the rules and
80 // declare main() with fewer parameters than it actually gets
81 // passed, and the interpreter barfs if you pass a function more
82 // parameters than it is declared to take. This does not attempt to
83 // take into account gratuitous differences in declared types,
85 std::vector<GenericValue> ActualArgs;
86 const unsigned ArgCount = F->getFunctionType()->getParamTypes().size();
87 for (unsigned i = 0; i < ArgCount; ++i) {
88 ActualArgs.push_back (ArgValues[i]);
91 // Set up the function call.
92 callFunction(F, ActualArgs);
94 // Reset the current frame location to the top of stack
95 CurFrame = ECStack.size()-1;
97 // Start executing the function.
100 // Run any atexit handlers now!
104 rv.IntVal = ExitCode;