blob: 9be6a9265d613a1954af2ea2d7ba0fb4ff527d99 [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//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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 Lattner2d52c1b2006-03-22 06:07:50 +000020#include "llvm/ModuleProvider.h"
Anton Korobeynikov579f0712008-02-20 11:08:44 +000021#include <cstring>
Chris Lattner18099662003-12-14 23:25:48 +000022using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000023
Dan Gohmand78c4002008-05-13 00:00:25 +000024namespace {
25
Chris Lattner2d52c1b2006-03-22 06:07:50 +000026static struct RegisterInterp {
27 RegisterInterp() { Interpreter::Register(); }
28} InterpRegistrator;
29
Dan Gohmand78c4002008-05-13 00:00:25 +000030}
31
Bob Wilsona1d3e662009-06-24 21:09:18 +000032extern "C" void LLVMLinkInInterpreter() { }
Jeff Cohen0eafbc32006-03-24 02:53:49 +000033
Brian Gaeke4bd3bd52003-09-03 20:34:19 +000034/// create - Create a new interpreter object. This can never fail.
Chris Lattner996fe012002-12-24 00:01:05 +000035///
Reid Klecknerfc8a2d52009-07-18 00:42:18 +000036ExecutionEngine *Interpreter::create(ModuleProvider *MP, std::string* ErrStr) {
Reid Spencer603682a2007-03-03 18:19:18 +000037 // Tell this ModuleProvide to materialize and release the module
Chris Lattnerdc351b92007-12-06 01:08:09 +000038 if (!MP->materializeModule(ErrStr))
Reid Spencer603682a2007-03-03 18:19:18 +000039 // We got an error, just return 0
40 return 0;
41
Chris Lattnerdc351b92007-12-06 01:08:09 +000042 return new Interpreter(MP);
Chris Lattner996fe012002-12-24 00:01:05 +000043}
44
45//===----------------------------------------------------------------------===//
46// Interpreter ctor - Initialize stuff
47//
Chris Lattnerdc351b92007-12-06 01:08:09 +000048Interpreter::Interpreter(ModuleProvider *M)
49 : ExecutionEngine(M), TD(M->getModule()) {
Chris Lattner91f228b2006-06-16 18:08:38 +000050
Reid Spencerff38cf82007-06-01 22:23:29 +000051 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
Owen Anderson20a631f2006-05-03 01:29:57 +000052 setTargetData(&TD);
Chris Lattner996fe012002-12-24 00:01:05 +000053 // Initialize the "backend"
54 initializeExecutionEngine();
Chris Lattner470754e2003-05-08 16:18:31 +000055 initializeExternalFunctions();
Chris Lattnerb78244f2003-05-12 02:14:34 +000056 emitGlobals();
Chris Lattnerc8c6c032003-12-28 09:44:37 +000057
Reid Spencer1e960cd2007-01-29 17:51:02 +000058 IL = new IntrinsicLowering(TD);
Chris Lattnerc8c6c032003-12-28 09:44:37 +000059}
60
61Interpreter::~Interpreter() {
62 delete IL;
Chris Lattner996fe012002-12-24 00:01:05 +000063}
64
Brian Gaekea7669032003-09-05 18:42:01 +000065void Interpreter::runAtExitHandlers () {
66 while (!AtExitHandlers.empty()) {
67 callFunction(AtExitHandlers.back(), std::vector<GenericValue>());
68 AtExitHandlers.pop_back();
Chris Lattner996fe012002-12-24 00:01:05 +000069 run();
70 }
Chris Lattner996fe012002-12-24 00:01:05 +000071}
72
Brian Gaekea7669032003-09-05 18:42:01 +000073/// run - Start execution with the specified function and arguments.
74///
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000075GenericValue
Misha Brukman5191b4b2005-04-22 04:08:30 +000076Interpreter::runFunction(Function *F,
77 const std::vector<GenericValue> &ArgValues) {
Brian Gaekea7669032003-09-05 18:42:01 +000078 assert (F && "Function *F was null at entry to run()");
Brian Gaeke84217652003-09-05 04:46:26 +000079
Brian Gaekea7669032003-09-05 18:42:01 +000080 // Try extra hard not to pass extra args to a function that isn't
81 // expecting them. C programmers frequently bend the rules and
82 // declare main() with fewer parameters than it actually gets
83 // passed, and the interpreter barfs if you pass a function more
84 // parameters than it is declared to take. This does not attempt to
85 // take into account gratuitous differences in declared types,
86 // though.
87 std::vector<GenericValue> ActualArgs;
Chris Lattnerfa829be2004-02-09 04:14:01 +000088 const unsigned ArgCount = F->getFunctionType()->getNumParams();
Brian Gaeke65c60e22003-10-24 19:59:28 +000089 for (unsigned i = 0; i < ArgCount; ++i)
Chris Lattnerfa829be2004-02-09 04:14:01 +000090 ActualArgs.push_back(ArgValues[i]);
Misha Brukman91fb9ab2005-04-21 22:43:08 +000091
Brian Gaekea7669032003-09-05 18:42:01 +000092 // Set up the function call.
93 callFunction(F, ActualArgs);
Brian Gaeke84217652003-09-05 04:46:26 +000094
Brian Gaekea7669032003-09-05 18:42:01 +000095 // Start executing the function.
96 run();
Misha Brukman91fb9ab2005-04-21 22:43:08 +000097
Jeff Cohen24396692006-02-07 05:29:44 +000098 return ExitValue;
Brian Gaeke84217652003-09-05 04:46:26 +000099}