LLVM API Documentation
00001 //===- Interpreter.cpp - Top-Level LLVM Interpreter Implementation --------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the top-level functionality for the LLVM interpreter. 00011 // This interpreter is designed to be a very simple, portable, inefficient 00012 // interpreter. 00013 // 00014 //===----------------------------------------------------------------------===// 00015 00016 #include "Interpreter.h" 00017 #include "llvm/CodeGen/IntrinsicLowering.h" 00018 #include "llvm/DerivedTypes.h" 00019 #include "llvm/Module.h" 00020 #include "llvm/ModuleProvider.h" 00021 using namespace llvm; 00022 00023 static struct RegisterInterp { 00024 RegisterInterp() { Interpreter::Register(); } 00025 } InterpRegistrator; 00026 00027 namespace llvm { 00028 void LinkInInterpreter() { 00029 } 00030 } 00031 00032 /// create - Create a new interpreter object. This can never fail. 00033 /// 00034 ExecutionEngine *Interpreter::create(ModuleProvider *MP) { 00035 Module *M; 00036 try { 00037 M = MP->materializeModule(); 00038 } catch (...) { 00039 return 0; // error materializing the module. 00040 } 00041 00042 if (M->getEndianness() == Module::AnyEndianness) { 00043 int Test = 0; 00044 *(char*)&Test = 1; // Return true if the host is little endian 00045 bool isLittleEndian = (Test == 1); 00046 M->setEndianness(isLittleEndian ? Module::LittleEndian : Module::BigEndian); 00047 } 00048 00049 if (M->getPointerSize() == Module::AnyPointerSize) { 00050 // Follow host. 00051 bool Ptr64 = sizeof(void*) == 8; 00052 M->setPointerSize(Ptr64 ? Module::Pointer64 : Module::Pointer32); 00053 } 00054 00055 return new Interpreter(M); 00056 } 00057 00058 //===----------------------------------------------------------------------===// 00059 // Interpreter ctor - Initialize stuff 00060 // 00061 Interpreter::Interpreter(Module *M) : ExecutionEngine(M), TD(M) { 00062 00063 memset(&ExitValue, 0, sizeof(ExitValue)); 00064 setTargetData(&TD); 00065 // Initialize the "backend" 00066 initializeExecutionEngine(); 00067 initializeExternalFunctions(); 00068 emitGlobals(); 00069 00070 IL = new DefaultIntrinsicLowering(); 00071 } 00072 00073 Interpreter::~Interpreter() { 00074 delete IL; 00075 } 00076 00077 void Interpreter::runAtExitHandlers () { 00078 while (!AtExitHandlers.empty()) { 00079 callFunction(AtExitHandlers.back(), std::vector<GenericValue>()); 00080 AtExitHandlers.pop_back(); 00081 run(); 00082 } 00083 } 00084 00085 /// run - Start execution with the specified function and arguments. 00086 /// 00087 GenericValue 00088 Interpreter::runFunction(Function *F, 00089 const std::vector<GenericValue> &ArgValues) { 00090 assert (F && "Function *F was null at entry to run()"); 00091 00092 // Try extra hard not to pass extra args to a function that isn't 00093 // expecting them. C programmers frequently bend the rules and 00094 // declare main() with fewer parameters than it actually gets 00095 // passed, and the interpreter barfs if you pass a function more 00096 // parameters than it is declared to take. This does not attempt to 00097 // take into account gratuitous differences in declared types, 00098 // though. 00099 std::vector<GenericValue> ActualArgs; 00100 const unsigned ArgCount = F->getFunctionType()->getNumParams(); 00101 for (unsigned i = 0; i < ArgCount; ++i) 00102 ActualArgs.push_back(ArgValues[i]); 00103 00104 // Set up the function call. 00105 callFunction(F, ActualArgs); 00106 00107 // Start executing the function. 00108 run(); 00109 00110 return ExitValue; 00111 } 00112