• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
RegisterInterp__anon8bd5f8450111::RegisterInterp26   RegisterInterp() { Interpreter::Register(); }
27 } InterpRegistrator;
28 
29 }
30 
LLVMLinkInInterpreter()31 extern "C" void LLVMLinkInInterpreter() { }
32 
33 /// Create a new interpreter object.
34 ///
create(std::unique_ptr<Module> M,std::string * ErrStr)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()) {
39     std::string Msg;
40     handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
41       Msg = EIB.message();
42     });
43     if (ErrStr)
44       *ErrStr = Msg;
45     // We got an error, just return 0
46     return nullptr;
47   }
48 
49   return new Interpreter(std::move(M));
50 }
51 
52 //===----------------------------------------------------------------------===//
53 // Interpreter ctor - Initialize stuff
54 //
Interpreter(std::unique_ptr<Module> M)55 Interpreter::Interpreter(std::unique_ptr<Module> M)
56     : ExecutionEngine(std::move(M)) {
57 
58   memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
59   // Initialize the "backend"
60   initializeExecutionEngine();
61   initializeExternalFunctions();
62   emitGlobals();
63 
64   IL = new IntrinsicLowering(getDataLayout());
65 }
66 
~Interpreter()67 Interpreter::~Interpreter() {
68   delete IL;
69 }
70 
runAtExitHandlers()71 void Interpreter::runAtExitHandlers () {
72   while (!AtExitHandlers.empty()) {
73     callFunction(AtExitHandlers.back(), None);
74     AtExitHandlers.pop_back();
75     run();
76   }
77 }
78 
79 /// run - Start execution with the specified function and arguments.
80 ///
runFunction(Function * F,ArrayRef<GenericValue> ArgValues)81 GenericValue Interpreter::runFunction(Function *F,
82                                       ArrayRef<GenericValue> ArgValues) {
83   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   // 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 
99   // Start executing the function.
100   run();
101 
102   return ExitValue;
103 }
104