blob: af23bf11beb36621f672510ccfb4f62b8c174277 [file] [log] [blame]
Chris Lattner996fe012002-12-24 00:01:05 +00001//===- Interpreter.cpp - Top-Level LLVM Interpreter Implementation --------===//
Misha Brukman91fb9ab2005-04-21 22:43:08 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// 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.
Misha Brukman91fb9ab2005-04-21 22:43:08 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner996fe012002-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 Lattnerbcdadf32004-06-20 07:49:54 +000017#include "llvm/CodeGen/IntrinsicLowering.h"
Brian Gaeke84217652003-09-05 04:46:26 +000018#include "llvm/DerivedTypes.h"
Chris Lattnerc8c6c032003-12-28 09:44:37 +000019#include "llvm/Module.h"
Chris Lattner18099662003-12-14 23:25:48 +000020using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000021
Brian Gaeke4bd3bd52003-09-03 20:34:19 +000022/// create - Create a new interpreter object. This can never fail.
Chris Lattner996fe012002-12-24 00:01:05 +000023///
Chris Lattnerc8c6c032003-12-28 09:44:37 +000024ExecutionEngine *Interpreter::create(Module *M, IntrinsicLowering *IL) {
Chris Lattner2caaaba2003-09-22 20:33:34 +000025 bool isLittleEndian = false;
Chris Lattner4d4f4242003-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 Lattner2caaaba2003-09-22 20:33:34 +000036 bool isLongPointer = false;
Chris Lattner4d4f4242003-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
Chris Lattnerc8c6c032003-12-28 09:44:37 +000045 return new Interpreter(M, isLittleEndian, isLongPointer, IL);
Chris Lattner996fe012002-12-24 00:01:05 +000046}
47
48//===----------------------------------------------------------------------===//
49// Interpreter ctor - Initialize stuff
50//
Chris Lattnerc8c6c032003-12-28 09:44:37 +000051Interpreter::Interpreter(Module *M, bool isLittleEndian, bool isLongPointer,
52 IntrinsicLowering *il)
Jeff Cohen24396692006-02-07 05:29:44 +000053 : ExecutionEngine(M),
Brian Gaeke65c60e22003-10-24 19:59:28 +000054 TD("lli", isLittleEndian, isLongPointer ? 8 : 4, isLongPointer ? 8 : 4,
Chris Lattnerc8c6c032003-12-28 09:44:37 +000055 isLongPointer ? 8 : 4), IL(il) {
Chris Lattner996fe012002-12-24 00:01:05 +000056
Jeff Cohen24396692006-02-07 05:29:44 +000057 memset(&ExitValue, 0, sizeof(ExitValue));
Chris Lattner996fe012002-12-24 00:01:05 +000058 setTargetData(TD);
59 // Initialize the "backend"
60 initializeExecutionEngine();
Chris Lattner470754e2003-05-08 16:18:31 +000061 initializeExternalFunctions();
Chris Lattnerb78244f2003-05-12 02:14:34 +000062 emitGlobals();
Chris Lattnerc8c6c032003-12-28 09:44:37 +000063
64 if (IL == 0) IL = new DefaultIntrinsicLowering();
65}
66
67Interpreter::~Interpreter() {
68 delete IL;
Chris Lattner996fe012002-12-24 00:01:05 +000069}
70
Brian Gaekea7669032003-09-05 18:42:01 +000071void Interpreter::runAtExitHandlers () {
72 while (!AtExitHandlers.empty()) {
73 callFunction(AtExitHandlers.back(), std::vector<GenericValue>());
74 AtExitHandlers.pop_back();
Chris Lattner996fe012002-12-24 00:01:05 +000075 run();
76 }
Chris Lattner996fe012002-12-24 00:01:05 +000077}
78
Brian Gaekea7669032003-09-05 18:42:01 +000079/// run - Start execution with the specified function and arguments.
80///
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000081GenericValue
Misha Brukman5191b4b2005-04-22 04:08:30 +000082Interpreter::runFunction(Function *F,
83 const std::vector<GenericValue> &ArgValues) {
Brian Gaekea7669032003-09-05 18:42:01 +000084 assert (F && "Function *F was null at entry to run()");
Brian Gaeke84217652003-09-05 04:46:26 +000085
Brian Gaekea7669032003-09-05 18:42:01 +000086 // Try extra hard not to pass extra args to a function that isn't
87 // expecting them. C programmers frequently bend the rules and
88 // declare main() with fewer parameters than it actually gets
89 // passed, and the interpreter barfs if you pass a function more
90 // parameters than it is declared to take. This does not attempt to
91 // take into account gratuitous differences in declared types,
92 // though.
93 std::vector<GenericValue> ActualArgs;
Chris Lattnerfa829be2004-02-09 04:14:01 +000094 const unsigned ArgCount = F->getFunctionType()->getNumParams();
Brian Gaeke65c60e22003-10-24 19:59:28 +000095 for (unsigned i = 0; i < ArgCount; ++i)
Chris Lattnerfa829be2004-02-09 04:14:01 +000096 ActualArgs.push_back(ArgValues[i]);
Misha Brukman91fb9ab2005-04-21 22:43:08 +000097
Brian Gaekea7669032003-09-05 18:42:01 +000098 // Set up the function call.
99 callFunction(F, ActualArgs);
Brian Gaeke84217652003-09-05 04:46:26 +0000100
Brian Gaekea7669032003-09-05 18:42:01 +0000101 // Start executing the function.
102 run();
Misha Brukman91fb9ab2005-04-21 22:43:08 +0000103
Jeff Cohen24396692006-02-07 05:29:44 +0000104 return ExitValue;
Brian Gaeke84217652003-09-05 04:46:26 +0000105}
Brian Gaeke960707c2003-11-11 22:41:34 +0000106