Line data Source code
1 : //===- Interpreter.cpp - Top-Level LLVM Interpreter Implementation --------===//
2 : //
3 : // The LLVM Compiler Infrastructure
4 : //
5 : // This file is distributed under the University of Illinois Open Source
6 : // 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/IR/DerivedTypes.h"
19 : #include "llvm/IR/Module.h"
20 : #include <cstring>
21 : using namespace llvm;
22 :
23 : namespace {
24 :
25 : static struct RegisterInterp {
26 0 : RegisterInterp() { Interpreter::Register(); }
27 : } InterpRegistrator;
28 :
29 : }
30 :
31 200 : extern "C" void LLVMLinkInInterpreter() { }
32 :
33 : /// Create a new interpreter object.
34 : ///
35 24 : ExecutionEngine *Interpreter::create(std::unique_ptr<Module> M,
36 : std::string *ErrStr) {
37 : // Tell this Module to materialize everything and release the GVMaterializer.
38 48 : if (Error Err = M->materializeAll()) {
39 : std::string Msg;
40 0 : handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
41 : Msg = EIB.message();
42 : });
43 0 : if (ErrStr)
44 : *ErrStr = Msg;
45 : // We got an error, just return 0
46 : return nullptr;
47 : }
48 :
49 24 : return new Interpreter(std::move(M));
50 : }
51 :
52 : //===----------------------------------------------------------------------===//
53 : // Interpreter ctor - Initialize stuff
54 : //
55 24 : Interpreter::Interpreter(std::unique_ptr<Module> M)
56 48 : : ExecutionEngine(std::move(M)) {
57 :
58 24 : memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
59 : // Initialize the "backend"
60 : initializeExecutionEngine();
61 24 : initializeExternalFunctions();
62 24 : emitGlobals();
63 :
64 24 : IL = new IntrinsicLowering(getDataLayout());
65 24 : }
66 :
67 10 : Interpreter::~Interpreter() {
68 5 : delete IL;
69 10 : }
70 5 :
71 : void Interpreter::runAtExitHandlers () {
72 5 : while (!AtExitHandlers.empty()) {
73 5 : callFunction(AtExitHandlers.back(), None);
74 5 : AtExitHandlers.pop_back();
75 5 : run();
76 : }
77 19 : }
78 19 :
79 0 : /// run - Start execution with the specified function and arguments.
80 : ///
81 0 : GenericValue Interpreter::runFunction(Function *F,
82 : ArrayRef<GenericValue> ArgValues) {
83 19 : assert (F && "Function *F was null at entry to run()");
84 :
85 : // Try extra hard not to pass extra args to a function that isn't
86 : // expecting them. C programmers frequently bend the rules and
87 38 : // declare main() with fewer parameters than it actually gets
88 : // passed, and the interpreter barfs if you pass a function more
89 : // parameters than it is declared to take. This does not attempt to
90 : // take into account gratuitous differences in declared types,
91 : // though.
92 : const size_t ArgCount = F->getFunctionType()->getNumParams();
93 : ArrayRef<GenericValue> ActualArgs =
94 : ArgValues.slice(0, std::min(ArgValues.size(), ArgCount));
95 :
96 : // Set up the function call.
97 : callFunction(F, ActualArgs);
98 38 :
99 : // Start executing the function.
100 38 : run();
101 :
102 : return ExitValue;
103 38 : }
|