blob: 9be6a9265d613a1954af2ea2d7ba0fb4ff527d99 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- Interpreter.cpp - Top-Level LLVM Interpreter Implementation --------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
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"
17#include "llvm/CodeGen/IntrinsicLowering.h"
18#include "llvm/DerivedTypes.h"
19#include "llvm/Module.h"
20#include "llvm/ModuleProvider.h"
Anton Korobeynikov357a27d2008-02-20 11:08:44 +000021#include <cstring>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022using namespace llvm;
23
Dan Gohman089efff2008-05-13 00:00:25 +000024namespace {
25
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026static struct RegisterInterp {
27 RegisterInterp() { Interpreter::Register(); }
28} InterpRegistrator;
29
Dan Gohman089efff2008-05-13 00:00:25 +000030}
31
Bob Wilson3217b3e2009-06-24 21:09:18 +000032extern "C" void LLVMLinkInInterpreter() { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033
34/// create - Create a new interpreter object. This can never fail.
35///
Reid Klecknerfa3585b2009-07-18 00:42:18 +000036ExecutionEngine *Interpreter::create(ModuleProvider *MP, std::string* ErrStr) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037 // Tell this ModuleProvide to materialize and release the module
Chris Lattnere44be002007-12-06 01:08:09 +000038 if (!MP->materializeModule(ErrStr))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039 // We got an error, just return 0
40 return 0;
41
Chris Lattnere44be002007-12-06 01:08:09 +000042 return new Interpreter(MP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043}
44
45//===----------------------------------------------------------------------===//
46// Interpreter ctor - Initialize stuff
47//
Chris Lattnere44be002007-12-06 01:08:09 +000048Interpreter::Interpreter(ModuleProvider *M)
49 : ExecutionEngine(M), TD(M->getModule()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050
51 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
52 setTargetData(&TD);
53 // Initialize the "backend"
54 initializeExecutionEngine();
55 initializeExternalFunctions();
56 emitGlobals();
57
58 IL = new IntrinsicLowering(TD);
59}
60
61Interpreter::~Interpreter() {
62 delete IL;
63}
64
65void Interpreter::runAtExitHandlers () {
66 while (!AtExitHandlers.empty()) {
67 callFunction(AtExitHandlers.back(), std::vector<GenericValue>());
68 AtExitHandlers.pop_back();
69 run();
70 }
71}
72
73/// run - Start execution with the specified function and arguments.
74///
75GenericValue
76Interpreter::runFunction(Function *F,
77 const std::vector<GenericValue> &ArgValues) {
78 assert (F && "Function *F was null at entry to run()");
79
80 // 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;
88 const unsigned ArgCount = F->getFunctionType()->getNumParams();
89 for (unsigned i = 0; i < ArgCount; ++i)
90 ActualArgs.push_back(ArgValues[i]);
91
92 // Set up the function call.
93 callFunction(F, ActualArgs);
94
95 // Start executing the function.
96 run();
97
98 return ExitValue;
99}