Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 1 | //===- Interpreter.cpp - Top-Level LLVM Interpreter Implementation --------===// |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 9 | // |
| 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 Lattner | 3048373 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/IntrinsicLowering.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 18 | #include "llvm/IR/DerivedTypes.h" |
| 19 | #include "llvm/IR/Module.h" |
Anton Korobeynikov | ae9f3a3 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 20 | #include <cstring> |
Chris Lattner | f7a743d | 2003-12-14 23:25:48 +0000 | [diff] [blame] | 21 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 22 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 23 | namespace { |
| 24 | |
Chris Lattner | 2fe4bb0 | 2006-03-22 06:07:50 +0000 | [diff] [blame] | 25 | static struct RegisterInterp { |
| 26 | RegisterInterp() { Interpreter::Register(); } |
| 27 | } InterpRegistrator; |
| 28 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Bob Wilson | e46161f | 2009-06-24 21:09:18 +0000 | [diff] [blame] | 31 | extern "C" void LLVMLinkInInterpreter() { } |
Jeff Cohen | 2f51914 | 2006-03-24 02:53:49 +0000 | [diff] [blame] | 32 | |
Brian Gaeke | 82d8277 | 2003-09-03 20:34:19 +0000 | [diff] [blame] | 33 | /// create - Create a new interpreter object. This can never fail. |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 34 | /// |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 35 | ExecutionEngine *Interpreter::create(Module *M, std::string* ErrStr) { |
| 36 | // Tell this Module to materialize everything and release the GVMaterializer. |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame^] | 37 | if (std::error_code EC = M->materializeAllPermanently()) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 38 | if (ErrStr) |
| 39 | *ErrStr = EC.message(); |
Reid Spencer | d4c0e62 | 2007-03-03 18:19:18 +0000 | [diff] [blame] | 40 | // We got an error, just return 0 |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 41 | return nullptr; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 42 | } |
Reid Spencer | d4c0e62 | 2007-03-03 18:19:18 +0000 | [diff] [blame] | 43 | |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 44 | return new Interpreter(M); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | //===----------------------------------------------------------------------===// |
| 48 | // Interpreter ctor - Initialize stuff |
| 49 | // |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 50 | Interpreter::Interpreter(Module *M) |
| 51 | : ExecutionEngine(M), TD(M) { |
Chris Lattner | 276f4b5 | 2006-06-16 18:08:38 +0000 | [diff] [blame] | 52 | |
Reid Spencer | e770787 | 2007-06-01 22:23:29 +0000 | [diff] [blame] | 53 | memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped)); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 54 | setDataLayout(&TD); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 55 | // Initialize the "backend" |
| 56 | initializeExecutionEngine(); |
Chris Lattner | da82ed5 | 2003-05-08 16:18:31 +0000 | [diff] [blame] | 57 | initializeExternalFunctions(); |
Chris Lattner | 56adf15 | 2003-05-12 02:14:34 +0000 | [diff] [blame] | 58 | emitGlobals(); |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 59 | |
Reid Spencer | 519e239 | 2007-01-29 17:51:02 +0000 | [diff] [blame] | 60 | IL = new IntrinsicLowering(TD); |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | Interpreter::~Interpreter() { |
| 64 | delete IL; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 67 | void Interpreter::runAtExitHandlers () { |
| 68 | while (!AtExitHandlers.empty()) { |
| 69 | callFunction(AtExitHandlers.back(), std::vector<GenericValue>()); |
| 70 | AtExitHandlers.pop_back(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 71 | run(); |
| 72 | } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 75 | /// run - Start execution with the specified function and arguments. |
| 76 | /// |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 77 | GenericValue |
Misha Brukman | 3c94497 | 2005-04-22 04:08:30 +0000 | [diff] [blame] | 78 | Interpreter::runFunction(Function *F, |
| 79 | const std::vector<GenericValue> &ArgValues) { |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 80 | assert (F && "Function *F was null at entry to run()"); |
Brian Gaeke | 413ab66 | 2003-09-05 04:46:26 +0000 | [diff] [blame] | 81 | |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 82 | // Try extra hard not to pass extra args to a function that isn't |
| 83 | // expecting them. C programmers frequently bend the rules and |
| 84 | // declare main() with fewer parameters than it actually gets |
| 85 | // passed, and the interpreter barfs if you pass a function more |
| 86 | // parameters than it is declared to take. This does not attempt to |
| 87 | // take into account gratuitous differences in declared types, |
| 88 | // though. |
| 89 | std::vector<GenericValue> ActualArgs; |
Chris Lattner | d5d8996 | 2004-02-09 04:14:01 +0000 | [diff] [blame] | 90 | const unsigned ArgCount = F->getFunctionType()->getNumParams(); |
Brian Gaeke | a824f42 | 2003-10-24 19:59:28 +0000 | [diff] [blame] | 91 | for (unsigned i = 0; i < ArgCount; ++i) |
Chris Lattner | d5d8996 | 2004-02-09 04:14:01 +0000 | [diff] [blame] | 92 | ActualArgs.push_back(ArgValues[i]); |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 93 | |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 94 | // Set up the function call. |
| 95 | callFunction(F, ActualArgs); |
Brian Gaeke | 413ab66 | 2003-09-05 04:46:26 +0000 | [diff] [blame] | 96 | |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 97 | // Start executing the function. |
| 98 | run(); |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 99 | |
Jeff Cohen | 8c9191c | 2006-02-07 05:29:44 +0000 | [diff] [blame] | 100 | return ExitValue; |
Brian Gaeke | 413ab66 | 2003-09-05 04:46:26 +0000 | [diff] [blame] | 101 | } |