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