blob: 8cb9d45bb6806759390f0015fd6ee9d340508503 [file] [log] [blame]
Chris Lattner996fe012002-12-24 00:01:05 +00001//===- Interpreter.cpp - Top-Level LLVM Interpreter Implementation --------===//
Misha Brukman91fb9ab2005-04-21 22:43:08 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman91fb9ab2005-04-21 22:43:08 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner996fe012002-12-24 00:01:05 +00009//
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"
Chris Lattnerbcdadf32004-06-20 07:49:54 +000017#include "llvm/CodeGen/IntrinsicLowering.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/DerivedTypes.h"
19#include "llvm/IR/Module.h"
Anton Korobeynikov579f0712008-02-20 11:08:44 +000020#include <cstring>
Chris Lattner18099662003-12-14 23:25:48 +000021using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000022
Dan Gohmand78c4002008-05-13 00:00:25 +000023namespace {
24
Chris Lattner2d52c1b2006-03-22 06:07:50 +000025static struct RegisterInterp {
26 RegisterInterp() { Interpreter::Register(); }
27} InterpRegistrator;
28
Dan Gohmand78c4002008-05-13 00:00:25 +000029}
30
Bob Wilsona1d3e662009-06-24 21:09:18 +000031extern "C" void LLVMLinkInInterpreter() { }
Jeff Cohen0eafbc32006-03-24 02:53:49 +000032
Rafael Espindola2a8a2792014-08-19 04:04:25 +000033/// Create a new interpreter object.
Chris Lattner996fe012002-12-24 00:01:05 +000034///
Rafael Espindola2a8a2792014-08-19 04:04:25 +000035ExecutionEngine *Interpreter::create(std::unique_ptr<Module> M,
36 std::string *ErrStr) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000037 // Tell this Module to materialize everything and release the GVMaterializer.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000038 if (std::error_code EC = M->materializeAllPermanently()) {
Rafael Espindolae9fab9b2014-01-14 23:51:27 +000039 if (ErrStr)
40 *ErrStr = EC.message();
Reid Spencer603682a2007-03-03 18:19:18 +000041 // We got an error, just return 0
Craig Topper353eda42014-04-24 06:44:33 +000042 return nullptr;
Rafael Espindolae9fab9b2014-01-14 23:51:27 +000043 }
Reid Spencer603682a2007-03-03 18:19:18 +000044
Rafael Espindola2a8a2792014-08-19 04:04:25 +000045 return new Interpreter(std::move(M));
Chris Lattner996fe012002-12-24 00:01:05 +000046}
47
48//===----------------------------------------------------------------------===//
49// Interpreter ctor - Initialize stuff
50//
Rafael Espindola2a8a2792014-08-19 04:04:25 +000051Interpreter::Interpreter(std::unique_ptr<Module> M)
Mehdi Aminia3fcefb2015-07-16 16:34:23 +000052 : ExecutionEngine(std::move(M)) {
Rafael Espindola2a8a2792014-08-19 04:04:25 +000053
Reid Spencerff38cf82007-06-01 22:23:29 +000054 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
Chris Lattner996fe012002-12-24 00:01:05 +000055 // Initialize the "backend"
56 initializeExecutionEngine();
Chris Lattner470754e2003-05-08 16:18:31 +000057 initializeExternalFunctions();
Chris Lattnerb78244f2003-05-12 02:14:34 +000058 emitGlobals();
Chris Lattnerc8c6c032003-12-28 09:44:37 +000059
Mehdi Aminia3fcefb2015-07-16 16:34:23 +000060 IL = new IntrinsicLowering(getDataLayout());
Chris Lattnerc8c6c032003-12-28 09:44:37 +000061}
62
63Interpreter::~Interpreter() {
64 delete IL;
Chris Lattner996fe012002-12-24 00:01:05 +000065}
66
Brian Gaekea7669032003-09-05 18:42:01 +000067void Interpreter::runAtExitHandlers () {
68 while (!AtExitHandlers.empty()) {
Benjamin Kramerbd7b1c82015-06-13 19:50:29 +000069 callFunction(AtExitHandlers.back(), None);
Brian Gaekea7669032003-09-05 18:42:01 +000070 AtExitHandlers.pop_back();
Chris Lattner996fe012002-12-24 00:01:05 +000071 run();
72 }
Chris Lattner996fe012002-12-24 00:01:05 +000073}
74
Brian Gaekea7669032003-09-05 18:42:01 +000075/// run - Start execution with the specified function and arguments.
76///
Benjamin Kramerbd7b1c82015-06-13 19:50:29 +000077GenericValue Interpreter::runFunction(Function *F,
78 ArrayRef<GenericValue> ArgValues) {
Brian Gaekea7669032003-09-05 18:42:01 +000079 assert (F && "Function *F was null at entry to run()");
Brian Gaeke84217652003-09-05 04:46:26 +000080
Brian Gaekea7669032003-09-05 18:42:01 +000081 // Try extra hard not to pass extra args to a function that isn't
82 // expecting them. C programmers frequently bend the rules and
83 // declare main() with fewer parameters than it actually gets
84 // passed, and the interpreter barfs if you pass a function more
85 // parameters than it is declared to take. This does not attempt to
86 // take into account gratuitous differences in declared types,
87 // though.
Benjamin Kramerbd7b1c82015-06-13 19:50:29 +000088 const size_t ArgCount = F->getFunctionType()->getNumParams();
89 ArrayRef<GenericValue> ActualArgs =
90 ArgValues.slice(0, std::min(ArgValues.size(), ArgCount));
Misha Brukman91fb9ab2005-04-21 22:43:08 +000091
Brian Gaekea7669032003-09-05 18:42:01 +000092 // Set up the function call.
93 callFunction(F, ActualArgs);
Brian Gaeke84217652003-09-05 04:46:26 +000094
Brian Gaekea7669032003-09-05 18:42:01 +000095 // Start executing the function.
96 run();
Misha Brukman91fb9ab2005-04-21 22:43:08 +000097
Jeff Cohen24396692006-02-07 05:29:44 +000098 return ExitValue;
Brian Gaeke84217652003-09-05 04:46:26 +000099}