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