blob: 8562981b629adaee40a785798fb61b8dab30f1d2 [file] [log] [blame]
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001//===- Interpreter.cpp - Top-Level LLVM Interpreter Implementation --------===//
Misha Brukmand1c881a2005-04-21 22:43:08 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmand1c881a2005-04-21 22:43:08 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerbd199fb2002-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 Lattner30483732004-06-20 07:49:54 +000017#include "llvm/CodeGen/IntrinsicLowering.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000018#include "llvm/IR/DerivedTypes.h"
19#include "llvm/IR/Module.h"
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000020#include <cstring>
Chris Lattnerf7a743d2003-12-14 23:25:48 +000021using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000022
Dan Gohman844731a2008-05-13 00:00:25 +000023namespace {
24
Chris Lattner2fe4bb02006-03-22 06:07:50 +000025static struct RegisterInterp {
26 RegisterInterp() { Interpreter::Register(); }
27} InterpRegistrator;
28
Dan Gohman844731a2008-05-13 00:00:25 +000029}
30
Bob Wilsone46161f2009-06-24 21:09:18 +000031extern "C" void LLVMLinkInInterpreter() { }
Jeff Cohen2f519142006-03-24 02:53:49 +000032
Stephen Hines37ed9c12014-12-01 14:51:49 -080033/// Create a new interpreter object.
Chris Lattnerbd199fb2002-12-24 00:01:05 +000034///
Stephen Hines37ed9c12014-12-01 14:51:49 -080035ExecutionEngine *Interpreter::create(std::unique_ptr<Module> M,
36 std::string *ErrStr) {
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +000037 // Tell this Module to materialize everything and release the GVMaterializer.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070038 if (std::error_code EC = M->materializeAllPermanently()) {
Stephen Hines36b56882014-04-23 16:57:46 -070039 if (ErrStr)
40 *ErrStr = EC.message();
Reid Spencerd4c0e622007-03-03 18:19:18 +000041 // We got an error, just return 0
Stephen Hinesdce4a402014-05-29 02:49:00 -070042 return nullptr;
Stephen Hines36b56882014-04-23 16:57:46 -070043 }
Reid Spencerd4c0e622007-03-03 18:19:18 +000044
Stephen Hines37ed9c12014-12-01 14:51:49 -080045 return new Interpreter(std::move(M));
Chris Lattnerbd199fb2002-12-24 00:01:05 +000046}
47
48//===----------------------------------------------------------------------===//
49// Interpreter ctor - Initialize stuff
50//
Stephen Hines37ed9c12014-12-01 14:51:49 -080051Interpreter::Interpreter(std::unique_ptr<Module> M)
52 : ExecutionEngine(std::move(M)), TD(Modules.back().get()) {
53
Reid Spencere7707872007-06-01 22:23:29 +000054 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
Micah Villmow3574eca2012-10-08 16:38:25 +000055 setDataLayout(&TD);
Chris Lattnerbd199fb2002-12-24 00:01:05 +000056 // Initialize the "backend"
57 initializeExecutionEngine();
Chris Lattnerda82ed52003-05-08 16:18:31 +000058 initializeExternalFunctions();
Chris Lattner56adf152003-05-12 02:14:34 +000059 emitGlobals();
Chris Lattner73011782003-12-28 09:44:37 +000060
Reid Spencer519e2392007-01-29 17:51:02 +000061 IL = new IntrinsicLowering(TD);
Chris Lattner73011782003-12-28 09:44:37 +000062}
63
64Interpreter::~Interpreter() {
65 delete IL;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000066}
67
Brian Gaeke70975ee2003-09-05 18:42:01 +000068void Interpreter::runAtExitHandlers () {
69 while (!AtExitHandlers.empty()) {
70 callFunction(AtExitHandlers.back(), std::vector<GenericValue>());
71 AtExitHandlers.pop_back();
Chris Lattnerbd199fb2002-12-24 00:01:05 +000072 run();
73 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +000074}
75
Brian Gaeke70975ee2003-09-05 18:42:01 +000076/// run - Start execution with the specified function and arguments.
77///
Jeff Cohen00b168892005-07-27 06:12:32 +000078GenericValue
Misha Brukman3c944972005-04-22 04:08:30 +000079Interpreter::runFunction(Function *F,
80 const std::vector<GenericValue> &ArgValues) {
Brian Gaeke70975ee2003-09-05 18:42:01 +000081 assert (F && "Function *F was null at entry to run()");
Brian Gaeke413ab662003-09-05 04:46:26 +000082
Brian Gaeke70975ee2003-09-05 18:42:01 +000083 // Try extra hard not to pass extra args to a function that isn't
84 // expecting them. C programmers frequently bend the rules and
85 // declare main() with fewer parameters than it actually gets
86 // passed, and the interpreter barfs if you pass a function more
87 // parameters than it is declared to take. This does not attempt to
88 // take into account gratuitous differences in declared types,
89 // though.
90 std::vector<GenericValue> ActualArgs;
Chris Lattnerd5d89962004-02-09 04:14:01 +000091 const unsigned ArgCount = F->getFunctionType()->getNumParams();
Brian Gaekea824f422003-10-24 19:59:28 +000092 for (unsigned i = 0; i < ArgCount; ++i)
Chris Lattnerd5d89962004-02-09 04:14:01 +000093 ActualArgs.push_back(ArgValues[i]);
Misha Brukmand1c881a2005-04-21 22:43:08 +000094
Brian Gaeke70975ee2003-09-05 18:42:01 +000095 // Set up the function call.
96 callFunction(F, ActualArgs);
Brian Gaeke413ab662003-09-05 04:46:26 +000097
Brian Gaeke70975ee2003-09-05 18:42:01 +000098 // Start executing the function.
99 run();
Misha Brukmand1c881a2005-04-21 22:43:08 +0000100
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000101 return ExitValue;
Brian Gaeke413ab662003-09-05 04:46:26 +0000102}