1 //===- Interpreter.cpp - Top-Level LLVM Interpreter Implementation --------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // 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/IR/DerivedTypes.h" 19 #include "llvm/IR/Module.h" 20 #include <cstring> 21 using namespace llvm; 22 23 namespace { 24 25 static struct RegisterInterp { 26 RegisterInterp() { Interpreter::Register(); } 27 } InterpRegistrator; 28 29 } 30 31 extern "C" void LLVMLinkInInterpreter() { } 32 33 /// create - Create a new interpreter object. This can never fail. 34 /// 35 ExecutionEngine *Interpreter::create(Module *M, std::string* ErrStr) { 36 // Tell this Module to materialize everything and release the GVMaterializer. 37 if (error_code EC = M->materializeAllPermanently()) { 38 if (ErrStr) 39 *ErrStr = EC.message(); 40 // We got an error, just return 0 41 return nullptr; 42 } 43 44 return new Interpreter(M); 45 } 46 47 //===----------------------------------------------------------------------===// 48 // Interpreter ctor - Initialize stuff 49 // 50 Interpreter::Interpreter(Module *M) 51 : ExecutionEngine(M), TD(M) { 52 53 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped)); 54 setDataLayout(&TD); 55 // Initialize the "backend" 56 initializeExecutionEngine(); 57 initializeExternalFunctions(); 58 emitGlobals(); 59 60 IL = new IntrinsicLowering(TD); 61 } 62 63 Interpreter::~Interpreter() { 64 delete IL; 65 } 66 67 void Interpreter::runAtExitHandlers () { 68 while (!AtExitHandlers.empty()) { 69 callFunction(AtExitHandlers.back(), std::vector<GenericValue>()); 70 AtExitHandlers.pop_back(); 71 run(); 72 } 73 } 74 75 /// run - Start execution with the specified function and arguments. 76 /// 77 GenericValue 78 Interpreter::runFunction(Function *F, 79 const std::vector<GenericValue> &ArgValues) { 80 assert (F && "Function *F was null at entry to run()"); 81 82 // Try extra hard not to pass extra args to a function that isn't 83 // expecting them. C programmers frequently bend the rules and 84 // declare main() with fewer parameters than it actually gets 85 // passed, and the interpreter barfs if you pass a function more 86 // parameters than it is declared to take. This does not attempt to 87 // take into account gratuitous differences in declared types, 88 // though. 89 std::vector<GenericValue> ActualArgs; 90 const unsigned ArgCount = F->getFunctionType()->getNumParams(); 91 for (unsigned i = 0; i < ArgCount; ++i) 92 ActualArgs.push_back(ArgValues[i]); 93 94 // Set up the function call. 95 callFunction(F, ActualArgs); 96 97 // Start executing the function. 98 run(); 99 100 return ExitValue; 101 } 102