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