blob: 5d7f9018542c512792495da687d8bd6ffa7a97b1 [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
24static struct RegisterInterp {
25 RegisterInterp() { Interpreter::Register(); }
26} InterpRegistrator;
27
28namespace llvm {
29 void LinkInInterpreter() {
30 }
31}
32
33/// create - Create a new interpreter object. This can never fail.
34///
35ExecutionEngine *Interpreter::create(ModuleProvider *MP, std::string* ErrStr) {
36 // Tell this ModuleProvide to materialize and release the module
Chris Lattnere44be002007-12-06 01:08:09 +000037 if (!MP->materializeModule(ErrStr))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038 // We got an error, just return 0
39 return 0;
40
Chris Lattnere44be002007-12-06 01:08:09 +000041 return new Interpreter(MP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042}
43
44//===----------------------------------------------------------------------===//
45// Interpreter ctor - Initialize stuff
46//
Chris Lattnere44be002007-12-06 01:08:09 +000047Interpreter::Interpreter(ModuleProvider *M)
48 : ExecutionEngine(M), TD(M->getModule()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049
50 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
51 setTargetData(&TD);
52 // Initialize the "backend"
53 initializeExecutionEngine();
54 initializeExternalFunctions();
55 emitGlobals();
56
57 IL = new IntrinsicLowering(TD);
58}
59
60Interpreter::~Interpreter() {
61 delete IL;
62}
63
64void Interpreter::runAtExitHandlers () {
65 while (!AtExitHandlers.empty()) {
66 callFunction(AtExitHandlers.back(), std::vector<GenericValue>());
67 AtExitHandlers.pop_back();
68 run();
69 }
70}
71
72/// run - Start execution with the specified function and arguments.
73///
74GenericValue
75Interpreter::runFunction(Function *F,
76 const std::vector<GenericValue> &ArgValues) {
77 assert (F && "Function *F was null at entry to run()");
78
79 // Try extra hard not to pass extra args to a function that isn't
80 // expecting them. C programmers frequently bend the rules and
81 // declare main() with fewer parameters than it actually gets
82 // passed, and the interpreter barfs if you pass a function more
83 // parameters than it is declared to take. This does not attempt to
84 // take into account gratuitous differences in declared types,
85 // though.
86 std::vector<GenericValue> ActualArgs;
87 const unsigned ArgCount = F->getFunctionType()->getNumParams();
88 for (unsigned i = 0; i < ArgCount; ++i)
89 ActualArgs.push_back(ArgValues[i]);
90
91 // Set up the function call.
92 callFunction(F, ActualArgs);
93
94 // Start executing the function.
95 run();
96
97 return ExitValue;
98}
99