blob: 5727f7adb49c3473a696d8b8348a96262d618848 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Misha Brukman91fb9ab2005-04-21 22:43:08 +00006//
John Criswell482202a2003-10-20 19:43:21 +00007//===----------------------------------------------------------------------===//
Chris Lattner996fe012002-12-24 00:01:05 +00008//
9// This file implements the top-level functionality for the LLVM interpreter.
10// This interpreter is designed to be a very simple, portable, inefficient
11// interpreter.
12//
13//===----------------------------------------------------------------------===//
14
15#include "Interpreter.h"
Chris Lattnerbcdadf32004-06-20 07:49:54 +000016#include "llvm/CodeGen/IntrinsicLowering.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/DerivedTypes.h"
18#include "llvm/IR/Module.h"
Anton Korobeynikov579f0712008-02-20 11:08:44 +000019#include <cstring>
Chris Lattner18099662003-12-14 23:25:48 +000020using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000021
Dan Gohmand78c4002008-05-13 00:00:25 +000022namespace {
23
Chris Lattner2d52c1b2006-03-22 06:07:50 +000024static struct RegisterInterp {
25 RegisterInterp() { Interpreter::Register(); }
26} InterpRegistrator;
27
Dan Gohmand78c4002008-05-13 00:00:25 +000028}
29
Bob Wilsona1d3e662009-06-24 21:09:18 +000030extern "C" void LLVMLinkInInterpreter() { }
Jeff Cohen0eafbc32006-03-24 02:53:49 +000031
Rafael Espindola2a8a2792014-08-19 04:04:25 +000032/// Create a new interpreter object.
Chris Lattner996fe012002-12-24 00:01:05 +000033///
Rafael Espindola2a8a2792014-08-19 04:04:25 +000034ExecutionEngine *Interpreter::create(std::unique_ptr<Module> M,
35 std::string *ErrStr) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000036 // Tell this Module to materialize everything and release the GVMaterializer.
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +000037 if (Error Err = M->materializeAll()) {
38 std::string Msg;
39 handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
40 Msg = EIB.message();
41 });
Rafael Espindolae9fab9b2014-01-14 23:51:27 +000042 if (ErrStr)
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +000043 *ErrStr = Msg;
Reid Spencer603682a2007-03-03 18:19:18 +000044 // We got an error, just return 0
Craig Topper353eda42014-04-24 06:44:33 +000045 return nullptr;
Rafael Espindolae9fab9b2014-01-14 23:51:27 +000046 }
Reid Spencer603682a2007-03-03 18:19:18 +000047
Rafael Espindola2a8a2792014-08-19 04:04:25 +000048 return new Interpreter(std::move(M));
Chris Lattner996fe012002-12-24 00:01:05 +000049}
50
51//===----------------------------------------------------------------------===//
52// Interpreter ctor - Initialize stuff
53//
Rafael Espindola2a8a2792014-08-19 04:04:25 +000054Interpreter::Interpreter(std::unique_ptr<Module> M)
Mehdi Aminia3fcefb2015-07-16 16:34:23 +000055 : ExecutionEngine(std::move(M)) {
Rafael Espindola2a8a2792014-08-19 04:04:25 +000056
Reid Spencerff38cf82007-06-01 22:23:29 +000057 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
Chris Lattner996fe012002-12-24 00:01:05 +000058 // Initialize the "backend"
59 initializeExecutionEngine();
Chris Lattner470754e2003-05-08 16:18:31 +000060 initializeExternalFunctions();
Chris Lattnerb78244f2003-05-12 02:14:34 +000061 emitGlobals();
Chris Lattnerc8c6c032003-12-28 09:44:37 +000062
Mehdi Aminia3fcefb2015-07-16 16:34:23 +000063 IL = new IntrinsicLowering(getDataLayout());
Chris Lattnerc8c6c032003-12-28 09:44:37 +000064}
65
66Interpreter::~Interpreter() {
67 delete IL;
Chris Lattner996fe012002-12-24 00:01:05 +000068}
69
Brian Gaekea7669032003-09-05 18:42:01 +000070void Interpreter::runAtExitHandlers () {
71 while (!AtExitHandlers.empty()) {
Benjamin Kramerbd7b1c82015-06-13 19:50:29 +000072 callFunction(AtExitHandlers.back(), None);
Brian Gaekea7669032003-09-05 18:42:01 +000073 AtExitHandlers.pop_back();
Chris Lattner996fe012002-12-24 00:01:05 +000074 run();
75 }
Chris Lattner996fe012002-12-24 00:01:05 +000076}
77
Brian Gaekea7669032003-09-05 18:42:01 +000078/// run - Start execution with the specified function and arguments.
79///
Benjamin Kramerbd7b1c82015-06-13 19:50:29 +000080GenericValue Interpreter::runFunction(Function *F,
81 ArrayRef<GenericValue> ArgValues) {
Brian Gaekea7669032003-09-05 18:42:01 +000082 assert (F && "Function *F was null at entry to run()");
Brian Gaeke84217652003-09-05 04:46:26 +000083
Brian Gaekea7669032003-09-05 18:42:01 +000084 // Try extra hard not to pass extra args to a function that isn't
85 // expecting them. C programmers frequently bend the rules and
86 // declare main() with fewer parameters than it actually gets
87 // passed, and the interpreter barfs if you pass a function more
88 // parameters than it is declared to take. This does not attempt to
89 // take into account gratuitous differences in declared types,
90 // though.
Benjamin Kramerbd7b1c82015-06-13 19:50:29 +000091 const size_t ArgCount = F->getFunctionType()->getNumParams();
92 ArrayRef<GenericValue> ActualArgs =
93 ArgValues.slice(0, std::min(ArgValues.size(), ArgCount));
Misha Brukman91fb9ab2005-04-21 22:43:08 +000094
Brian Gaekea7669032003-09-05 18:42:01 +000095 // Set up the function call.
96 callFunction(F, ActualArgs);
Brian Gaeke84217652003-09-05 04:46:26 +000097
Brian Gaekea7669032003-09-05 18:42:01 +000098 // Start executing the function.
99 run();
Misha Brukman91fb9ab2005-04-21 22:43:08 +0000100
Jeff Cohen24396692006-02-07 05:29:44 +0000101 return ExitValue;
Brian Gaeke84217652003-09-05 04:46:26 +0000102}