Interpreter.cpp revision 00b16889ab461b7ecef1c91ade101186b7f1fce2
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" 20using namespace llvm; 21 22/// create - Create a new interpreter object. This can never fail. 23/// 24ExecutionEngine *Interpreter::create(Module *M, IntrinsicLowering *IL) { 25 bool isLittleEndian = false; 26 switch (M->getEndianness()) { 27 case Module::LittleEndian: isLittleEndian = true; break; 28 case Module::BigEndian: isLittleEndian = false; break; 29 case Module::AnyPointerSize: 30 int Test = 0; 31 *(char*)&Test = 1; // Return true if the host is little endian 32 isLittleEndian = (Test == 1); 33 break; 34 } 35 36 bool isLongPointer = false; 37 switch (M->getPointerSize()) { 38 case Module::Pointer32: isLongPointer = false; break; 39 case Module::Pointer64: isLongPointer = true; break; 40 case Module::AnyPointerSize: 41 isLongPointer = (sizeof(void*) == 8); // Follow host 42 break; 43 } 44 45 return new Interpreter(M, isLittleEndian, isLongPointer, IL); 46} 47 48//===----------------------------------------------------------------------===// 49// Interpreter ctor - Initialize stuff 50// 51Interpreter::Interpreter(Module *M, bool isLittleEndian, bool isLongPointer, 52 IntrinsicLowering *il) 53 : ExecutionEngine(M), ExitCode(0), 54 TD("lli", isLittleEndian, isLongPointer ? 8 : 4, isLongPointer ? 8 : 4, 55 isLongPointer ? 8 : 4), IL(il) { 56 57 setTargetData(TD); 58 // Initialize the "backend" 59 initializeExecutionEngine(); 60 initializeExternalFunctions(); 61 emitGlobals(); 62 63 if (IL == 0) IL = new DefaultIntrinsicLowering(); 64} 65 66Interpreter::~Interpreter() { 67 delete IL; 68} 69 70void Interpreter::runAtExitHandlers () { 71 while (!AtExitHandlers.empty()) { 72 callFunction(AtExitHandlers.back(), std::vector<GenericValue>()); 73 AtExitHandlers.pop_back(); 74 run(); 75 } 76} 77 78/// run - Start execution with the specified function and arguments. 79/// 80GenericValue 81Interpreter::runFunction(Function *F, 82 const std::vector<GenericValue> &ArgValues) { 83 assert (F && "Function *F was null at entry to run()"); 84 85 // Try extra hard not to pass extra args to a function that isn't 86 // expecting them. C programmers frequently bend the rules and 87 // declare main() with fewer parameters than it actually gets 88 // passed, and the interpreter barfs if you pass a function more 89 // parameters than it is declared to take. This does not attempt to 90 // take into account gratuitous differences in declared types, 91 // though. 92 std::vector<GenericValue> ActualArgs; 93 const unsigned ArgCount = F->getFunctionType()->getNumParams(); 94 for (unsigned i = 0; i < ArgCount; ++i) 95 ActualArgs.push_back(ArgValues[i]); 96 97 // Set up the function call. 98 callFunction(F, ActualArgs); 99 100 // Start executing the function. 101 run(); 102 103 GenericValue rv; 104 rv.IntVal = ExitCode; 105 return rv; 106} 107 108