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"
20 /// create - Create a new interpreter object. This can never fail.
22 ExecutionEngine *Interpreter::create(Module *M){
23 bool isLittleEndian = false;
24 switch (M->getEndianness()) {
25 case Module::LittleEndian: isLittleEndian = true; break;
26 case Module::BigEndian: isLittleEndian = false; break;
27 case Module::AnyPointerSize:
29 *(char*)&Test = 1; // Return true if the host is little endian
30 isLittleEndian = (Test == 1);
34 bool isLongPointer = false;
35 switch (M->getPointerSize()) {
36 case Module::Pointer32: isLongPointer = false; break;
37 case Module::Pointer64: isLongPointer = true; break;
38 case Module::AnyPointerSize:
39 isLongPointer = (sizeof(void*) == 8); // Follow host
43 return new Interpreter(M, isLittleEndian, isLongPointer);
46 //===----------------------------------------------------------------------===//
47 // Interpreter ctor - Initialize stuff
49 Interpreter::Interpreter(Module *M, bool isLittleEndian, bool isLongPointer)
50 : ExecutionEngine(M), ExitCode(0),
51 TD("lli", isLittleEndian, isLongPointer ? 8 : 4, isLongPointer ? 8 : 4,
52 isLongPointer ? 8 : 4) {
55 // Initialize the "backend"
56 initializeExecutionEngine();
57 initializeExternalFunctions();
61 void Interpreter::runAtExitHandlers () {
62 while (!AtExitHandlers.empty()) {
63 callFunction(AtExitHandlers.back(), std::vector<GenericValue>());
64 AtExitHandlers.pop_back();
69 /// run - Start execution with the specified function and arguments.
71 GenericValue Interpreter::run(Function *F,
72 const std::vector<GenericValue> &ArgValues) {
73 assert (F && "Function *F was null at entry to run()");
75 // Try extra hard not to pass extra args to a function that isn't
76 // expecting them. C programmers frequently bend the rules and
77 // declare main() with fewer parameters than it actually gets
78 // passed, and the interpreter barfs if you pass a function more
79 // parameters than it is declared to take. This does not attempt to
80 // take into account gratuitous differences in declared types,
82 std::vector<GenericValue> ActualArgs;
83 const unsigned ArgCount = F->getFunctionType()->getParamTypes().size();
84 for (unsigned i = 0; i < ArgCount; ++i)
85 ActualArgs.push_back (ArgValues[i]);
87 // Set up the function call.
88 callFunction(F, ActualArgs);
90 // Start executing the function.
93 // Run any atexit handlers now!