Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 1 | //===- Interpreter.cpp - Top-Level LLVM Interpreter Implementation --------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
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 | 39c0726 | 2003-08-24 19:50:53 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
Brian Gaeke | 413ab66 | 2003-09-05 04:46:26 +0000 | [diff] [blame] | 18 | #include "llvm/DerivedTypes.h" |
Chris Lattner | f7a743d | 2003-12-14 23:25:48 +0000 | [diff] [blame] | 19 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 20 | |
Brian Gaeke | 82d8277 | 2003-09-03 20:34:19 +0000 | [diff] [blame] | 21 | /// create - Create a new interpreter object. This can never fail. |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 22 | /// |
Brian Gaeke | a824f42 | 2003-10-24 19:59:28 +0000 | [diff] [blame] | 23 | ExecutionEngine *Interpreter::create(Module *M){ |
Chris Lattner | 021c190 | 2003-09-22 20:33:34 +0000 | [diff] [blame] | 24 | bool isLittleEndian = false; |
Chris Lattner | 39c0726 | 2003-08-24 19:50:53 +0000 | [diff] [blame] | 25 | switch (M->getEndianness()) { |
| 26 | case Module::LittleEndian: isLittleEndian = true; break; |
| 27 | case Module::BigEndian: isLittleEndian = false; break; |
| 28 | case Module::AnyPointerSize: |
| 29 | int Test = 0; |
| 30 | *(char*)&Test = 1; // Return true if the host is little endian |
| 31 | isLittleEndian = (Test == 1); |
| 32 | break; |
| 33 | } |
| 34 | |
Chris Lattner | 021c190 | 2003-09-22 20:33:34 +0000 | [diff] [blame] | 35 | bool isLongPointer = false; |
Chris Lattner | 39c0726 | 2003-08-24 19:50:53 +0000 | [diff] [blame] | 36 | switch (M->getPointerSize()) { |
| 37 | case Module::Pointer32: isLongPointer = false; break; |
| 38 | case Module::Pointer64: isLongPointer = true; break; |
| 39 | case Module::AnyPointerSize: |
| 40 | isLongPointer = (sizeof(void*) == 8); // Follow host |
| 41 | break; |
| 42 | } |
| 43 | |
Brian Gaeke | a824f42 | 2003-10-24 19:59:28 +0000 | [diff] [blame] | 44 | return new Interpreter(M, isLittleEndian, isLongPointer); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | //===----------------------------------------------------------------------===// |
| 48 | // Interpreter ctor - Initialize stuff |
| 49 | // |
Brian Gaeke | a824f42 | 2003-10-24 19:59:28 +0000 | [diff] [blame] | 50 | Interpreter::Interpreter(Module *M, bool isLittleEndian, bool isLongPointer) |
| 51 | : ExecutionEngine(M), ExitCode(0), |
| 52 | TD("lli", isLittleEndian, isLongPointer ? 8 : 4, isLongPointer ? 8 : 4, |
| 53 | isLongPointer ? 8 : 4) { |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 54 | |
| 55 | setTargetData(TD); |
| 56 | // Initialize the "backend" |
| 57 | initializeExecutionEngine(); |
Chris Lattner | da82ed5 | 2003-05-08 16:18:31 +0000 | [diff] [blame] | 58 | initializeExternalFunctions(); |
Chris Lattner | 56adf15 | 2003-05-12 02:14:34 +0000 | [diff] [blame] | 59 | emitGlobals(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 62 | void Interpreter::runAtExitHandlers () { |
| 63 | while (!AtExitHandlers.empty()) { |
| 64 | callFunction(AtExitHandlers.back(), std::vector<GenericValue>()); |
| 65 | AtExitHandlers.pop_back(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 66 | run(); |
| 67 | } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 70 | /// run - Start execution with the specified function and arguments. |
| 71 | /// |
Chris Lattner | 2cab55d | 2003-12-26 06:13:05 +0000 | [diff] [blame] | 72 | GenericValue Interpreter::runFunction(Function *F, |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 73 | const std::vector<GenericValue> &ArgValues) { |
| 74 | assert (F && "Function *F was null at entry to run()"); |
Brian Gaeke | 413ab66 | 2003-09-05 04:46:26 +0000 | [diff] [blame] | 75 | |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 76 | // Try extra hard not to pass extra args to a function that isn't |
| 77 | // expecting them. C programmers frequently bend the rules and |
| 78 | // declare main() with fewer parameters than it actually gets |
| 79 | // passed, and the interpreter barfs if you pass a function more |
| 80 | // parameters than it is declared to take. This does not attempt to |
| 81 | // take into account gratuitous differences in declared types, |
| 82 | // though. |
| 83 | std::vector<GenericValue> ActualArgs; |
| 84 | const unsigned ArgCount = F->getFunctionType()->getParamTypes().size(); |
Brian Gaeke | a824f42 | 2003-10-24 19:59:28 +0000 | [diff] [blame] | 85 | for (unsigned i = 0; i < ArgCount; ++i) |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 86 | ActualArgs.push_back (ArgValues[i]); |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 87 | |
| 88 | // Set up the function call. |
| 89 | callFunction(F, ActualArgs); |
Brian Gaeke | 413ab66 | 2003-09-05 04:46:26 +0000 | [diff] [blame] | 90 | |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 91 | // Start executing the function. |
| 92 | run(); |
| 93 | |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 94 | GenericValue rv; |
| 95 | rv.IntVal = ExitCode; |
| 96 | return rv; |
Brian Gaeke | 413ab66 | 2003-09-05 04:46:26 +0000 | [diff] [blame] | 97 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 98 | |