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 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 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" |
| 21 | using namespace llvm; |
| 22 | |
| 23 | static struct RegisterInterp { |
| 24 | RegisterInterp() { Interpreter::Register(); } |
| 25 | } InterpRegistrator; |
| 26 | |
| 27 | namespace llvm { |
| 28 | void LinkInInterpreter() { |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /// create - Create a new interpreter object. This can never fail. |
| 33 | /// |
| 34 | ExecutionEngine *Interpreter::create(ModuleProvider *MP, std::string* ErrStr) { |
| 35 | // Tell this ModuleProvide to materialize and release the module |
| 36 | Module *M = MP->releaseModule(ErrStr); |
| 37 | if (!M) |
| 38 | // We got an error, just return 0 |
| 39 | return 0; |
| 40 | |
| 41 | // This is a bit nasty, but the ExecutionEngine won't be able to delete the |
| 42 | // module due to use/def issues if we don't delete this MP here. Below we |
| 43 | // construct a new Interpreter with the Module we just got. This creates a |
| 44 | // new ExistingModuleProvider in the EE instance. Consequently, MP is left |
| 45 | // dangling and it contains references into the module which cause problems |
| 46 | // when the module is deleted via the ExistingModuleProvide via EE. |
| 47 | delete MP; |
| 48 | |
| 49 | return new Interpreter(M); |
| 50 | } |
| 51 | |
| 52 | //===----------------------------------------------------------------------===// |
| 53 | // Interpreter ctor - Initialize stuff |
| 54 | // |
| 55 | Interpreter::Interpreter(Module *M) : ExecutionEngine(M), TD(M) { |
| 56 | |
| 57 | memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped)); |
| 58 | setTargetData(&TD); |
| 59 | // Initialize the "backend" |
| 60 | initializeExecutionEngine(); |
| 61 | initializeExternalFunctions(); |
| 62 | emitGlobals(); |
| 63 | |
| 64 | IL = new IntrinsicLowering(TD); |
| 65 | } |
| 66 | |
| 67 | Interpreter::~Interpreter() { |
| 68 | delete IL; |
| 69 | } |
| 70 | |
| 71 | void Interpreter::runAtExitHandlers () { |
| 72 | while (!AtExitHandlers.empty()) { |
| 73 | callFunction(AtExitHandlers.back(), std::vector<GenericValue>()); |
| 74 | AtExitHandlers.pop_back(); |
| 75 | run(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /// run - Start execution with the specified function and arguments. |
| 80 | /// |
| 81 | GenericValue |
| 82 | Interpreter::runFunction(Function *F, |
| 83 | const std::vector<GenericValue> &ArgValues) { |
| 84 | assert (F && "Function *F was null at entry to run()"); |
| 85 | |
| 86 | // Try extra hard not to pass extra args to a function that isn't |
| 87 | // expecting them. C programmers frequently bend the rules and |
| 88 | // declare main() with fewer parameters than it actually gets |
| 89 | // passed, and the interpreter barfs if you pass a function more |
| 90 | // parameters than it is declared to take. This does not attempt to |
| 91 | // take into account gratuitous differences in declared types, |
| 92 | // though. |
| 93 | std::vector<GenericValue> ActualArgs; |
| 94 | const unsigned ArgCount = F->getFunctionType()->getNumParams(); |
| 95 | for (unsigned i = 0; i < ArgCount; ++i) |
| 96 | ActualArgs.push_back(ArgValues[i]); |
| 97 | |
| 98 | // Set up the function call. |
| 99 | callFunction(F, ActualArgs); |
| 100 | |
| 101 | // Start executing the function. |
| 102 | run(); |
| 103 | |
| 104 | return ExitValue; |
| 105 | } |
| 106 | |