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