1 //===- Interpreter.cpp - Top-Level LLVM Interpreter Implementation --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the top-level functionality for the LLVM interpreter.
11 // This interpreter is designed to be a very simple, portable, inefficient
14 //===----------------------------------------------------------------------===//
16 #include "Interpreter.h"
17 #include "llvm/CodeGen/IntrinsicLowering.h"
18 #include "llvm/IR/DerivedTypes.h"
19 #include "llvm/IR/Module.h"
25 static struct RegisterInterp {
26 RegisterInterp() { Interpreter::Register(); }
31 extern "C" void LLVMLinkInInterpreter() { }
33 /// Create a new interpreter object.
35 ExecutionEngine *Interpreter::create(std::unique_ptr<Module> M,
36 std::string *ErrStr) {
37 // Tell this Module to materialize everything and release the GVMaterializer.
38 if (Error Err = M->materializeAll()) {
40 handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
45 // We got an error, just return 0
49 return new Interpreter(std::move(M));
52 //===----------------------------------------------------------------------===//
53 // Interpreter ctor - Initialize stuff
55 Interpreter::Interpreter(std::unique_ptr<Module> M)
56 : ExecutionEngine(std::move(M)) {
58 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
59 // Initialize the "backend"
60 initializeExecutionEngine();
61 initializeExternalFunctions();
64 IL = new IntrinsicLowering(getDataLayout());
67 Interpreter::~Interpreter() {
71 void Interpreter::runAtExitHandlers () {
72 while (!AtExitHandlers.empty()) {
73 callFunction(AtExitHandlers.back(), None);
74 AtExitHandlers.pop_back();
79 /// run - Start execution with the specified function and arguments.
81 GenericValue Interpreter::runFunction(Function *F,
82 ArrayRef<GenericValue> ArgValues) {
83 assert (F && "Function *F was null at entry to run()");
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 // 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,
92 const size_t ArgCount = F->getFunctionType()->getNumParams();
93 ArrayRef<GenericValue> ActualArgs =
94 ArgValues.slice(0, std::min(ArgValues.size(), ArgCount));
96 // Set up the function call.
97 callFunction(F, ActualArgs);
99 // Start executing the function.