Make ExecutionEngine owning a DataLayout
[oota-llvm.git] / lib / ExecutionEngine / Interpreter / Interpreter.cpp
index 3453d831957b5bef88d541527c375c702b061b7b..8cb9d45bb6806759390f0015fd6ee9d340508503 100644 (file)
@@ -1,5 +1,12 @@
 //===- Interpreter.cpp - Top-Level LLVM Interpreter Implementation --------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
 // This file implements the top-level functionality for the LLVM interpreter.
 // This interpreter is designed to be a very simple, portable, inefficient
 // interpreter.
 //===----------------------------------------------------------------------===//
 
 #include "Interpreter.h"
-#include "llvm/Target/TargetMachineImpls.h"
+#include "llvm/CodeGen/IntrinsicLowering.h"
+#include "llvm/IR/DerivedTypes.h"
+#include "llvm/IR/Module.h"
+#include <cstring>
+using namespace llvm;
 
-/// createInterpreter - Create a new interpreter object.  This can never fail.
+namespace {
+
+static struct RegisterInterp {
+  RegisterInterp() { Interpreter::Register(); }
+} InterpRegistrator;
+
+}
+
+extern "C" void LLVMLinkInInterpreter() { }
+
+/// Create a new interpreter object.
 ///
-ExecutionEngine *ExecutionEngine::createInterpreter(Module *M,
-                                                   unsigned Config,
-                                                   bool DebugMode,
-                                                   bool TraceMode) {
-  return new Interpreter(M, Config, DebugMode, TraceMode);
+ExecutionEngine *Interpreter::create(std::unique_ptr<Module> M,
+                                     std::string *ErrStr) {
+  // Tell this Module to materialize everything and release the GVMaterializer.
+  if (std::error_code EC = M->materializeAllPermanently()) {
+    if (ErrStr)
+      *ErrStr = EC.message();
+    // We got an error, just return 0
+    return nullptr;
+  }
+
+  return new Interpreter(std::move(M));
 }
 
 //===----------------------------------------------------------------------===//
 // Interpreter ctor - Initialize stuff
 //
-Interpreter::Interpreter(Module *M, unsigned Config,
-                        bool DebugMode, bool TraceMode)
-  : ExecutionEngine(M), ExitCode(0), Debug(DebugMode), Trace(TraceMode),
-    CurFrame(-1), TD("lli", (Config & TM::EndianMask) == TM::LittleEndian,
-                    (Config & TM::PtrSizeMask) == TM::PtrSize64 ? 8 : 4,
-                    (Config & TM::PtrSizeMask) == TM::PtrSize64 ? 8 : 4,
-                    (Config & TM::PtrSizeMask) == TM::PtrSize64 ? 8 : 4) {
-
-  setTargetData(TD);
+Interpreter::Interpreter(std::unique_ptr<Module> M)
+    : ExecutionEngine(std::move(M)) {
+
+  memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
   // Initialize the "backend"
   initializeExecutionEngine();
   initializeExternalFunctions();
-  CW.setModule(M);  // Update Writer
   emitGlobals();
+
+  IL = new IntrinsicLowering(getDataLayout());
 }
 
-/// run - Start execution with the specified function and arguments.
-///
-int Interpreter::run(const std::string &MainFunction,
-                    const std::vector<std::string> &Args) {
-  // Start interpreter into the main function...
-  //
-  if (!callMainFunction(MainFunction, Args) && !Debug) {
-    // If not in debug mode and if the call succeeded, run the code now...
+Interpreter::~Interpreter() {
+  delete IL;
+}
+
+void Interpreter::runAtExitHandlers () {
+  while (!AtExitHandlers.empty()) {
+    callFunction(AtExitHandlers.back(), None);
+    AtExitHandlers.pop_back();
     run();
   }
-
-  do {
-    // If debug mode, allow the user to interact... also, if the user pressed 
-    // ctrl-c or execution hit an error, enter the event loop...
-    if (Debug || isStopped())
-      handleUserInput();
-
-    // If the program has exited, run atexit handlers...
-    if (ECStack.empty() && !AtExitHandlers.empty()) {
-      callFunction(AtExitHandlers.back(), std::vector<GenericValue>());
-      AtExitHandlers.pop_back();
-      run();
-    }
-  } while (!ECStack.empty());
-
-  PerformExitStuff();
-  return ExitCode;
 }
 
+/// run - Start execution with the specified function and arguments.
+///
+GenericValue Interpreter::runFunction(Function *F,
+                                      ArrayRef<GenericValue> ArgValues) {
+  assert (F && "Function *F was null at entry to run()");
+
+  // Try extra hard not to pass extra args to a function that isn't
+  // expecting them.  C programmers frequently bend the rules and
+  // declare main() with fewer parameters than it actually gets
+  // passed, and the interpreter barfs if you pass a function more
+  // parameters than it is declared to take. This does not attempt to
+  // take into account gratuitous differences in declared types,
+  // though.
+  const size_t ArgCount = F->getFunctionType()->getNumParams();
+  ArrayRef<GenericValue> ActualArgs =
+      ArgValues.slice(0, std::min(ArgValues.size(), ArgCount));
+
+  // Set up the function call.
+  callFunction(F, ActualArgs);
+
+  // Start executing the function.
+  run();
+
+  return ExitValue;
+}