Remove tabs.
[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 using namespace llvm;
22
23 static struct RegisterInterp {
24   RegisterInterp() { Interpreter::Register(); }
25 } InterpRegistrator;
26
27 namespace llvm {
28   void LinkInInterpreter() {
29   }
30 }
31
32 /// create - Create a new interpreter object.  This can never fail.
33 ///
34 ExecutionEngine *Interpreter::create(ModuleProvider *MP) {
35   Module *M;
36   try {
37     M = MP->materializeModule();
38   } catch (...) {
39     return 0;  // error materializing the module.
40   }
41   
42   // FIXME: This should probably compute the entire data layout
43   std::string DataLayout;
44   int Test = 0;
45   *(char*)&Test = 1;    // Return true if the host is little endian
46   bool isLittleEndian = (Test == 1);
47   DataLayout.append(isLittleEndian ? "e" : "E");
48
49   bool Ptr64 = sizeof(void*) == 8;
50   DataLayout.append(Ptr64 ? "-p:64:64" : "-p:32:32");
51         
52   M->setDataLayout(DataLayout);
53
54   return new Interpreter(M);
55 }
56
57 //===----------------------------------------------------------------------===//
58 // Interpreter ctor - Initialize stuff
59 //
60 Interpreter::Interpreter(Module *M) : ExecutionEngine(M), TD(M) {
61       
62   memset(&ExitValue, 0, sizeof(ExitValue));
63   setTargetData(&TD);
64   // Initialize the "backend"
65   initializeExecutionEngine();
66   initializeExternalFunctions();
67   emitGlobals();
68
69   IL = new IntrinsicLowering(TD);
70 }
71
72 Interpreter::~Interpreter() {
73   delete IL;
74 }
75
76 void Interpreter::runAtExitHandlers () {
77   while (!AtExitHandlers.empty()) {
78     callFunction(AtExitHandlers.back(), std::vector<GenericValue>());
79     AtExitHandlers.pop_back();
80     run();
81   }
82 }
83
84 /// run - Start execution with the specified function and arguments.
85 ///
86 GenericValue
87 Interpreter::runFunction(Function *F,
88                          const std::vector<GenericValue> &ArgValues) {
89   assert (F && "Function *F was null at entry to run()");
90
91   // Try extra hard not to pass extra args to a function that isn't
92   // expecting them.  C programmers frequently bend the rules and
93   // declare main() with fewer parameters than it actually gets
94   // passed, and the interpreter barfs if you pass a function more
95   // parameters than it is declared to take. This does not attempt to
96   // take into account gratuitous differences in declared types,
97   // though.
98   std::vector<GenericValue> ActualArgs;
99   const unsigned ArgCount = F->getFunctionType()->getNumParams();
100   for (unsigned i = 0; i < ArgCount; ++i)
101     ActualArgs.push_back(ArgValues[i]);
102
103   // Set up the function call.
104   callFunction(F, ActualArgs);
105
106   // Start executing the function.
107   run();
108
109   return ExitValue;
110 }
111