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