blob: 9818adfff82ed84fb3e4f1de8693a574bb5b6d84 [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.
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +000038 if (Error Err = M->materializeAll()) {
39 std::string Msg;
40 handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
41 Msg = EIB.message();
42 });
Rafael Espindolae9fab9b2014-01-14 23:51:27 +000043 if (ErrStr)
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +000044 *ErrStr = Msg;
Reid Spencer603682a2007-03-03 18:19:18 +000045 // We got an error, just return 0
Craig Topper353eda42014-04-24 06:44:33 +000046 return nullptr;
Rafael Espindolae9fab9b2014-01-14 23:51:27 +000047 }
Reid Spencer603682a2007-03-03 18:19:18 +000048
Rafael Espindola2a8a2792014-08-19 04:04:25 +000049 return new Interpreter(std::move(M));
Chris Lattner996fe012002-12-24 00:01:05 +000050}
51
52//===----------------------------------------------------------------------===//
53// Interpreter ctor - Initialize stuff
54//
Rafael Espindola2a8a2792014-08-19 04:04:25 +000055Interpreter::Interpreter(std::unique_ptr<Module> M)
Mehdi Aminia3fcefb2015-07-16 16:34:23 +000056 : ExecutionEngine(std::move(M)) {
Rafael Espindola2a8a2792014-08-19 04:04:25 +000057
Reid Spencerff38cf82007-06-01 22:23:29 +000058 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
Chris Lattner996fe012002-12-24 00:01:05 +000059 // Initialize the "backend"
60 initializeExecutionEngine();
Chris Lattner470754e2003-05-08 16:18:31 +000061 initializeExternalFunctions();
Chris Lattnerb78244f2003-05-12 02:14:34 +000062 emitGlobals();
Chris Lattnerc8c6c032003-12-28 09:44:37 +000063
Mehdi Aminia3fcefb2015-07-16 16:34:23 +000064 IL = new IntrinsicLowering(getDataLayout());
Chris Lattnerc8c6c032003-12-28 09:44:37 +000065}
66
67Interpreter::~Interpreter() {
68 delete IL;
Chris Lattner996fe012002-12-24 00:01:05 +000069}
70
Brian Gaekea7669032003-09-05 18:42:01 +000071void Interpreter::runAtExitHandlers () {
72 while (!AtExitHandlers.empty()) {
Benjamin Kramerbd7b1c82015-06-13 19:50:29 +000073 callFunction(AtExitHandlers.back(), None);
Brian Gaekea7669032003-09-05 18:42:01 +000074 AtExitHandlers.pop_back();
Chris Lattner996fe012002-12-24 00:01:05 +000075 run();
76 }
Chris Lattner996fe012002-12-24 00:01:05 +000077}
78
Brian Gaekea7669032003-09-05 18:42:01 +000079/// run - Start execution with the specified function and arguments.
80///
Benjamin Kramerbd7b1c82015-06-13 19:50:29 +000081GenericValue Interpreter::runFunction(Function *F,
82 ArrayRef<GenericValue> ArgValues) {
Brian Gaekea7669032003-09-05 18:42:01 +000083 assert (F && "Function *F was null at entry to run()");
Brian Gaeke84217652003-09-05 04:46:26 +000084
Brian Gaekea7669032003-09-05 18:42:01 +000085 // 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.
Benjamin Kramerbd7b1c82015-06-13 19:50:29 +000092 const size_t ArgCount = F->getFunctionType()->getNumParams();
93 ArrayRef<GenericValue> ActualArgs =
94 ArgValues.slice(0, std::min(ArgValues.size(), ArgCount));
Misha Brukman91fb9ab2005-04-21 22:43:08 +000095
Brian Gaekea7669032003-09-05 18:42:01 +000096 // Set up the function call.
97 callFunction(F, ActualArgs);
Brian Gaeke84217652003-09-05 04:46:26 +000098
Brian Gaekea7669032003-09-05 18:42:01 +000099 // Start executing the function.
100 run();
Misha Brukman91fb9ab2005-04-21 22:43:08 +0000101
Jeff Cohen24396692006-02-07 05:29:44 +0000102 return ExitValue;
Brian Gaeke84217652003-09-05 04:46:26 +0000103}