blob: bb14cd2ee89abbb3b8b0d797bdc0813ea9e8dbef [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 Gaeked0fde302003-11-11 22:41:34 +000020namespace llvm {
21
Brian Gaeke82d82772003-09-03 20:34:19 +000022/// create - Create a new interpreter object. This can never fail.
Chris Lattnerbd199fb2002-12-24 00:01:05 +000023///
Brian Gaekea824f422003-10-24 19:59:28 +000024ExecutionEngine *Interpreter::create(Module *M){
Chris Lattner021c1902003-09-22 20:33:34 +000025 bool isLittleEndian = false;
Chris Lattner39c07262003-08-24 19:50:53 +000026 switch (M->getEndianness()) {
27 case Module::LittleEndian: isLittleEndian = true; break;
28 case Module::BigEndian: isLittleEndian = false; break;
29 case Module::AnyPointerSize:
30 int Test = 0;
31 *(char*)&Test = 1; // Return true if the host is little endian
32 isLittleEndian = (Test == 1);
33 break;
34 }
35
Chris Lattner021c1902003-09-22 20:33:34 +000036 bool isLongPointer = false;
Chris Lattner39c07262003-08-24 19:50:53 +000037 switch (M->getPointerSize()) {
38 case Module::Pointer32: isLongPointer = false; break;
39 case Module::Pointer64: isLongPointer = true; break;
40 case Module::AnyPointerSize:
41 isLongPointer = (sizeof(void*) == 8); // Follow host
42 break;
43 }
44
Brian Gaekea824f422003-10-24 19:59:28 +000045 return new Interpreter(M, isLittleEndian, isLongPointer);
Chris Lattnerbd199fb2002-12-24 00:01:05 +000046}
47
48//===----------------------------------------------------------------------===//
49// Interpreter ctor - Initialize stuff
50//
Brian Gaekea824f422003-10-24 19:59:28 +000051Interpreter::Interpreter(Module *M, bool isLittleEndian, bool isLongPointer)
52 : ExecutionEngine(M), ExitCode(0),
53 TD("lli", isLittleEndian, isLongPointer ? 8 : 4, isLongPointer ? 8 : 4,
54 isLongPointer ? 8 : 4) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +000055
56 setTargetData(TD);
57 // Initialize the "backend"
58 initializeExecutionEngine();
Chris Lattnerda82ed52003-05-08 16:18:31 +000059 initializeExternalFunctions();
Chris Lattner56adf152003-05-12 02:14:34 +000060 emitGlobals();
Chris Lattnerbd199fb2002-12-24 00:01:05 +000061}
62
Brian Gaeke70975ee2003-09-05 18:42:01 +000063void Interpreter::runAtExitHandlers () {
64 while (!AtExitHandlers.empty()) {
65 callFunction(AtExitHandlers.back(), std::vector<GenericValue>());
66 AtExitHandlers.pop_back();
Chris Lattnerbd199fb2002-12-24 00:01:05 +000067 run();
68 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +000069}
70
Brian Gaeke70975ee2003-09-05 18:42:01 +000071/// run - Start execution with the specified function and arguments.
72///
73GenericValue Interpreter::run(Function *F,
74 const std::vector<GenericValue> &ArgValues) {
75 assert (F && "Function *F was null at entry to run()");
Brian Gaeke413ab662003-09-05 04:46:26 +000076
Brian Gaeke70975ee2003-09-05 18:42:01 +000077 // Try extra hard not to pass extra args to a function that isn't
78 // expecting them. C programmers frequently bend the rules and
79 // declare main() with fewer parameters than it actually gets
80 // passed, and the interpreter barfs if you pass a function more
81 // parameters than it is declared to take. This does not attempt to
82 // take into account gratuitous differences in declared types,
83 // though.
84 std::vector<GenericValue> ActualArgs;
85 const unsigned ArgCount = F->getFunctionType()->getParamTypes().size();
Brian Gaekea824f422003-10-24 19:59:28 +000086 for (unsigned i = 0; i < ArgCount; ++i)
Brian Gaeke70975ee2003-09-05 18:42:01 +000087 ActualArgs.push_back (ArgValues[i]);
Brian Gaeke70975ee2003-09-05 18:42:01 +000088
89 // Set up the function call.
90 callFunction(F, ActualArgs);
Brian Gaeke413ab662003-09-05 04:46:26 +000091
Brian Gaeke70975ee2003-09-05 18:42:01 +000092 // Start executing the function.
93 run();
94
95 // Run any atexit handlers now!
96 runAtExitHandlers();
97
98 GenericValue rv;
99 rv.IntVal = ExitCode;
100 return rv;
Brian Gaeke413ab662003-09-05 04:46:26 +0000101}
Brian Gaeked0fde302003-11-11 22:41:34 +0000102
103} // End llvm namespace