Interpreter cleanups:
[oota-llvm.git] / lib / ExecutionEngine / Interpreter / Interpreter.cpp
1 //===- Interpreter.cpp - Top-Level LLVM Interpreter Implementation --------===//
2 //
3 // This file implements the top-level functionality for the LLVM interpreter.
4 // This interpreter is designed to be a very simple, portable, inefficient
5 // interpreter.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "Interpreter.h"
10 #include "llvm/Module.h"
11
12 /// create - Create a new interpreter object.  This can never fail.
13 ///
14 ExecutionEngine *Interpreter::create(Module *M, bool TraceMode){
15   bool isLittleEndian;
16   switch (M->getEndianness()) {
17   case Module::LittleEndian: isLittleEndian = true; break;
18   case Module::BigEndian:    isLittleEndian = false; break;
19   case Module::AnyPointerSize:
20     int Test = 0;
21     *(char*)&Test = 1;    // Return true if the host is little endian
22     isLittleEndian = (Test == 1);
23     break;
24   }
25
26   bool isLongPointer;
27   switch (M->getPointerSize()) {
28   case Module::Pointer32: isLongPointer = false; break;
29   case Module::Pointer64: isLongPointer = true; break;
30   case Module::AnyPointerSize:
31     isLongPointer = (sizeof(void*) == 8);  // Follow host
32     break;
33   }
34
35   return new Interpreter(M, isLittleEndian, isLongPointer, TraceMode);
36 }
37
38 //===----------------------------------------------------------------------===//
39 // Interpreter ctor - Initialize stuff
40 //
41 Interpreter::Interpreter(Module *M, bool isLittleEndian, bool isLongPointer,
42                          bool TraceMode)
43   : ExecutionEngine(M), ExitCode(0), Trace(TraceMode),
44     CurFrame(-1), TD("lli", isLittleEndian, isLongPointer ? 8 : 4,
45                      isLongPointer ? 8 : 4, isLongPointer ? 8 : 4) {
46
47   setTargetData(TD);
48   // Initialize the "backend"
49   initializeExecutionEngine();
50   initializeExternalFunctions();
51   CW.setModule(M);  // Update Writer
52   emitGlobals();
53 }
54
55 /// run - Start execution with the specified function and arguments.
56 ///
57 int Interpreter::run(const std::string &MainFunction,
58                      const std::vector<std::string> &Args,
59                      const char ** envp) {
60   // Start interpreter into the main function...
61   //
62   if (!callMainFunction(MainFunction, Args)) {
63     // If the call succeeded, run the code now...
64     run();
65   }
66
67   do {
68     // If the program has exited, run atexit handlers...
69     if (ECStack.empty() && !AtExitHandlers.empty()) {
70       callFunction(AtExitHandlers.back(), std::vector<GenericValue>());
71       AtExitHandlers.pop_back();
72       run();
73     }
74   } while (!ECStack.empty());
75
76   return ExitCode;
77 }
78