Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===- Interpreter.cpp - Top-Level LLVM Interpreter Implementation --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 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. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 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" |
| 17 | #include "llvm/CodeGen/IntrinsicLowering.h" |
| 18 | #include "llvm/DerivedTypes.h" |
| 19 | #include "llvm/Module.h" |
| 20 | #include "llvm/ModuleProvider.h" |
Anton Korobeynikov | 357a27d | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 21 | #include <cstring> |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 24 | namespace { |
| 25 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 26 | static struct RegisterInterp { |
| 27 | RegisterInterp() { Interpreter::Register(); } |
| 28 | } InterpRegistrator; |
| 29 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 32 | namespace llvm { |
| 33 | void LinkInInterpreter() { |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | /// create - Create a new interpreter object. This can never fail. |
| 38 | /// |
Evan Cheng | a6394fc | 2008-08-08 08:11:34 +0000 | [diff] [blame] | 39 | ExecutionEngine *Interpreter::create(ModuleProvider *MP, std::string* ErrStr, |
Bill Wendling | 5ed22ac | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 40 | CodeGenOpt::Level OptLevel /*unused*/) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 41 | // Tell this ModuleProvide to materialize and release the module |
Chris Lattner | e44be00 | 2007-12-06 01:08:09 +0000 | [diff] [blame] | 42 | if (!MP->materializeModule(ErrStr)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 43 | // We got an error, just return 0 |
| 44 | return 0; |
| 45 | |
Chris Lattner | e44be00 | 2007-12-06 01:08:09 +0000 | [diff] [blame] | 46 | return new Interpreter(MP); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | //===----------------------------------------------------------------------===// |
| 50 | // Interpreter ctor - Initialize stuff |
| 51 | // |
Chris Lattner | e44be00 | 2007-12-06 01:08:09 +0000 | [diff] [blame] | 52 | Interpreter::Interpreter(ModuleProvider *M) |
| 53 | : ExecutionEngine(M), TD(M->getModule()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 54 | |
| 55 | memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped)); |
| 56 | setTargetData(&TD); |
| 57 | // Initialize the "backend" |
| 58 | initializeExecutionEngine(); |
| 59 | initializeExternalFunctions(); |
| 60 | emitGlobals(); |
| 61 | |
| 62 | IL = new IntrinsicLowering(TD); |
| 63 | } |
| 64 | |
| 65 | Interpreter::~Interpreter() { |
| 66 | delete IL; |
| 67 | } |
| 68 | |
| 69 | void Interpreter::runAtExitHandlers () { |
| 70 | while (!AtExitHandlers.empty()) { |
| 71 | callFunction(AtExitHandlers.back(), std::vector<GenericValue>()); |
| 72 | AtExitHandlers.pop_back(); |
| 73 | run(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /// run - Start execution with the specified function and arguments. |
| 78 | /// |
| 79 | GenericValue |
| 80 | Interpreter::runFunction(Function *F, |
| 81 | const std::vector<GenericValue> &ArgValues) { |
| 82 | assert (F && "Function *F was null at entry to run()"); |
| 83 | |
| 84 | // 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. |
| 91 | std::vector<GenericValue> ActualArgs; |
| 92 | const unsigned ArgCount = F->getFunctionType()->getNumParams(); |
| 93 | for (unsigned i = 0; i < ArgCount; ++i) |
| 94 | ActualArgs.push_back(ArgValues[i]); |
| 95 | |
| 96 | // Set up the function call. |
| 97 | callFunction(F, ActualArgs); |
| 98 | |
| 99 | // Start executing the function. |
| 100 | run(); |
| 101 | |
| 102 | return ExitValue; |
| 103 | } |
| 104 | |