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