Deal with error handling better.
[oota-llvm.git] / lib / ExecutionEngine / Interpreter / Interpreter.cpp
1 //===- Interpreter.cpp - Top-Level LLVM Interpreter Implementation --------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 <iostream>
22 using namespace llvm;
23
24 static struct RegisterInterp {
25   RegisterInterp() { Interpreter::Register(); }
26 } InterpRegistrator;
27
28 namespace llvm {
29   void LinkInInterpreter() {
30   }
31 }
32
33 /// create - Create a new interpreter object.  This can never fail.
34 ///
35 ExecutionEngine *Interpreter::create(ModuleProvider *MP, std::string* ErrStr) {
36   // Tell this ModuleProvide to materialize and release the module
37   Module *M = MP->releaseModule(ErrStr);
38   if (!M)
39     // We got an error, just return 0
40     return 0;
41
42   // This is a bit nasty, but the ExecutionEngine won't be able to delete the
43   // module due to use/def issues if we don't delete this MP here. Below we
44   // construct a new Interpreter with the Module we just got. This creates a
45   // new ExistingModuleProvider in the EE instance. Consequently, MP is left
46   // dangling and it contains references into the module which cause problems
47   // when the module is deleted via the ExistingModuleProvide via EE.
48   delete MP;
49   
50   // FIXME: This should probably compute the entire data layout
51   std::string DataLayout;
52   int Test = 0;
53   *(char*)&Test = 1;    // Return true if the host is little endian
54   bool isLittleEndian = (Test == 1);
55   DataLayout.append(isLittleEndian ? "e" : "E");
56
57   bool Ptr64 = sizeof(void*) == 8;
58   DataLayout.append(Ptr64 ? "-p:64:64" : "-p:32:32");
59         
60   M->setDataLayout(DataLayout);
61
62   return new Interpreter(M);
63 }
64
65 //===----------------------------------------------------------------------===//
66 // Interpreter ctor - Initialize stuff
67 //
68 Interpreter::Interpreter(Module *M) : ExecutionEngine(M), TD(M) {
69       
70   memset(&ExitValue, 0, sizeof(ExitValue));
71   setTargetData(&TD);
72   // Initialize the "backend"
73   initializeExecutionEngine();
74   initializeExternalFunctions();
75   emitGlobals();
76
77   IL = new IntrinsicLowering(TD);
78 }
79
80 Interpreter::~Interpreter() {
81   delete IL;
82 }
83
84 void Interpreter::runAtExitHandlers () {
85   while (!AtExitHandlers.empty()) {
86     callFunction(AtExitHandlers.back(), std::vector<GenericValue>());
87     AtExitHandlers.pop_back();
88     run();
89   }
90 }
91
92 /// run - Start execution with the specified function and arguments.
93 ///
94 GenericValue
95 Interpreter::runFunction(Function *F,
96                          const std::vector<GenericValue> &ArgValues) {
97   assert (F && "Function *F was null at entry to run()");
98
99   // Try extra hard not to pass extra args to a function that isn't
100   // expecting them.  C programmers frequently bend the rules and
101   // declare main() with fewer parameters than it actually gets
102   // passed, and the interpreter barfs if you pass a function more
103   // parameters than it is declared to take. This does not attempt to
104   // take into account gratuitous differences in declared types,
105   // though.
106   std::vector<GenericValue> ActualArgs;
107   const unsigned ArgCount = F->getFunctionType()->getNumParams();
108   for (unsigned i = 0; i < ArgCount; ++i)
109     ActualArgs.push_back(ArgValues[i]);
110
111   // Set up the function call.
112   callFunction(F, ActualArgs);
113
114   // Start executing the function.
115   run();
116
117   return ExitValue;
118 }
119