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