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