Get JIT/Interpreter working on Windows again.
[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   bool isLittleEndian = false;
43   switch (M->getEndianness()) {
44   case Module::LittleEndian: isLittleEndian = true; break;
45   case Module::BigEndian:    isLittleEndian = false; break;
46   case Module::AnyPointerSize:
47     int Test = 0;
48     *(char*)&Test = 1;    // Return true if the host is little endian
49     isLittleEndian = (Test == 1);
50     break;
51   }
52
53   bool isLongPointer = false;
54   switch (M->getPointerSize()) {
55   case Module::Pointer32: isLongPointer = false; break;
56   case Module::Pointer64: isLongPointer = true; break;
57   case Module::AnyPointerSize:
58     isLongPointer = (sizeof(void*) == 8);  // Follow host
59     break;
60   }
61
62   return new Interpreter(M, isLittleEndian, isLongPointer);
63 }
64
65 //===----------------------------------------------------------------------===//
66 // Interpreter ctor - Initialize stuff
67 //
68 Interpreter::Interpreter(Module *M, bool isLittleEndian, bool isLongPointer)
69   : ExecutionEngine(M),
70     TD("lli", isLittleEndian, isLongPointer ? 8 : 4, isLongPointer ? 8 : 4,
71        isLongPointer ? 8 : 4) {
72
73   memset(&ExitValue, 0, sizeof(ExitValue));
74   setTargetData(TD);
75   // Initialize the "backend"
76   initializeExecutionEngine();
77   initializeExternalFunctions();
78   emitGlobals();
79
80   IL = new DefaultIntrinsicLowering();
81 }
82
83 Interpreter::~Interpreter() {
84   delete IL;
85 }
86
87 void Interpreter::runAtExitHandlers () {
88   while (!AtExitHandlers.empty()) {
89     callFunction(AtExitHandlers.back(), std::vector<GenericValue>());
90     AtExitHandlers.pop_back();
91     run();
92   }
93 }
94
95 /// run - Start execution with the specified function and arguments.
96 ///
97 GenericValue
98 Interpreter::runFunction(Function *F,
99                          const std::vector<GenericValue> &ArgValues) {
100   assert (F && "Function *F was null at entry to run()");
101
102   // Try extra hard not to pass extra args to a function that isn't
103   // expecting them.  C programmers frequently bend the rules and
104   // declare main() with fewer parameters than it actually gets
105   // passed, and the interpreter barfs if you pass a function more
106   // parameters than it is declared to take. This does not attempt to
107   // take into account gratuitous differences in declared types,
108   // though.
109   std::vector<GenericValue> ActualArgs;
110   const unsigned ArgCount = F->getFunctionType()->getNumParams();
111   for (unsigned i = 0; i < ArgCount; ++i)
112     ActualArgs.push_back(ArgValues[i]);
113
114   // Set up the function call.
115   callFunction(F, ActualArgs);
116
117   // Start executing the function.
118   run();
119
120   return ExitValue;
121 }
122