LLVM API Documentation
00001 //===- Interpreter.cpp - Top-Level LLVM Interpreter Implementation --------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // 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 #include <cstring> 00022 using namespace llvm; 00023 00024 namespace { 00025 00026 static struct RegisterInterp { 00027 RegisterInterp() { Interpreter::Register(); } 00028 } InterpRegistrator; 00029 00030 } 00031 00032 extern "C" void LLVMLinkInInterpreter() { } 00033 00034 /// create - Create a new interpreter object. This can never fail. 00035 /// 00036 ExecutionEngine *Interpreter::create(ModuleProvider *MP, std::string* ErrStr) { 00037 // Tell this ModuleProvide to materialize and release the module 00038 if (!MP->materializeModule(ErrStr)) 00039 // We got an error, just return 0 00040 return 0; 00041 00042 return new Interpreter(MP); 00043 } 00044 00045 //===----------------------------------------------------------------------===// 00046 // Interpreter ctor - Initialize stuff 00047 // 00048 Interpreter::Interpreter(ModuleProvider *M) 00049 : ExecutionEngine(M), TD(M->getModule()) { 00050 00051 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped)); 00052 setTargetData(&TD); 00053 // Initialize the "backend" 00054 initializeExecutionEngine(); 00055 initializeExternalFunctions(); 00056 emitGlobals(); 00057 00058 IL = new IntrinsicLowering(TD); 00059 } 00060 00061 Interpreter::~Interpreter() { 00062 delete IL; 00063 } 00064 00065 void Interpreter::runAtExitHandlers () { 00066 while (!AtExitHandlers.empty()) { 00067 callFunction(AtExitHandlers.back(), std::vector<GenericValue>()); 00068 AtExitHandlers.pop_back(); 00069 run(); 00070 } 00071 } 00072 00073 /// run - Start execution with the specified function and arguments. 00074 /// 00075 GenericValue 00076 Interpreter::runFunction(Function *F, 00077 const std::vector<GenericValue> &ArgValues) { 00078 assert (F && "Function *F was null at entry to run()"); 00079 00080 // Try extra hard not to pass extra args to a function that isn't 00081 // expecting them. C programmers frequently bend the rules and 00082 // declare main() with fewer parameters than it actually gets 00083 // passed, and the interpreter barfs if you pass a function more 00084 // parameters than it is declared to take. This does not attempt to 00085 // take into account gratuitous differences in declared types, 00086 // though. 00087 std::vector<GenericValue> ActualArgs; 00088 const unsigned ArgCount = F->getFunctionType()->getNumParams(); 00089 for (unsigned i = 0; i < ArgCount; ++i) 00090 ActualArgs.push_back(ArgValues[i]); 00091 00092 // Set up the function call. 00093 callFunction(F, ActualArgs); 00094 00095 // Start executing the function. 00096 run(); 00097 00098 return ExitValue; 00099 }