blob: 518290a9f79b4ef1ba61ee32af8bd0b7ace27eac [file] [log] [blame]
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001//===- 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"
Chris Lattner39c07262003-08-24 19:50:53 +000010#include "llvm/Module.h"
Brian Gaeke413ab662003-09-05 04:46:26 +000011#include "llvm/DerivedTypes.h"
12#include "llvm/Function.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000013
Brian Gaeke82d82772003-09-03 20:34:19 +000014/// create - Create a new interpreter object. This can never fail.
Chris Lattnerbd199fb2002-12-24 00:01:05 +000015///
Brian Gaekef58815e2003-09-04 22:21:24 +000016ExecutionEngine *Interpreter::create(Module *M, bool TraceMode){
Chris Lattner39c07262003-08-24 19:50:53 +000017 bool isLittleEndian;
18 switch (M->getEndianness()) {
19 case Module::LittleEndian: isLittleEndian = true; break;
20 case Module::BigEndian: isLittleEndian = false; break;
21 case Module::AnyPointerSize:
22 int Test = 0;
23 *(char*)&Test = 1; // Return true if the host is little endian
24 isLittleEndian = (Test == 1);
25 break;
26 }
27
28 bool isLongPointer;
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
34 break;
35 }
36
Brian Gaekef58815e2003-09-04 22:21:24 +000037 return new Interpreter(M, isLittleEndian, isLongPointer, TraceMode);
Chris Lattnerbd199fb2002-12-24 00:01:05 +000038}
39
40//===----------------------------------------------------------------------===//
41// Interpreter ctor - Initialize stuff
42//
Chris Lattner39c07262003-08-24 19:50:53 +000043Interpreter::Interpreter(Module *M, bool isLittleEndian, bool isLongPointer,
Brian Gaekef58815e2003-09-04 22:21:24 +000044 bool TraceMode)
45 : ExecutionEngine(M), ExitCode(0), Trace(TraceMode),
Chris Lattner39c07262003-08-24 19:50:53 +000046 CurFrame(-1), TD("lli", isLittleEndian, isLongPointer ? 8 : 4,
47 isLongPointer ? 8 : 4, isLongPointer ? 8 : 4) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +000048
49 setTargetData(TD);
50 // Initialize the "backend"
51 initializeExecutionEngine();
Chris Lattnerda82ed52003-05-08 16:18:31 +000052 initializeExternalFunctions();
Chris Lattnerbd199fb2002-12-24 00:01:05 +000053 CW.setModule(M); // Update Writer
Chris Lattner56adf152003-05-12 02:14:34 +000054 emitGlobals();
Chris Lattnerbd199fb2002-12-24 00:01:05 +000055}
56
Brian Gaeke70975ee2003-09-05 18:42:01 +000057void Interpreter::runAtExitHandlers () {
58 while (!AtExitHandlers.empty()) {
59 callFunction(AtExitHandlers.back(), std::vector<GenericValue>());
60 AtExitHandlers.pop_back();
Chris Lattnerbd199fb2002-12-24 00:01:05 +000061 run();
62 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +000063}
64
Brian Gaeke70975ee2003-09-05 18:42:01 +000065/// run - Start execution with the specified function and arguments.
66///
67GenericValue Interpreter::run(Function *F,
68 const std::vector<GenericValue> &ArgValues) {
69 assert (F && "Function *F was null at entry to run()");
Brian Gaeke413ab662003-09-05 04:46:26 +000070
Brian Gaeke70975ee2003-09-05 18:42:01 +000071 // 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,
77 // though.
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]);
Brian Gaeke413ab662003-09-05 04:46:26 +000082 }
Brian Gaeke70975ee2003-09-05 18:42:01 +000083
84 // Set up the function call.
85 callFunction(F, ActualArgs);
Brian Gaeke413ab662003-09-05 04:46:26 +000086
87 // Reset the current frame location to the top of stack
88 CurFrame = ECStack.size()-1;
89
Brian Gaeke70975ee2003-09-05 18:42:01 +000090 // Start executing the function.
91 run();
92
93 // Run any atexit handlers now!
94 runAtExitHandlers();
95
96 GenericValue rv;
97 rv.IntVal = ExitCode;
98 return rv;
Brian Gaeke413ab662003-09-05 04:46:26 +000099}