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