blob: d3b6fba3e60646d420549c15b9ed4297f81d3732 [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"
19#include "llvm/Function.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000020
Brian Gaeke82d82772003-09-03 20:34:19 +000021/// create - Create a new interpreter object. This can never fail.
Chris Lattnerbd199fb2002-12-24 00:01:05 +000022///
Brian Gaekef58815e2003-09-04 22:21:24 +000023ExecutionEngine *Interpreter::create(Module *M, bool TraceMode){
Chris Lattner021c1902003-09-22 20:33:34 +000024 bool isLittleEndian = false;
Chris Lattner39c07262003-08-24 19:50:53 +000025 switch (M->getEndianness()) {
26 case Module::LittleEndian: isLittleEndian = true; break;
27 case Module::BigEndian: isLittleEndian = false; break;
28 case Module::AnyPointerSize:
29 int Test = 0;
30 *(char*)&Test = 1; // Return true if the host is little endian
31 isLittleEndian = (Test == 1);
32 break;
33 }
34
Chris Lattner021c1902003-09-22 20:33:34 +000035 bool isLongPointer = false;
Chris Lattner39c07262003-08-24 19:50:53 +000036 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
41 break;
42 }
43
Brian Gaekef58815e2003-09-04 22:21:24 +000044 return new Interpreter(M, isLittleEndian, isLongPointer, TraceMode);
Chris Lattnerbd199fb2002-12-24 00:01:05 +000045}
46
47//===----------------------------------------------------------------------===//
48// Interpreter ctor - Initialize stuff
49//
Chris Lattner39c07262003-08-24 19:50:53 +000050Interpreter::Interpreter(Module *M, bool isLittleEndian, bool isLongPointer,
Brian Gaekef58815e2003-09-04 22:21:24 +000051 bool TraceMode)
52 : ExecutionEngine(M), ExitCode(0), Trace(TraceMode),
Chris Lattner39c07262003-08-24 19:50:53 +000053 CurFrame(-1), TD("lli", isLittleEndian, isLongPointer ? 8 : 4,
54 isLongPointer ? 8 : 4, 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 Lattnerbd199fb2002-12-24 00:01:05 +000060 CW.setModule(M); // Update Writer
Chris Lattner56adf152003-05-12 02:14:34 +000061 emitGlobals();
Chris Lattnerbd199fb2002-12-24 00:01:05 +000062}
63
Brian Gaeke70975ee2003-09-05 18:42:01 +000064void Interpreter::runAtExitHandlers () {
65 while (!AtExitHandlers.empty()) {
66 callFunction(AtExitHandlers.back(), std::vector<GenericValue>());
67 AtExitHandlers.pop_back();
Chris Lattnerbd199fb2002-12-24 00:01:05 +000068 run();
69 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +000070}
71
Brian Gaeke70975ee2003-09-05 18:42:01 +000072/// run - Start execution with the specified function and arguments.
73///
74GenericValue Interpreter::run(Function *F,
75 const std::vector<GenericValue> &ArgValues) {
76 assert (F && "Function *F was null at entry to run()");
Brian Gaeke413ab662003-09-05 04:46:26 +000077
Brian Gaeke70975ee2003-09-05 18:42:01 +000078 // 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,
84 // though.
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]);
Brian Gaeke413ab662003-09-05 04:46:26 +000089 }
Brian Gaeke70975ee2003-09-05 18:42:01 +000090
91 // Set up the function call.
92 callFunction(F, ActualArgs);
Brian Gaeke413ab662003-09-05 04:46:26 +000093
94 // Reset the current frame location to the top of stack
95 CurFrame = ECStack.size()-1;
96
Brian Gaeke70975ee2003-09-05 18:42:01 +000097 // Start executing the function.
98 run();
99
100 // Run any atexit handlers now!
101 runAtExitHandlers();
102
103 GenericValue rv;
104 rv.IntVal = ExitCode;
105 return rv;
Brian Gaeke413ab662003-09-05 04:46:26 +0000106}