blob: bcaa8569dc47b5367e8d6466e95b9ec6a458f85d [file] [log] [blame]
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001//===- Interpreter.cpp - Top-Level LLVM Interpreter Implementation --------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnerbd199fb2002-12-24 00:01:05 +00009//
10// This file implements the top-level functionality for the LLVM interpreter.
11// This interpreter is designed to be a very simple, portable, inefficient
12// interpreter.
13//
14//===----------------------------------------------------------------------===//
15
16#include "Interpreter.h"
Chris Lattner39c07262003-08-24 19:50:53 +000017#include "llvm/Module.h"
Brian Gaeke413ab662003-09-05 04:46:26 +000018#include "llvm/DerivedTypes.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000019
Brian Gaeke82d82772003-09-03 20:34:19 +000020/// create - Create a new interpreter object. This can never fail.
Chris Lattnerbd199fb2002-12-24 00:01:05 +000021///
Brian Gaekea824f422003-10-24 19:59:28 +000022ExecutionEngine *Interpreter::create(Module *M){
Chris Lattner021c1902003-09-22 20:33:34 +000023 bool isLittleEndian = false;
Chris Lattner39c07262003-08-24 19:50:53 +000024 switch (M->getEndianness()) {
25 case Module::LittleEndian: isLittleEndian = true; break;
26 case Module::BigEndian: isLittleEndian = false; break;
27 case Module::AnyPointerSize:
28 int Test = 0;
29 *(char*)&Test = 1; // Return true if the host is little endian
30 isLittleEndian = (Test == 1);
31 break;
32 }
33
Chris Lattner021c1902003-09-22 20:33:34 +000034 bool isLongPointer = false;
Chris Lattner39c07262003-08-24 19:50:53 +000035 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
40 break;
41 }
42
Brian Gaekea824f422003-10-24 19:59:28 +000043 return new Interpreter(M, isLittleEndian, isLongPointer);
Chris Lattnerbd199fb2002-12-24 00:01:05 +000044}
45
46//===----------------------------------------------------------------------===//
47// Interpreter ctor - Initialize stuff
48//
Brian Gaekea824f422003-10-24 19:59:28 +000049Interpreter::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) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +000053
54 setTargetData(TD);
55 // Initialize the "backend"
56 initializeExecutionEngine();
Chris Lattnerda82ed52003-05-08 16:18:31 +000057 initializeExternalFunctions();
Chris Lattner56adf152003-05-12 02:14:34 +000058 emitGlobals();
Chris Lattnerbd199fb2002-12-24 00:01:05 +000059}
60
Brian Gaeke70975ee2003-09-05 18:42:01 +000061void Interpreter::runAtExitHandlers () {
62 while (!AtExitHandlers.empty()) {
63 callFunction(AtExitHandlers.back(), std::vector<GenericValue>());
64 AtExitHandlers.pop_back();
Chris Lattnerbd199fb2002-12-24 00:01:05 +000065 run();
66 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +000067}
68
Brian Gaeke70975ee2003-09-05 18:42:01 +000069/// run - Start execution with the specified function and arguments.
70///
71GenericValue Interpreter::run(Function *F,
72 const std::vector<GenericValue> &ArgValues) {
73 assert (F && "Function *F was null at entry to run()");
Brian Gaeke413ab662003-09-05 04:46:26 +000074
Brian Gaeke70975ee2003-09-05 18:42:01 +000075 // 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,
81 // though.
82 std::vector<GenericValue> ActualArgs;
83 const unsigned ArgCount = F->getFunctionType()->getParamTypes().size();
Brian Gaekea824f422003-10-24 19:59:28 +000084 for (unsigned i = 0; i < ArgCount; ++i)
Brian Gaeke70975ee2003-09-05 18:42:01 +000085 ActualArgs.push_back (ArgValues[i]);
Brian Gaeke70975ee2003-09-05 18:42:01 +000086
87 // Set up the function call.
88 callFunction(F, ActualArgs);
Brian Gaeke413ab662003-09-05 04:46:26 +000089
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}